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. 在根目录建立样式文件修改全局样式
相关推荐
巴勒个啦1 分钟前
SCADA数据接入实战:如何处理工业协议数据并呈现给前端
前端框架
明月_清风1 分钟前
爆破前端生态!Cloudflare 收购 Vite 背后,前端开发者会迎来什么变化?
前端·vite
光影少年1 分钟前
react的useMemo 如何优化?
前端·react.js·掘金·金石计划
星栈2 分钟前
Makepad、egui、Dioxus、Tauri:Rust GUI 到底怎么选
前端·rust
ai_coder_ai2 分钟前
如何在自动化脚本中实现定时操作?
java·前端·javascript
努力早日退休3 分钟前
一个 9999px 引发的跨平台血案:小程序离屏隐藏元素的滚动兼容性问题
前端·javascript
YFF菲菲兔5 分钟前
React 核心流程总述
react.js
嘟嘟071718 分钟前
前端异步编程完全指南:从json-server到DeepSeek大模型接口调用
前端
用户0595401744618 分钟前
大模型多轮对话“失忆”踩坑实录:一次线上事故让我排查了48小时,最终靠 Playwright + Pytest 把记忆锁死
前端·css
橘子星18 分钟前
前端薅数据神器 Fetch:不用翻墙,在线拿捏后端与 AI 接口
前端·后端