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. 在根目录建立样式文件修改全局样式
相关推荐
一念之间lq6 分钟前
Elpis 第四阶段· Vue3 完成动态组件建设
前端·vue.js
akira09126 分钟前
滚动控制视频播放是如何实现的?GSAP ScrollTrigger + seek 实践 vivo官网案例
前端·产品
用户636836608559 分钟前
前端使用nuxt.js的seo优化
前端
OldBirds9 分钟前
烧脑时刻:Dart 中异步生成器与流
前端·后端
湛海不过深蓝11 分钟前
【echarts】折线图颜色分段设置不同颜色
前端·javascript·echarts
昨晚我输给了一辆AE8612 分钟前
关于 react-hook-form 的 isValid 在有些场景下的值总是 false 问题
前端·react.js
xinyu_Jina18 分钟前
Calculator Game:WebAssembly在计算密集型组合优化中的性能优势
前端·ui·性能优化
JustHappy21 分钟前
「2025年终个人总结」🤬🤬回答我!你个菜鸟程序员这一年发生了啥?
前端
啃火龙果的兔子33 分钟前
可以指定端口启动本地前端的npm包
前端·npm·node.js
new code Boy42 分钟前
前端base-64 编码解码
前端·javascript·html