样式处理 #
一、内联样式 #
1.1 基本用法 #
jsx
function StyledComponent() {
return (
<div style={{
color: 'red',
fontSize: '16px',
backgroundColor: '#f0f0f0',
padding: '20px'
}}>
Styled Content
</div>
);
}
1.2 动态样式 #
jsx
function DynamicStyle() {
const [isActive, setIsActive] = createSignal(false);
return (
<button
style={{
backgroundColor: isActive() ? 'green' : 'gray',
color: 'white',
padding: '10px 20px',
border: 'none',
borderRadius: '4px'
}}
onClick={() => setIsActive(!isActive)}
>
{isActive() ? 'Active' : 'Inactive'}
</button>
);
}
1.3 CSS 变量 #
jsx
function CSSVariables() {
const [theme, setTheme] = createSignal({
primary: '#007bff',
secondary: '#6c757d'
});
return (
<div style={{
'--primary-color': theme().primary,
'--secondary-color': theme().secondary
}}>
<button class="btn-primary">Primary</button>
</div>
);
}
二、CSS 类 #
2.1 className 属性 #
jsx
function ClassExample() {
return (
<div className="container main">
<h1 className="title">Hello</h1>
</div>
);
}
2.2 动态类名 #
jsx
function DynamicClass() {
const [isActive, setIsActive] = createSignal(false);
return (
<div className={`btn ${isActive() ? 'active' : ''}`}>
Dynamic Class
</div>
);
}
2.3 classList 属性 #
jsx
function ClassListExample() {
const [isActive, setIsActive] = createSignal(false);
const [isDisabled, setIsDisabled] = createSignal(false);
return (
<button
classList={{
btn: true,
'btn-primary': true,
active: isActive(),
disabled: isDisabled()
}}
>
Button
</button>
);
}
三、CSS Modules #
3.1 配置 #
Vite 默认支持 CSS Modules:
jsx
import styles from './Button.module.css';
function Button(props) {
return (
<button className={styles.button}>
{props.children}
</button>
);
}
3.2 CSS 文件 #
css
/* Button.module.css */
.button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
}
.button:hover {
background-color: #0056b3;
}
.primary {
background-color: #007bff;
}
.danger {
background-color: #dc3545;
}
3.3 使用多个类 #
jsx
import styles from './Button.module.css';
function Button({ variant = 'primary', children }) {
return (
<button className={`${styles.button} ${styles[variant]}`}>
{children}
</button>
);
}
四、全局样式 #
4.1 导入全局 CSS #
jsx
// index.jsx
import './global.css';
import './styles/main.css';
function App() {
return <div>...</div>;
}
4.2 :global 选择器 #
css
/* 在 CSS Module 中使用全局选择器 */
.container :global(.external-class) {
color: red;
}
五、Tailwind CSS #
5.1 安装配置 #
bash
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
5.2 配置文件 #
javascript
// tailwind.config.js
export default {
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {}
},
plugins: []
};
5.3 使用 #
jsx
function Button({ children }) {
return (
<button className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
{children}
</button>
);
}
function Card({ title, children }) {
return (
<div className="bg-white shadow-lg rounded-lg p-6">
<h2 className="text-xl font-bold mb-4">{title}</h2>
<div className="text-gray-600">{children}</div>
</div>
);
}
六、最佳实践 #
6.1 样式组织 #
text
src/
├── styles/
│ ├── global.css
│ ├── variables.css
│ └── utilities.css
├── components/
│ ├── Button/
│ │ ├── Button.jsx
│ │ └── Button.module.css
│ └── Card/
│ ├── Card.jsx
│ └── Card.module.css
6.2 主题管理 #
jsx
// contexts/ThemeContext.jsx
const themes = {
light: {
background: '#ffffff',
text: '#000000',
primary: '#007bff'
},
dark: {
background: '#1a1a1a',
text: '#ffffff',
primary: '#4da3ff'
}
};
export function ThemeProvider(props) {
const [themeName, setThemeName] = createSignal('light');
const theme = createMemo(() => themes[themeName()]);
return (
<ThemeContext.Provider value={{ theme, themeName, setThemeName }}>
<div
style={{
'background-color': theme().background,
'color': theme().text
}}
>
{props.children}
</div>
</ThemeContext.Provider>
);
}
七、总结 #
7.1 样式方案对比 #
| 方案 | 优点 | 缺点 |
|---|---|---|
| 内联样式 | 动态、作用域 | 难维护、无伪类 |
| CSS 类 | 简单、可复用 | 全局污染 |
| CSS Modules | 作用域、可维护 | 需要构建 |
| Tailwind | 快速开发、一致性好 | 类名长 |
7.2 推荐方案 #
- 小型项目:CSS Modules
- 快速原型:Tailwind CSS
- 动态样式:内联样式 + CSS 变量
- 组件库:CSS Modules + CSS 变量
最后更新:2026-03-28