15 min read — Published 1 month ago

Styling the React Hero Component with CSS and Sass

Craft stunning landing pages fast with our pre-built React Hero component. Easily customize and style with CSS/Sass. Get started now!

Landing Page Examples
Featured Image

Creating a landing page can feel like a daunting task. You have to organize a ton of information, prioritize some aspects over others, and optimally style the page to achieve your conversion goals. With so much to do, it’s easy to overlook individual components, such as the hero section. The hero section is crucial for grabbing users’ attention and spearheading their journey through your landing page. This is especially true for React landing page examples, which help you quickly build interactive and engaging web applications. Here, we’ll focus on the hero component and how you can style it with CSS and Sass.

MagicUI's startup landing page template has a ready-to-use React hero component to help you get started. It’s packed with sample content to illustrate the structure, so you can quickly customize it to fit your needs and create a stunning landing page.

Overview of the React Hero Component

React Hero ComponentReact Hero Component

In a React application, a Hero Component is a visually prominent section at the top of a webpage. It is designed to be the first thing users see when they land on the site, often including:

  • Large image or video

  • Headline

  • Subheadline

  • Call-to-action (CTA) button

Its primary purpose is to:

  • Grab attention

  • Convey a key message

  • Guide users toward necessary actions

Significance of the Hero Component 

First Impressions

It creates an immediate impact by being the first visual element users encounter, setting the tone for the website.

Brand Messaging

It effectively communicates the brand or product's core message or value proposition.

User Engagement

Engages users by incorporating interactive elements like CTA buttons, encouraging them to take actions such as signing up or making a purchase.

Visual Appeal

Enhances the overall user experience by visually appealing design elements to capture interest.

Guides users towards necessary actions, improving site usability and conversion rates.

Dropbox

Uses a simple and clean Hero Component with a headline, brief description, and a CTA button for user sign-up or login.

Airbnb

Features high-quality images of destinations with a search bar and CTA, driving users to start their booking process.

Slack

It employs vibrant visuals and a strong headline, including a CTA button to try the product or learn more.

Importance of Styling in User Experience

React Hero ComponentReact Hero Component

Visual Appeal and User Perception

The visual appeal of a Hero Component plays a critical role in shaping user perception and interaction. A well-designed Hero Component engages users immediately with:

  • High-quality visuals 

  • Compelling text

It reflects the brand’s professionalism, which helps build trust with users. The site is also easier to navigate with clear and intuitive design elements.

Studies and Surveys

  • Adobe Study: Indicates that 38% of users will only engage with a website if the content or layout is attractive.

  • HubSpot Report: Personalized and aesthetically pleasing Hero Components can boost conversion rates by up to 202%

Understanding the Basics of CSS and Sass

React Hero ComponentReact Hero Component

CSS Overview

Cascading Style Sheets CSS styles web pages by controlling the:

  • Layout

  • Colors

  • Typography

  • Other visual aspects

Css Helps In Layout

Managing how elements are positioned and sized. 

Colors: Defining text, background, and border colors. 

Typography

Handling font styles, sizes, and text alignment. 

Visual Effects: Applying shadows, gradients, and transitions to enhance the design.

Sass Overview

Syntactically Awesome Style Sheets Sass extends CSS with additional features: 

Variables

Allows the use of reusable variables for consistent styling. 

  • Example: $primary-color: #3498db; 

  • Nesting: Enables the nesting of selectors to improve readability and organization. 

  • Example: scss Copy code .hero { .title { font-size: 2rem; } } 

  • Mixins: Facilitates the reuse of common styles across different elements. 

  • Example: scss Copy code @mixin button-style($color) { background-color: $color; border: none; padding: 10px; }

  • Functions: Allows dynamic styling through calculations and manipulations. 

  • Example: scss Copy code @function calculate-spacing($base, $multiplier) { @return $base * $multiplier; }

Key Differences Between CSS and Sass

React Hero ComponentReact Hero Component

CSS vs. Sass: What’s the Difference?

Developers can use either CSS or Sass when styling a website. Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor that adds functionality to standard CSS. When developers write code in Sass, they write code in a language that resembles CSS before compiling it into standard CSS. This approach offers users many benefits, especially with large style sheets.

Sass Is a CSS Extension

Sass is a superset of CSS that adds functionality to standard CSS. This means that all the code valid in CSS is also valid in Sass. Developers can start a project using Sass right away without any conversion. As they build styles for their project, they can compile the Sass into CSS for the browser to read.

Sass Syntax Is Cleaner

While CSS and Sass have similar structures, Sass's syntax is much cleaner. There’s no need for repetitive code in Sass, which makes the structure much more readable. Developers can use nesting, mixins, and variables to reduce redundancy in their styles.

Sass Includes Variables for Consistency

Sass supports variables for colors, fonts, or any CSS value that can be reused throughout a style sheet. While CSS also supports custom properties, Sass variables are much more powerful. Sass variables can be updated throughout the document, while CSS custom properties can only be updated with JavaScript.

