How To Use Inline Styles in ReactJS?
Last Updated :
17 Jul, 2024
Inline styles in React allow you to directly apply CSS styles to elements using the style attribute. This is often used for dynamic styling, where styles need to be applied based on the component's state or props. Inline styles in React are written as JavaScript objects.
Each CSS property is camelCased, and the values are provided as strings. This method is particularly useful for applying styles conditionally or dynamically, as it allows you to compute styles based on component state or props.
These are the following approaches used for showing inline styles in React.js:
Steps to Create a React App
Step 1: Initialize the project from terminal using the command.
npx create-react-app my-app
Step 2: Navigate to the project folder using the command.
cd my-app
Folder Structure
Folder StructureDependencies
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
1. Using the style Attribute Directly
You can apply styles directly in the JSX by using the style attribute. This method is straightforward and useful for simple, static styles.
Syntax
const MyComponent = () => {
return <div style={{ color: 'red', fontSize: '20px' }}>Hello, World!</div>;
};
Example: In this example, the div element is styled with a red color and a font size of 20 pixels.
JavaScript
import React from 'react';
const MyComponent = () => {
return <div style={{ color: 'red', fontSize: '20px' }}>
Hello, World!</div>;
};
export default MyComponent;
Output
How To Use Inline Styles in ReactJS?2. Storing Styles in a Variable
Store the styles in a variable and then use that variable in the style attribute. This approach is useful when you have multiple elements that need the same styles or when you want to make your styles more readable.
Syntax
const MyComponent = () => {
const myStyles = {
color: 'green',
fontSize: '18px',
};
return <div style={myStyles}>Geeks For Geeks</div>;
};
Example: In this example, the styles are stored in a myStyles variable and then applied to the div element.
JavaScript
import React from 'react';
const MyComponent = () => {
const myStyles = {
color: 'green',
fontSize: '18px',
};
return <div style={myStyles}>Geeks For Geeks</div>;
};
export default MyComponent;
Output
How To Use Inline Styles in ReactJS?3. Using Inline Styles with Dynamic Properties
You can use JavaScript expressions to dynamically set styles, which is useful when styles need to change based on component props or state.
Syntax
const MyComponent = ({ fontSize }) => {
return <div style={{ color: 'purple', fontSize: `${fontSize}px` }}>Hello, World!</div>;
};
Example: In this example, the font size of the div element is set dynamically based on the fontSize prop.
JavaScript
import React from 'react';
const MyComponent = ({ fontSize }) => {
return <div style={{
color: 'purple',
fontSize: `${fontSize}px`
}}>Hello GFG</div>;
};
export default MyComponent;
Output
How To Use Inline Styles in ReactJS?
Similar Reads
How to use Inline Styles in React ?
ReactJS is a JavaScript library that is used to build a user interface. React makes it easy to create an interactive user interface by using a component-based approach. It offers various ways to apply styles to components. One of the methods is using inline styles, where styles are defined directly
3 min read
How to use styles in ReactJS ?
React is a popular JavaScript library for building single-page applications (SPAs) with dynamic user interfaces. Styling in React can be done in various ways, each with its advantages. In this article, we will explore different approaches to applying styles in React, including inline styles, CSS fil
4 min read
How to use Radium in React app for inline styling?
Radium is a popular npm package for React which allows us to use inline styling with pseudo selectors and media queries. Without this third party package, one can not do inline styling with pseudo selectors because React doesn't allow us to use pseudo-selectors in inline styling. Examples of pseudo
2 min read
How to use innerHtml in React JS ?
React.js provides a easy way of handling UI updates. However, sometimes you need to inject HTML directly into your components. PrerequisitesIntroduction to ReactReact ComponentsReact HooksNPM or NPXIn this article, we are going to learn how can we use InnerHTML in React JS. We can add HTML into Reac
2 min read
How to use React Icons in Next.js ?
Icons make the website beautiful and interactive. Icons are an important part of a website's user interfaces, which are used in expressing objects, actions, and ideas. They are used to communicate the core idea and intent of a product or action and express the object visually. Here, we will learn to
3 min read
How to Set z-index Value in React JS ?
This article explores setting the z-index attribute in React, a CSS property influencing the stacking order of HTML elements for visibility control. In React, adjusting the z-index attribute enables effective management of stacking contexts within the application. We will discuss the following two a
3 min read
How to Create List Styling in ReactJS ?
React, a declarative, efficient, and flexible JavaScript library for building user interfaces, plays a crucial role as the 'V' in MVC. Specifically, ReactJS, an open-source, component-based front-end library maintained by Facebook, focuses on the view layer of applications. In this article, we will
3 min read
Using styled API in ReactJS
Styled components in ReactJS is a CSS-in-JS library that can be used for a better UI design. This API allows us to create a styled component and apply all styling properties. For using Styled API in React we need to install styled components. Prerequisites:Basics of ReactJSStyled ComponentsApproachT
3 min read
How to use List Component in ReactJS?
Lists are continuous, vertical indexes of text or images. Material UI for React has this component available for us, and it is very easy to integrate. We can use the List Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReactJSSteps to Create the React Application And In
2 min read
How to use ReactJS with HTML Template ?
We all know ReactJS is a front-end development framework. For the front end, we need to create HTML templates. So, this article teaches you how to use HTML templates in ReactJS. The process is very simple. Prerequisites:NodeJS or NPMReact JSApproach: We will create a simple one-page HTML template i
4 min read