Selected topic
Styling In React
Prefer practical output? Use related tools below while reading.
In traditional CSS, styles are global and can cause naming conflicts. To avoid this, React introduced CSS Modules, a way to scope styles to individual components, making it easier to manage and reuse code.
Header component with its own styles in a separate file (Header.module.css):css
/ src/components/Header/Header.module.css /
.header-container {
display: flex;
justify-content: space-between;
}.logo {
width: 20px;
}
In our React component, we can import the styles using ES6 syntax:
jsx
// src/components/Header.js
import React from 'react';
import styles from './Header.module.css';const Header = () => {
return (
<div className={styles.headerContainer}>
<img src="logo.png" alt="Logo" className={styles.logo} />
<nav>
<ul>
{/ Navigation items /}
</ul>
</nav>
</div>
);
};
export default Header;
Component.module.css).