Sass Allows Nesting of Selectors

CSS allows developers to nest selectors to reduce redundancy and make the code more readable. This feature helps organize styles, visually showing the relationship between parent and child elements. In contrast, CSS does not support nesting, so developers must write out every class independently.

Sass Mixins Reduce Repetitive Code

Sass mixins allow developers to create reusable styling blocks. When working on a project, developers can define a group of CSS properties within a mixin and call that mixin wherever they want to use those styles. This functionality reduces redundancy and helps to keep the code DRY (Don’t Repeat Yourself).

Sass Supports Inheritance

Sass supports inheritance with the @extend feature. This allows developers to share styles between selectors, so they don’t have to write out all the properties for similar classes. While CSS has a form of inheritance, it doesn’t allow for this type of code sharing.

Sass Includes Functions for Calculations

Sass includes built-in functions for mathematical calculations. When using Sass, developers can perform calculations and use the results directly in their styles. This helps to:

  • Eliminate errors 

  • Improve efficiency

While CSS now supports its calculations with the calc() function, it doesn’t have the same advanced functionality as Sass.

Sass Promotes Modularity

Sass promotes modularity with partials and imports. Partials are small Sass files that can be included in other Sass files. This allows developers to break their styles into smaller, more manageable pieces that can be organized logically. 

Imports allow a main Sass file to pull in styles from other smaller files. This modular approach helps to:

  • Keep projects organized 

  • Improve maintainability

Sass Offers Error Checking

Sass comes with an enhanced error-checking feature. When developers compile their Sass into CSS, they receive a detailed report on any errors or issues in the code. The report includes the line numbers for both the Sass and CSS files, making it easy to troubleshoot problems.

Sass Requires Compilation

Sass must be compiled or converted into CSS before a browser can read it. In contrast, browsers directly interpret CSS. This means developers must create a system to compile Sass files for their projects. Many code editors now come with built-in Sass compilation, which streamlines the process for developers.

Sass Simplifies Complex Styling Tasks

Using plain CSS to manage complex styling for a Hero Component might lead to repetitive code for similar elements. Sass simplifies this by using mixins for typical styles and variables for color schemes:

  • Reducing code duplication 

  • Improving maintainability

Setting Up a React Project with CSS and Sass

React Hero ComponentReact Hero Component

Setting Up a Basic React Project with CSS

1. Create a React Project

Run the following command to create a new React application:
`npx create-react-app my-app`.

2. Add CSS Files

Create .css files inside your project’s src directory for styling.

3. Import CSS

Import CSS files into your React components using:
`import './App.css';`.

4. Apply Styles

Use class names in JSX to apply styles.

Setting Up a React Project with Sass

1. Create a React Project

Run the following command to create a new React application:
`npx create-react-app my-app`.

2. Install Sass

Execute npm install sass to add Sass support to your React project.

3. Rename Files

Change .css files to .scss.

4. Import Sass 

Import .scss files into React components with:
import './App.scss';.

5. Compile Sass

Sass compilation is handled automatically during development.

Structuring Stylesheets in a React Project

It’s good practice to structure your stylesheets in a modular way. This helps keep your React projects organized and easier to maintain.

Component-Specific Styles 

Create separate .scss or .css files for individual components to maintain modularity.

Global Styles 

Implement a global stylesheet (e.g., global.scss) for common styles and variables.

Partial Files 

In Sass, create partials (e.g., _variables.scss) to organize reusable styles and import them where needed. 

Styling the React Hero Component Using CSS

Styling a React Hero component using CSS is simple once you know how to do it. A Hero component is a significant, prominent component usually placed at the top of a website and contains eye-catching content encouraging users to take action.  Hero Components are often styled using CSS, which can be done in React just like in regular HTML.

Create a Hero Component in React

To start, we need to create a Hero component. Below is an example of a simple Hero Component built with React.

javascript import React from 'react'; import './Hero.css'; const Hero = () => (   <div className="hero">     <h1 className="hero-title">Welcome to Our Website</h1>     <p className="hero-subtitle">Discover amazing content</p>     <button className="hero-cta">Get Started</button>   </div> ); export default Hero;

The first step is to import React and create a functional component. Next, we define the structure of the Hero component. In this example, the Hero Component contains a title, a subtitle, and a call to action button. We export the component so we can import and use it in other files.

Define CSS Styles for the Hero Component 

We must create a CSS file for the Hero Component styles. Below is an example of some CSS styles we can use to style the Hero Component.

