Naming conventions help keep your CSS organized and easy to understand. They make it simple to find and change styles later.
Naming conventions in CSS
/* Example of a CSS class name */ .button-primary { background-color: blue; color: white; }
Use lowercase letters and hyphens (-) to separate words.
Start class names with a letter, not a number or special character.
.header-main { font-size: 2rem; }
.nav__item { padding: 1rem; }
.btn--large { padding: 1.5rem 3rem; }
#footer { background-color: #333; color: #eee; }
This example shows a card component styled with clear CSS class names using the BEM naming convention. The block is card, the element inside is card__title, and the modifier card--highlighted changes the background color.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>CSS Naming Conventions Example</title> <style> /* Block element */ .card { border: 2px solid #4a90e2; padding: 1rem; border-radius: 0.5rem; max-width: 300px; margin: 1rem auto; font-family: Arial, sans-serif; } /* Element inside block */ .card__title { font-size: 1.5rem; color: #4a90e2; margin-bottom: 0.5rem; } /* Modifier for variation */ .card--highlighted { background-color: #e6f0ff; } </style> </head> <body> <section class="card card--highlighted" aria-label="Highlighted card"> <h2 class="card__title">Welcome!</h2> <p>This card uses clear naming conventions for easy styling.</p> </section> </body> </html>
Consistent naming helps you and others understand your CSS quickly.
Use semantic names that describe the purpose, not the look (e.g., .button-primary instead of .blue-button).
Consider popular conventions like BEM (Block__Element--Modifier) for bigger projects.
Good naming conventions keep CSS organized and easy to maintain.
Use lowercase letters and hyphens to separate words in class names.
BEM is a helpful pattern to name blocks, elements, and modifiers clearly.
Practice
Solution
Step 1: Understand common CSS naming styles
CSS classes should be easy to read and consistent. Lowercase with hyphens is widely accepted.Step 2: Compare options
Use lowercase letters and hyphens to separate words, likemain-header. uses lowercase and hyphens, which is recommended. Options B and C use uppercase or camelCase, less common in CSS. Use spaces between words, likemain header. uses spaces, which is invalid in class names.Final Answer:
Use lowercase letters and hyphens to separate words, likemain-header. -> Option CQuick Check:
Lowercase + hyphens = best CSS naming [OK]
- Using spaces in class names
- Using uppercase letters
- Using camelCase instead of hyphens
Solution
Step 1: Recall BEM naming rules
BEM usesblock__element--modifierformat. Double hyphens separate modifiers.Step 2: Identify correct format
button--primary uses double hyphens for modifier--primary, matching BEM. Options A and B use camelCase or underscores, not BEM. button primary uses space, invalid in class names.Final Answer:
button--primary -> Option AQuick Check:
BEM modifier uses double hyphens [OK]
- Using underscores instead of hyphens
- Using camelCase for BEM
- Using spaces in class names
card__title--large, what does it represent in BEM?Solution
Step 1: Break down the BEM parts
Incard__title--large,cardis the block,titleis the element (after double underscore), andlargeis the modifier (after double hyphen).Step 2: Match parts to options
Block: card, Element: title, Modifier: large correctly identifies block, element, and modifier. Other options mix these parts incorrectly.Final Answer:
Block: card, Element: title, Modifier: large -> Option DQuick Check:
BEM = block__element--modifier [OK]
- Confusing element and modifier positions
- Mixing block and element names
- Ignoring double underscores and hyphens
nav__item_activeSolution
Step 1: Check BEM syntax
BEM requires modifiers to be separated by double hyphens--, not underscores.Step 2: Analyze the class name
The classnav__item_activeuses a single underscore for the modifier, which is incorrect. It should benav__item--active.Final Answer:
Underscore used instead of double hyphen for modifier -> Option BQuick Check:
Modifier separator = double hyphen [OK]
- Using single underscore for modifiers
- Forgetting double underscore between block and element
- Using spaces in class names
Solution
Step 1: Understand BEM structure for blocks, elements, and modifiers
Block iscard. Element iscard__title. Modifier on block iscard--highlighted.Step 2: Evaluate options
card,card__title,card--highlightedcorrectly uses BEM naming.card,card_title,card_highlighteduses underscores, not BEM.card,card-title,card-highlighteduses hyphens but not BEM format.card,card__title--highlighted,card__highlightedincorrectly applies modifier to element and duplicates modifier.Final Answer:
card,card__title,card--highlighted-> Option AQuick Check:
BEM block, element, modifier naming [OK]
- Using underscores instead of BEM separators
- Applying modifiers to elements incorrectly
- Mixing hyphen and underscore styles
Ready to prove it?
Pass the test and this pattern moves unknown
