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. 在根目录建立样式文件修改全局样式
相关推荐
用户老李15 小时前
相机模型与标定自学教程 02:外参 \[R | t] 与完整投影链路
前端
用户老李15 小时前
无线电自学教程 03:电路基础——从欧姆定律到 LC 谐振
前端
申君健248641827720215 小时前
ESM 源码发布 + 显式子路径导出
前端
大龄秃头程序员15 小时前
实践 iOS Touch 响应链:从 sendEvent 到 nextResponder 的完整闭环
前端
用户老李15 小时前
YOLO 目标检测自学教程 05:动手——从零实现 mini-YOLOv1
前端
用户老李15 小时前
视频编码自学教程 02:颜色与色彩空间——从 RGB 到 Y'CbCr 4:2:0
前端
用户老李15 小时前
无线电自学教程 02:数学工具箱——正弦、复数、相量与分贝
前端
lpp1915 小时前
一个网页的完整一生:从敲网址到页面显示,中间发生了什么
前端
春涧草茶17 小时前
慢就是快16-1协程
开发语言·前端·python
Oiiouui17 小时前
linux服务器部署Logto后端与对应前端的实现
linux·服务器·前端·后端