mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 14:52:55 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+416
-52
@@ -1,65 +1,429 @@
|
||||
---
|
||||
description: This rule provides best practices for CSS development, covering code organization, performance, security, testing, and common pitfalls. It aims to ensure maintainable, scalable, and efficient CSS code.
|
||||
globs: *.css
|
||||
description: Definitive guidelines for writing maintainable, performant, and accessible CSS. Focus on modern practices, robust architecture, and efficient styling patterns.
|
||||
globs: **/*
|
||||
---
|
||||
- **Use a CSS Preprocessor**: Leverage preprocessors like Sass, Less, or Stylus for features like variables, nesting, mixins, and functions to enhance code organization and maintainability.
|
||||
# CSS Best Practices
|
||||
|
||||
- **Code Organization and Structure**:
|
||||
- **Directory Structure**: Organize CSS files into logical directories based on functionality (e.g., `components`, `modules`, `pages`, `themes`).
|
||||
- **File Naming Conventions**: Use meaningful and consistent file names (e.g., `button.css`, `header.module.css`). Consider using a naming convention like BEM (Block, Element, Modifier) or SUIT CSS.
|
||||
- **Module Organization**: Break down large stylesheets into smaller, reusable modules.
|
||||
- **Component Architecture**: Structure CSS based on UI components, ensuring each component has its dedicated stylesheet.
|
||||
- **Code Splitting**: Split CSS into critical (above-the-fold) and non-critical CSS for improved initial page load time. Utilize tools like `Critical CSS` or `PurgeCSS`.
|
||||
This guide outlines the definitive best practices for writing CSS within our team. Adhering to these principles ensures our stylesheets are scalable, maintainable, performant, and accessible.
|
||||
|
||||
- **Formatting and Style**:
|
||||
- **Indentation**: Use soft-tabs with two spaces for indentation.
|
||||
- **Trailing Whitespace**: Avoid trailing whitespace.
|
||||
- **Declaration Order**: Maintain a consistent declaration order (e.g., alphabetical, grouping related properties).
|
||||
- **Property Units**: Use appropriate units (e.g., `rem` or `em` for font sizes, `vw` or `%` for responsive layouts).
|
||||
- **Quotation**: Use double quotes for attribute values in selectors.
|
||||
- **Line Length**: Keep lines reasonably short (e.g., under 80-100 characters) for readability.
|
||||
## 1. Code Organization & Structure
|
||||
|
||||
- **Common Patterns and Anti-patterns**:
|
||||
- **BEM (Block, Element, Modifier)**: Use BEM or similar methodologies for clear and maintainable class naming.
|
||||
- **Object-Oriented CSS (OOCSS)**: Apply OOCSS principles to create reusable and scalable styles.
|
||||
- **Avoid !important**: Minimize the use of `!important` to prevent specificity conflicts.
|
||||
- **Avoid Inline Styles**: Prefer external stylesheets or embedded styles over inline styles for better maintainability and separation of concerns.
|
||||
- **Don't use IDs for styling**: Avoid using IDs for styling since they are too specific and make styles harder to override.
|
||||
Adopt a modular, predictable structure. Use Sass for pre-processing.
|
||||
|
||||
- **Understanding CSS Specificity**: Master CSS specificity rules to avoid unexpected style conflicts. Use specific selectors sparingly.
|
||||
### 1.1 File Structure (Modular & Scalable)
|
||||
|
||||
- **Use Flexible/Relative Units**: Employ relative units like `em`, `rem`, and `vw` for creating responsive designs that adapt to different screen sizes.
|
||||
Organize CSS into logical, focused files. A simplified ITCSS-like structure is recommended.
|
||||
|
||||
- **Performance Considerations**:
|
||||
- **Optimize Selectors**: Use efficient CSS selectors to minimize rendering time. Avoid overly complex selectors.
|
||||
- **Minify CSS**: Minify CSS files to reduce file size and improve loading times.
|
||||
- **Compress CSS**: Use Gzip or Brotli compression on the server to further reduce CSS file sizes.
|
||||
- **Browser Caching**: Leverage browser caching to store CSS files locally, reducing server load and improving performance.
|
||||
- **Avoid Expensive Properties**: Avoid CSS properties that are computationally expensive (e.g., `filter`, `box-shadow` with large blur radii) when possible.
|
||||
```css
|
||||
/*
|
||||
css/
|
||||
├── base/ // Project-wide defaults (reset, typography, variables)
|
||||
│ ├── _reset.scss
|
||||
│ ├── _typography.scss
|
||||
│ └── _variables.scss // Design tokens
|
||||
├── components/ // Self-contained UI components (buttons, cards)
|
||||
│ ├── _button.scss
|
||||
│ └── _card.scss
|
||||
├── layout/ // Major page sections (header, footer, grid)
|
||||
│ ├── _grid.scss
|
||||
│ └── _header.scss
|
||||
└── utilities/ // Single-purpose helper classes (spacing, text alignment)
|
||||
├── _spacing.scss
|
||||
└── _helpers.scss
|
||||
*/
|
||||
|
||||
- **Security Best Practices**:
|
||||
- **Sanitize User Input**: When using CSS variables based on user input, sanitize the input to prevent CSS injection attacks.
|
||||
- **Content Security Policy (CSP)**: Implement a CSP to control the sources from which CSS can be loaded.
|
||||
// main.scss
|
||||
@import 'base/reset';
|
||||
@import 'base/variables';
|
||||
@import 'base/typography';
|
||||
|
||||
- **Testing Approaches**:
|
||||
- **Unit Testing**: Use tools like `CSS Modules` or `styled-components` to write unit tests for individual CSS components.
|
||||
- **Visual Regression Testing**: Implement visual regression testing to detect unexpected changes in CSS styles.
|
||||
- **Linting**: Use a CSS linter (e.g., `stylelint`) to enforce coding standards and catch potential errors.
|
||||
@import 'layout/grid';
|
||||
@import 'layout/header';
|
||||
|
||||
- **Common Pitfalls and Gotchas**:
|
||||
- **Specificity Conflicts**: Be aware of specificity issues and use tools to visualize and manage CSS specificity.
|
||||
- **Browser Compatibility**: Test CSS across different browsers and versions to ensure compatibility.
|
||||
- **Vendor Prefixes**: Use vendor prefixes when necessary but consider using tools like `autoprefixer` to automate the process.
|
||||
@import 'components/button';
|
||||
@import 'components/card';
|
||||
|
||||
- **Tooling and Environment**:
|
||||
- **CSS Linters**: Use `stylelint` to enforce coding standards and identify potential errors.
|
||||
- **CSS Formatters**: Employ tools like `Prettier` to automatically format CSS code consistently.
|
||||
- **Build Tools**: Integrate CSS compilation, minification, and optimization into your build process using tools like `Webpack`, `Parcel`, or `Gulp`.
|
||||
- **CSS Modules**: Use CSS Modules to scope CSS classes locally and avoid naming conflicts.
|
||||
@import 'utilities/spacing';
|
||||
@import 'utilities/helpers';
|
||||
```
|
||||
|
||||
- **Additional Tips**:
|
||||
- **Document CSS**: Add comments to explain complex or non-obvious CSS rules.
|
||||
- **Keep CSS DRY (Don't Repeat Yourself)**: Reuse CSS code as much as possible using variables, mixins, and inheritance.
|
||||
- **Regularly Review and Refactor**: Regularly review and refactor CSS code to maintain its quality and prevent code bloat.
|
||||
- **Consider Accessibility**: Ensure CSS styles contribute to website accessibility (e.g., sufficient color contrast, proper use of semantic HTML).
|
||||
- **Use CSS Variables (Custom Properties)**: Use CSS variables for theming and to manage values across your CSS.
|
||||
### 1.2 Naming Convention (BEM)
|
||||
|
||||
Use BEM (Block-Element-Modifier) for clear, flat, and highly readable selectors. This prevents specificity wars and improves reusability.
|
||||
|
||||
❌ BAD: Deeply nested, ambiguous selectors
|
||||
```css
|
||||
.header .nav ul li a { /* ... */ }
|
||||
.card.featured { /* ... */ }
|
||||
```
|
||||
|
||||
✅ GOOD: BEM for clarity and reusability
|
||||
```css
|
||||
/* Block */
|
||||
.card { /* ... */ }
|
||||
|
||||
/* Element */
|
||||
.card__title { /* ... */ }
|
||||
.card__image { /* ... */ }
|
||||
|
||||
/* Modifier */
|
||||
.card--featured { /* ... */ }
|
||||
.card__title--large { /* ... */ }
|
||||
```
|
||||
|
||||
### 1.3 CSS Custom Properties (Design Tokens)
|
||||
|
||||
Centralize design values (colors, fonts, spacing) using CSS variables. This enables consistency, easy theming, and dynamic updates.
|
||||
|
||||
```css
|
||||
/* _variables.scss */
|
||||
:root {
|
||||
--color-primary: #007bff;
|
||||
--color-text: #333;
|
||||
--font-family-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto;
|
||||
--spacing-md: 1rem;
|
||||
}
|
||||
|
||||
/* Usage */
|
||||
.button {
|
||||
background-color: var(--color-primary);
|
||||
color: white;
|
||||
padding: var(--spacing-md);
|
||||
font-family: var(--font-family-sans);
|
||||
}
|
||||
```
|
||||
|
||||
### 1.4 Minimal Nesting (Sass)
|
||||
|
||||
Avoid excessive nesting in Sass; it leads to overly specific and hard-to-override CSS. Nest only when necessary for context or specificity (e.g., pseudo-states).
|
||||
|
||||
❌ BAD: Over-nested selectors
|
||||
```scss
|
||||
.nav {
|
||||
ul {
|
||||
li {
|
||||
a {
|
||||
color: var(--color-text);
|
||||
&:hover {
|
||||
color: var(--color-primary);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD: Flat structure, nest only for direct context
|
||||
```scss
|
||||
.nav {
|
||||
&__list { /* ... */ }
|
||||
&__item { /* ... */ }
|
||||
&__link {
|
||||
color: var(--color-text);
|
||||
&:hover {
|
||||
color: var(--color-primary);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Layout & Responsive Design
|
||||
|
||||
Prioritize mobile-first design using modern layout tools and relative units.
|
||||
|
||||
### 2.1 Mobile-First Approach
|
||||
|
||||
Design for small screens first, then progressively enhance for larger viewports using media queries.
|
||||
|
||||
```css
|
||||
/* Mobile styles first */
|
||||
.container {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
/* Larger screens (e.g., tablet and up) */
|
||||
@media (min-width: 768px) {
|
||||
.container {
|
||||
padding: 2rem;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2.2 CSS Grid for 2D Layouts, Flexbox for 1D Layouts
|
||||
|
||||
Use the right tool for the job.
|
||||
|
||||
✅ GOOD: Flexbox for aligning items in a single dimension (row or column)
|
||||
```css
|
||||
.button-group {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: center; /* Horizontally center items */
|
||||
align-items: center; /* Vertically center items */
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD: CSS Grid for complex two-dimensional page layouts
|
||||
```css
|
||||
.page-layout {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr; /* Single column on mobile */
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
@media (min-width: 992px) {
|
||||
.page-layout {
|
||||
grid-template-columns: 250px 1fr; /* Sidebar + main content on desktop */
|
||||
grid-template-areas: "sidebar main";
|
||||
}
|
||||
.page-layout__sidebar { grid-area: sidebar; }
|
||||
.page-layout__main { grid-area: main; }
|
||||
}
|
||||
```
|
||||
|
||||
### 2.3 Relative Units
|
||||
|
||||
Use relative units (`rem`, `em`, `vw`, `vh`, `%`) for flexible, scalable, and accessible designs. Avoid fixed `px` values for spacing and typography.
|
||||
|
||||
❌ BAD: Fixed `px` values
|
||||
```css
|
||||
.text { font-size: 16px; margin-bottom: 20px; }
|
||||
.hero { height: 400px; }
|
||||
```
|
||||
|
||||
✅ GOOD: Relative units
|
||||
```css
|
||||
.text { font-size: 1rem; margin-bottom: 1.25rem; } /* Based on root font-size */
|
||||
.hero { min-height: 70vh; } /* Viewport height */
|
||||
.image { max-width: 100%; height: auto; } /* Fluid images */
|
||||
```
|
||||
|
||||
### 2.4 Container Queries
|
||||
|
||||
Leverage `@container` queries for component-level responsiveness, allowing components to adapt based on their parent container's size, not just the viewport.
|
||||
|
||||
```css
|
||||
.card-container {
|
||||
container-type: inline-size;
|
||||
container-name: card-scope;
|
||||
}
|
||||
|
||||
.card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
@container card-scope (min-width: 400px) {
|
||||
.card {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
}
|
||||
.card__image {
|
||||
width: 150px;
|
||||
height: auto;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
Optimize CSS for fast loading and efficient rendering.
|
||||
|
||||
### 3.1 Efficient Selectors
|
||||
|
||||
Browsers read selectors right-to-left. Keep selectors short and avoid overly specific or universal selectors where possible.
|
||||
|
||||
❌ BAD: Overly specific, slow selector
|
||||
```css
|
||||
main div p.title { /* ... */ }
|
||||
* { box-sizing: border-box; } /* Global, but expensive if not reset */
|
||||
```
|
||||
|
||||
✅ GOOD: Short, class-based selectors
|
||||
```css
|
||||
.title { /* ... */ }
|
||||
.button { /* ... */ }
|
||||
```
|
||||
|
||||
### 3.2 Minimize Reflows & Repaints
|
||||
|
||||
Animate `transform` and `opacity` properties for smooth animations, as they can be hardware-accelerated. Avoid animating properties that trigger layout changes (e.g., `width`, `height`, `margin`, `padding`).
|
||||
|
||||
❌ BAD: Animating layout properties
|
||||
```css
|
||||
.modal {
|
||||
transition: width 0.3s ease, height 0.3s ease;
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD: Animating `transform` and `opacity`
|
||||
```css
|
||||
.modal {
|
||||
transition: transform 0.3s ease, opacity 0.3s ease;
|
||||
transform: scale(0.9);
|
||||
opacity: 0;
|
||||
}
|
||||
.modal.is-open {
|
||||
transform: scale(1);
|
||||
opacity: 1;
|
||||
}
|
||||
```
|
||||
|
||||
### 3.3 Font Optimization
|
||||
|
||||
Use `font-display: swap` for web fonts to prevent invisible text during font loading (FOIT).
|
||||
|
||||
```css
|
||||
@font-face {
|
||||
font-family: 'CustomFont';
|
||||
src: url('CustomFont.woff2') format('woff2');
|
||||
font-weight: 400;
|
||||
font-display: swap; /* Crucial for performance */
|
||||
}
|
||||
```
|
||||
|
||||
## 4. Accessibility
|
||||
|
||||
Bake accessibility into every styling decision.
|
||||
|
||||
### 4.1 Color Contrast
|
||||
|
||||
Ensure text and interactive elements meet WCAG AA contrast ratios (4.5:1 for normal text, 3:1 for large text/UI components). Use tools to check.
|
||||
|
||||
### 4.2 Visible Focus Indicators
|
||||
|
||||
Provide clear, visible focus styles for keyboard users.
|
||||
|
||||
❌ BAD: Removing outline without replacement
|
||||
```css
|
||||
a:focus, button:focus {
|
||||
outline: none; /* Don't do this without a custom focus style! */
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD: Custom, visible focus styles
|
||||
```css
|
||||
a:focus, button:focus {
|
||||
outline: 2px solid var(--color-primary);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
```
|
||||
|
||||
### 4.3 Respect `prefers-reduced-motion`
|
||||
|
||||
Offer a reduced motion experience for users who prefer it.
|
||||
|
||||
```css
|
||||
/* Default animation */
|
||||
.element {
|
||||
transition: transform 0.3s ease-out;
|
||||
}
|
||||
|
||||
/* Reduced motion preference */
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.element {
|
||||
transition: none;
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 5. Common Pitfalls & Anti-patterns
|
||||
|
||||
Avoid these patterns to maintain a healthy codebase.
|
||||
|
||||
### 5.1 Avoid `!important`
|
||||
|
||||
`!important` breaks the cascade and makes styles extremely difficult to override and debug. Use it only in rare, justified cases (e.g., utility classes that *must* override everything).
|
||||
|
||||
❌ BAD: Overuse of `!important`
|
||||
```css
|
||||
.button {
|
||||
background-color: red !important;
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD: Manage specificity carefully, use BEM, and rely on the cascade.
|
||||
```css
|
||||
/* Adjust specificity or order */
|
||||
.button--primary {
|
||||
background-color: var(--color-primary);
|
||||
}
|
||||
```
|
||||
|
||||
### 5.2 Avoid ID Selectors in CSS
|
||||
|
||||
IDs have extremely high specificity, leading to specificity wars and reduced reusability. Reserve IDs for JavaScript hooks or fragment identifiers.
|
||||
|
||||
❌ BAD: Styling with IDs
|
||||
```css
|
||||
#main-nav {
|
||||
/* ... */
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD: Use classes instead
|
||||
```css
|
||||
.main-nav {
|
||||
/* ... */
|
||||
}
|
||||
```
|
||||
|
||||
### 5.3 No Inline Styles
|
||||
|
||||
Inline styles are difficult to manage, override, and maintain. Keep all styling in stylesheets.
|
||||
|
||||
❌ BAD: Inline styles
|
||||
```html
|
||||
<div style="color: red; font-size: 16px;">Hello</div>
|
||||
```
|
||||
|
||||
✅ GOOD: Class-based styling
|
||||
```html
|
||||
<div class="text-error text-base">Hello</div>
|
||||
```
|
||||
```css
|
||||
.text-error { color: red; }
|
||||
.text-base { font-size: 1rem; }
|
||||
```
|
||||
|
||||
### 5.4 Use Shorthand Properties
|
||||
|
||||
Use shorthand properties where appropriate to reduce file size and improve readability.
|
||||
|
||||
❌ BAD: Longhand properties
|
||||
```css
|
||||
.box {
|
||||
margin-top: 10px;
|
||||
margin-right: 20px;
|
||||
margin-bottom: 10px;
|
||||
margin-left: 20px;
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD: Shorthand properties
|
||||
```css
|
||||
.box {
|
||||
margin: 10px 20px;
|
||||
}
|
||||
```
|
||||
|
||||
### 5.5 Browser Compatibility (`@supports`)
|
||||
|
||||
Use `@supports` to provide progressive enhancements for modern CSS features, with fallbacks for older browsers.
|
||||
|
||||
```css
|
||||
/* Fallback for older browsers */
|
||||
.gallery {
|
||||
display: block; /* Default to block layout */
|
||||
}
|
||||
|
||||
/* Enhanced layout with Grid */
|
||||
@supports (display: grid) {
|
||||
.gallery {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
||||
gap: 1rem;
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user