Welcome back to CodeYourCraft! Today, we're diving into the world of CSS Stylesheets in React JS. By the end of this lesson, you'll have a solid understanding of how to style your React components.
In a React application, you can style your components using JavaScript, CSS, or a CSS-in-JS library like Styled Components or Emotion. Today, we'll focus on using external CSS files, as it's a great way to organize and reuse styles across your application.
To link an external CSS file in your React project, follow these steps:
src/styles folder. Let's call it App.css.src folder, create a new folder called public.public folder, create a new folder called css.App.css) into the public/css folder.src folder, open the index.js file and import your CSS file at the top:import React from 'react';
import ReactDOM from 'react-dom';
import './styles/App.css'; // <-- Import your CSS file here!
// ... rest of your codeNow let's style a simple React component using our external CSS file:
Title:import React from 'react';
const Title = () => {
return (
<h1>Welcome to CodeYourCraft</h1>
);
};
export default Title;App.css file, add the following styles:h1 {
font-family: Arial, sans-serif;
color: #333;
text-align: center;
}App component to use the Title component:import React from 'react';
import ReactDOM from 'react-dom';
import Title from './Title'; // <-- Import your new component here!
import './styles/App.css'; // <-- Import your CSS file here!
const App = () => {
return (
<div>
<Title />
</div>
);
};
export default App;š Remember, for components within components, use the className prop instead of the class prop.
What should you use instead of the `class` prop when styling a child component in React?
That's it for today! In the next lesson, we'll dive deeper into CSS Stylesheets in React, exploring advanced techniques and practical examples. See you then! š
Happy coding! š¤š»š