Components & Import/Export
Read This on Github
⭐ How to use css in ReactJS
Simple Follow the Steps
- First, create a CSS file with a name like "app.css" in the same directory as your React component file(s).
- In the CSS file, write your styles for the components.
- In your React component file(s), import the CSS file at the top using
import './app.css'. - In your JSX code, use the
classNameattribute to apply the styles to your components. - The value of
classNameshould match the class selector in your CSS file.
Example:
.title {
font-size: 24px;
color: blue;
}
import React from 'react';
import './app.css';
function App() {
return (
<div>
<h1 className="title">Hello World!</h1>
</div>
);
}
export default App;In this example, the title class from the CSS file is applied to the h1 element in the React component through the className attribute.
⭐ Experiment 1
When you apply a CSS file to a React component, it will only affect that component and its child components. The styles will not affect other components in your application unless you specifically import and apply the CSS file to those components as well.
- You can see you have app.css file in the same directory as your React component file(s).
- In the CSS file, write your styles for the components.
.header {
text-align: center;
background-color: #644ec2;
color: white;
}
.footer {
text-align: center;
background-color: #c59a58;
color: white;
}- Now inside your
srcfolder, you can create a new file namedfooter.jsand write the following code.
const Footer = () => {
return (
<div>
<h1 className="footer">Footer</h1>
</div>
)
}
export default Footer;- Now inside your
srcfolder, you can create a new file namedheader.jsand write the following code.
import Footer from "./footer";
const header = ()=>{
return(
<div>
<h1>Header</h1>
<Footer/>
</div>
)
}
export default header;- Now inside your
app.jsfile, just import theheader.jsfile and inside the className just use theheaderclass from the CSS file is applied to theh1element in the React component through theclassNameattribute.
import logo from './logo.svg';
import './App.css';
import Header from './component/header';
function App() {
return (
<>
<div className="header">
<header/></div>
</>
);
}
export default App;- Now you can notice that the
headerclass from the CSS file is applied to theh1element in the React component through theclassNameattribute also thefooterclass from the CSS file is applied to the footer component through theclassNameattribute.
So the css file is applied to the React component and its child components here parent component is app.js and child component is header.js and footer.js.
⚡ Playground
⭐ Experiment 2
Css styles specific to a component in the same file as the component, to make your code more modular and easier to maintain.
- Now just make a new file named
header.cssinside thesrc>componentfolder and write the following code.
.header {
background-color: bisque;
color: #644ec2;
}- Now inside your
header.jsfile, just import theheader.cssfile and inside the className just use theheaderclass from the CSS file is applied to theh1element in the React component through theclassNameattribute.
import Footer from "./Footer";
import "./Header.css"
const Header = ()=>{
return(
<div>
<h1 className="header">Header</h1>
<Footer/>
</div>
)
}
export default Header;-
Now you can notice that the
headerclass from the CSS file is applied to theh1element in the React component through theclassNameattribute.So this is how you can apply css styles specific to a component in the same file as the component, to make your code more modular and easier to maintain.
⚡ Playground
ℹ️You can also use id instead of class in the CSS file.
header.css.header { background-color: bisque; color: #644ec2; } #xam{ color: #ff038d; }header.jsimport Footer from "./Footer"; import "./Header.css" const Header = ()=>{ return( <div> <h1 className="header">Header</h1> <p id ="xam">hey what's up </p> <Footer/> </div> ) } export default Header;
⭐ Experiment 2
Let's try to use inline CSS
Simply just add the style attribute to the element you want to add CSS to, and set the value to an object with a camelCased version of each CSS property.
import Footer from "./Footer";
import "./Header.css"
const Header = ()=>{
return(
<div>
<h1 className="header">Header</h1>
<p id ="xam">hey what's up </p>
<p style={{backgroundColor:"#915454" , color:"#6592a3"}}>My name is xam</p>
<Footer/>
</div>
)
}
export default Header;