css .hero {   background: url('hero-bg.jpg') no-repeat center center;   background-size: cover;   text-align: center;   padding: 50px 20px; } .hero-title {   font-size: 3rem;   color: #fff; } .hero-subtitle {   font-size: 1.5rem;   color: #ddd; } .hero-cta {   background-color: #3498db;   color: #fff;   border: none;   padding: 10px 20px;   font-size: 1rem;   cursor: pointer; }

Notice how the CSS targets specific classes we defined in the component. This is how we can build and style a Hero Component in React. 

Enhancing Hero Component Styling with Sass

React Hero ComponentReact Hero Component

Sass variables allow you to define reusable values for colors, fonts, spacing, and other design properties. Using variables ensures consistency across your styling and makes it easier to manage and update your design. 

Color Variables

Instead of hardcoding color values throughout your stylesheets, you can define them as variables. If you need to change a color, you only need to update it in one place rather than multiple CSS rules. Set up a color scheme by defining variables for primary and secondary colors. 

You can then use these variables throughout your Hero Component to maintain a consistent color palette. This approach ensures that all elements share the same colors and that any updates to the color scheme are applied uniformly across the component.

Nesting Selectors for Cleaner Code

Sass nesting allows you to organize related styles together, which enhances readability and maintainability. Instead of writing flat, repetitive CSS rules, you can nest selectors within their parent elements. This method mirrors the HTML structure, making the stylesheet:

  • More intuitive 

  • Easier to navigate

For a Hero Component, you might nest styles for the title, subtitle, and CTA buttons within the .hero class. This nesting clearly shows the relationship between these elements and their parent container, reducing redundancy and simplifying the management of complex styles.

Creating Reusable Mixins for Common Styles

Mixins in Sass enable you to define reusable chunks of CSS that can be applied to multiple elements. They are handy for consistently applying common styles like button styles, shadows, or padding throughout the component. Using mixins helps avoid repetition and ensures uniformity in styling. 

Button Mixin

Create a mixin for consistent button styling that includes:

  • Padding

  • Background color

  • Border properties

You can then include this mixin in various buttons within your Hero Component, ensuring all buttons have the same styling without duplicating code.

 Leveraging Sass Functions for Advanced Styling

Sass functions allow you to perform calculations and create dynamic styles based on input values. They can be used for tasks like calculating responsive font sizes, spacing, or other design metrics based on screen size or other variables. 

You can use a Sass function to calculate responsive font sizes that adjust based on the viewport width. This dynamic approach ensures that text remains readable and visually appealing across:

  • Different devices 

  • Screen sizes

Optimizing the Hero Component for Performance

React Hero ComponentReact Hero Component

Lightweight Stylesheets Are Your Hero Component's Best Friend

To improve performance, it's crucial to keep your stylesheets lightweight. This involves 

minifying CSS files to reduce size and removing unused styles to eliminate unnecessary code. Smaller files load faster, enhancing:

  • Page load times 

  • Overall user experience 

Research shows that large CSS files can significantly impact page load times. For instance, reducing CSS file size by just 100 KB can improve load times by 1-2 seconds, depending on the user's internet speed.

Use Lazy Loading to Reduce Your Hero Component's Initial Load Time

Lazy loading defers the loading of background images until they are needed, which helps improve initial page load times. By loading images only when they enter the viewport or are about to be viewed, you reduce the amount of data that needs to be loaded upfront, enhancing overall page performance. 

Implement lazy loading for the Hero Component's background image. Instead of loading the image with the initial page load, use a placeholder or a lower-resolution version until the user scrolls to the Hero Component.

Avoid Common CSS Pitfalls That Negatively Impact Performance

Common CSS mistakes can negatively impact performance. These include:

  • Excessive nesting leading to complex and inefficient CSS rules 

  • Extensive media queries that slow-rendering

Simplifying styles and reducing complexity help maintain performance. If a React application’s performance is impacted by excessive nesting, refactor the styles to reduce nesting levels and optimize media queries. This can lead to a more efficient rendering process and improved performance.

How to Create Scalable React Components with Styled Components

React Hero ComponentReact Hero Component

Styled Components is a library for styling React components using CSS-in-JS. It allows you to create reusable and scalable UI components by combining JavaScript and CSS in a single file. Styled Components automatically handle the generation of unique class names to avoid style conflicts and ensure that styles are scoped to the components they are applied to.

Build Your Own React Hero Component with Styled Components

One of the biggest benefits of using Styled Components in React is that it helps promote the development of scalable components. Each styled component you create encapsulates its style, which helps avoid global style conflicts. This makes it easier to manage and update styles as your application grows. 

If you want to build a hero component for your application, using Styled Components can help ensure that this reusable UI component is easy to maintain and will not cause styling issues later on.

Check Out Our React Component Library for Design Engineers

MagicUI is a free, open-source UI library that helps design engineers create stunning user interfaces for web applications. With its collection of over 20 animated components built with:

  • React

  • TypeScript

  • Tailwind CSS

  • Framer Motion

Animated Components

This library focuses on interactive UI elements with a strong emphasis on animation. The result is a design-centric approach that helps bridge the gap between design and development, empowering engineers to craft captivating digital experiences. The highly customizable MagicUI components enable seamless adaptation to match any desired branding and design requirements. 

In addition to the free library, MagicUI Pro offers website templates that can help reduce time and costs. You can save thousands of hours using our startup landing page template to create a beautiful landing page and convert your visitors into customers. 

Dillion Verma


Share this post


© 2024 Magic UI

Made with Engyne