Hey there, developers! Welcome to this guide. Inline CSS can be super handy in certain situations, especially when you need to make quick design tweaks without dealing with separate files. But did you know that you can use external CSS files as well as inline styles in a React project? Don't worry, I'll walk you through it in this article.
I'll reveal the top 5 amazing techniques for using inline CSS in your React.js projects.
The way of the inline style:
- Using Inline Styles with the style Attribute:
- Using Inline Styles with object variable:
- Applying Inline Styles Conditionally Based on State or Props:
- Extending Inline Styles with Pseudo-selectors and Media Queries using the Radium tool:
- Utilizing CSS Modules for Scoped Styling:
1. Using Inline Styles with the style Attribute:
- Inline styles in JSX are written as JavaScript objects within double curly braces ({{ }}).
- CSS property names are written in camelCase instead of kebab-case.
- Property values are provided as strings.
import React from 'react';
function App() {
return (
<div style={{ height: '100vh', display: 'grid', placeItems: 'center', }}>
<h1 style={{ color: 'red' }}>Hello, World!</h1>
</div>
);
}
export default App;
2. Using Inline Styles with object variable:
import React from 'react';
function App() {
const styleDiv = {
height: '100vh',
display: 'grid',
placeItems: 'center'
}
const styleHeading = {
color: 'steelblue'
}
return (
<div style={styleDiv}>
<h1 style={styleHeading}>Hello, World!</h1>
</div>
);
}
export default App;
3. Applying Inline Styles Conditionally Based:
import React from 'react';
function App({ isSpecial }) {
return (
<div>
<h1 style={{
color: isSpecial ? 'red' : 'green',
fontSize: isSpecial ? '24px' : '20px',
fontWeight: isSpecial ? 'normal' : 'bold'
}}>Hello, World!</h1>
</div>
);
}
export default App;
4. Extending Inline Styles with Pseudo-selectors and Media Queries using the Radium tool:
npm i radium
// If installation fails, try this command 👇
npm install--save radium--force
import React from 'react';
import Radium from 'radium';
const styles = {
parentEl: {
height: '100vh',
display: "grid",
placeItems: "center",
},
childEl: {
color: "red",
transition: 'color 0.3s', // Adding transition for smooth color change
':hover': { // Applying hover effect
color: 'green',
}
}
}
function App() {
return (
<div style={styles.parentEl}>
<h1 style={styles.childEl}>Hello, World!</h1>
</div>
);
};
export default Radium(App);
5. Utilizing CSS Modules for Scoped Styling:
// styles.module.css file
.myClass {
color: red;
font - size: 20px;
}
// App.js file
import React from 'react';
import styles from './styles.module.css';
function App() {
return (
<div className={styles.myClass}>
Hello, World!
</div>
);
}
export default App;
This way you can write inline CSS within your React component. In this way, we can write CSS styles separately from the component logic.
Conclusion
To become a developer like a senior, you need to know such tricks. We've told you everything from this article to adjusting styles based on state or props with the style attribute and even extending inline styles with pseudo selectors using tools like Radium. Start practicing now to enhance your skills.
For bigger projects, it's best to use a separate CSS file rather than just inline styles. But still, if you prefer using inline styles, even for bigger projects, then you can use Tailwind CSS, which is easy to use even for large-scale projects.
If you have any questions regarding this article or any other web development, you can ask them in the question box given below, and you will get an answer as soon as possible.