JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Applying global CSS styles in React applications

Author:迹忆客 Last Updated:2025/03/03 Views:

To apply global CSS styles in your React application, write the CSS in a file with .css extension and import it in your index.js file.

The global CSS should be imported in index.js to ensure it is loaded on all pages of your React application.

Below is an example of an index.css file that declares 2 globally available classes.

.bg-salmon {
  background-color: salmon;
}

.text-white {
  color: white;
}

Here is how we can import the index.css file in the index.js file .

// 👇️ import css
import './index.css';

import {createRoot} from 'react-dom/client';
import App from './App';

const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

root.render(
  <App />
);

The above example assumes that our index.css file is in the same directory as our index.js file.

When importing global CSS files in React, the best practice is to import the CSS file into the index.js file.

The index.js file is the entry point for your React application, so it always runs.

On the other hand, if we import a CSS file into a component, the CSS styles may be removed once our component unmounts.

We can apply global styles in any file.

Here is an example App component that uses these two classes.

const App = () => {
  return (
    <div>
      <div className="bg-salmon text-white">jiyik.com</div>
    </div>
  );
};

export default App;

Applying global CSS styles in React applications


Applying global CSS styles in React applications using CSS Modules

If you're using CSS Modules to style your React app, you can :globalswitch to the global scope by passing the current selector to .

:global(.bg-salmon) {
  background-color: salmon;
}

If a selector is switched to global mode, it will not be modified by CSS Modules and will be available globally.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Passing events and parameters to onClick in React

Publish Date:2025/03/03 Views:63 Category:React

在 React 中传递事件和参数 onClick: 将内联函数传递给元素的 onClick 属性。 该函数应获取事件对象并调用 handleClick 。 将事件和参数传递给 handleClick 。 const App = () = { const handleClick = ( e

How to remove event listeners in React

Publish Date:2025/03/03 Views:194 Category:React

在 React 中删除事件监听器: 在 useEffect 挂钩中添加事件侦听器。 从 useEffect 挂钩返回一个函数。 当组件卸载时,使用 removeEventListener 方法移除事件监听器。 import {useRef, useEffect} from r

Using conditions to jump out of a map in map() in React

Publish Date:2025/03/03 Views:147 Category:React

React 中在 map() 中使用条件: 在数组上调用 map() 方法。 使用 if 条件,如果条件满足则显式返回。 否则返回不同的值或返回 null 以不呈现任何内容。 export default function App () { const arr =

Calling multiple onClick functions in React

Publish Date:2025/03/03 Views:105 Category:React

在 React 中调用多个 onClick 函数: 在元素上设置 onClick 属性。 在事件处理函数中调用其他函数。 事件处理函数可以根据需要调用尽可能多的其他函数。 export default function App () { const s

Find all elements by class name in React

Publish Date:2025/03/03 Views:154 Category:React

在 React 中通过 className 查找所有元素: 使用 getElementsByClassName 方法获取具有特定类的所有元素。 将对该方法的调用放在 useEffect() 钩子中。 import {useEffect} from react ; const App = () = { useEf

Check if an element has focus in React

Publish Date:2025/03/03 Views:95 Category:React

要检查元素是否在 React 中获得焦点: 在元素上设置 ref 属性。 元素呈现后,检查元素是否是文档中的活动元素。 如果是,则该元素被聚焦。 import {useEffect, useRef} from react ; export defaul

Looping through an array of objects in React

Publish Date:2025/03/03 Views:64 Category:React

在 React 中循环对象数组: 使用 map() 方法迭代数组。 我们传递给 map() 的函数会为数组中的每个元素调用。 该方法返回一个新数组,其中包含传入函数的结果。 export default function App (

Get the class name of an element in React

Publish Date:2025/03/03 Views:65 Category:React

在 React 中使用 event.target 获取元素的类名 获取元素的类名: 将元素上的 onClick 属性设置为事件处理函数。 访问元素的类名作为 event.currentTarget.className 。 export default function App () { cons

How to add key attribute to React fragment

Publish Date:2025/03/03 Views:137 Category:React

使用更详细的片段语法将 key 属性添加到 React 片段,例如 React.Fragment key={key} 。 更冗长的语法实现了相同的结果对元素列表进行分组,而不向 DOM 添加额外的节点。 import React from react

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial