react中修改组件样式的几种方法

  1. 使用自定义类名className,引入样式文件进行样式覆盖
java 复制代码
import React from 'react';
import { Button } from 'antd';

const MyComponent = () => {
  return (
    <Button className="custom-button">点击我</Button>
  );
};

export default MyComponent;
css 复制代码
.custom-button {
  background-color: blue;
  color: white;
}
  1. 使用内联样式
javascript 复制代码
import React from 'react';
import { Button } from 'antd';

const MyComponent = () => {
  return (
    <Button style={{ backgroundColor: 'blue', color: 'white' }}>点击我</Button>
  );
};

export default MyComponent;
  1. 使用css module 中的 :global 进行全局样式覆盖
css 复制代码
// test.module.css

.container {
  padding: 20px;
  background-color: lightgray;
}

/* 全局样式 */
:global(body) {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
}
  1. 使用styled-components 编写样式化的组件
javascript 复制代码
import React from 'react';
import { Button } from 'antd';
import styled from 'styled-components';

const StyledButton = styled(Button)`
  background-color: blue;
  color: white;
`;

const MyComponent = () => {
  return (
    <StyledButton>点击我</StyledButton>
  );
};

export default MyComponent;
  1. 在根目录建立样式文件修改全局样式
相关推荐
我的写法有点潮19 小时前
JS中对象是怎么运算的呢
前端·javascript·面试
悠哉摸鱼大王19 小时前
NV12 转 RGB 完整指南
前端·javascript
一壶纱19 小时前
UniApp + Pinia 数据持久化
前端·数据库·uni-app
双向3319 小时前
【RAG+LLM实战指南】如何用检索增强生成破解AI幻觉难题?
前端
海云前端119 小时前
前端人必懂的浏览器指纹:不止是技术,更是求职加分项
前端
青莲84319 小时前
Java内存模型(JMM)与JVM内存区域完整详解
android·前端·面试
parade岁月19 小时前
把 Git 提交变成“可执行规范”:Commit 规范体系与 Husky/Commitlint/Commitizen/Lint-staged 全链路介绍
前端·代码规范
青莲84319 小时前
Java内存回收机制(GC)完整详解
java·前端·面试
pas13619 小时前
29-mini-vue element搭建更新
前端·javascript·vue.js
IT=>小脑虎19 小时前
2026版 React 零基础小白进阶知识点【衔接基础·企业级实战】
前端·react.js·前端框架