create-aelf-dapp
Version:
Create aelf Dapp based on Next.js with one command
148 lines (118 loc) • 3.25 kB
Markdown
# Suggest Practice
Here are some best practices for using Tailwind CSS
to improve your CSS development efficiency
and maintain consistent and maintainable styles.
## 1. Use Tailwind CLI and JIT Mode [Template Default]
Using JIT (Just-In-Time) Mode significantly reduces the size of the generated CSS file
and improves the development experience.
```javascript
module.exports = {
mode: 'jit',
purge: ['./src/**/*.{js,ts,jsx,tsx}', './public/index.html'],
theme: {
extend: {},
},
plugins: [],
}
```
## 2. Organize CSS Classes
Tailwind provides an atomic way to write CSS,
but organizing class names properly can improve readability and maintainability:
- Group by Category: Group similar classes together,
such as layout, spacing, color, etc.
```html
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Button
</button>
```
- Use @apply: In some cases, you can use @apply in your CSS to reuse common styles.
```css
/* styles.css */
.btn {
@apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}
```
## 3. Custom Configuration
- Custom Theme: Extend or override the default theme configuration in tailwind.config.js.
```javascript
module.exports = {
theme: {
extend: {
colors: {
primary: '#1a202c',
secondary: '#2d3748',
},
spacing: {
'72': '18rem',
'84': '21rem',
'96': '24rem',
},
},
},
}
```
## 4. Use Plugins
Tailwind offers many official and community plugins to extend its functionality. For example:
- Tailwind Forms: A plugin to improve form styles.
```bash
npm install @tailwindcss/forms
```
```javascript
// tailwind.config.js
module.exports = {
plugins: [
require('@tailwindcss/forms'),
],
}
```
## 5. Responsive Design
Tailwind supports responsive design.
Use built-in responsive modifiers to handle styles for different screen sizes:
```html
<div class="text-center md:text-left lg:text-right">
Responsive Text Alignment
</div>
```
## 6. Use Tools and Extensions [Template Default]
Prettier Plugin: Use the Prettier plugin to format Tailwind CSS classes.
```bash
npm install -D prettier prettier-plugin-tailwindcss
```
Configure the plugin in .prettierrc file:
```json
{
"plugins": ["prettier-plugin-tailwindcss"]
}
```
## 7. Minimize CSS in Production [Template Default]
https://tailwindcss.com/docs/optimizing-for-production
```javascript
// if process.env.NODE_ENV === 'production'
// postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
...(process.env.NODE_ENV === 'production' ? { cssnano: {} } : {})
}
}
```
## 8. Use Composite Classes and Components
For commonly used style combinations, use composite classes or components:
```html
<!-- In HTML -->
<div class="card p-4 bg-white shadow-lg rounded-lg">
<h2 class="card-title text-2xl font-bold">Card Title</h2>
<p class="card-content">This is a card content.</p>
</div>
<!-- Or use components -->
<!-- MyComponent.js -->
export default function MyComponent() {
return (
<div className="card">
<h2 className="card-title">Card Title</h2>
<p className="card-content">This is a card content.</p>
</div>
);
}
```