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. 在根目录建立样式文件修改全局样式
相关推荐
Asize8 分钟前
重生之我在 Vibe Coding 时代当程序员:第十五课,正则表达式和 HTTP 请求:规则不是背出来的,是拆出来的
前端·javascript·后端
Mintopia10 分钟前
从意图到评估:理解用户操作产品的完整行动链路
前端
竹林81812 分钟前
从报错到跑通:我用 @solana/web3.js 在 React 中实现 Solana 钱包连接的全过程
前端
Asize13 分钟前
重生之我在 Vibe Coding 时代当程序员:第十六课,从模拟队列到原型链
前端·javascript·后端
vim怎么退出15 分钟前
Dive into React——高级特性
前端·react.js·源码阅读
如果超人不会飞16 分钟前
TinyVue Container 组件完全指南:五种版型撑起你的"应用骨架"
前端·vue.js
冰暮流星27 分钟前
javascript之this关键字
开发语言·前端·javascript
余大大.30 分钟前
SystemVerilog-参数宏与拼接符的使用
前端
羸弱的穷酸书生33 分钟前
跟AI学一手之前端导出
前端·文件导出