Discover how simple names can save you hours of frustrating CSS fixes!
Why Naming conventions in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are styling a website with many buttons, headers, and sections. You write CSS classes like .button1, .button2, .headerMain, and .headerSecondary without a clear pattern.
When you come back later to update styles, it's hard to remember what .button2 does or if .headerMain is for the main header or a sidebar. This confusion leads to mistakes and wasted time fixing styles that break other parts.
Using naming conventions means giving your CSS classes clear, consistent names that describe their purpose and relationship. This makes your code easier to read, understand, and update without guessing.
.button1 { color: blue; }\n.button2 { color: red; }\n.headerMain { font-size: 2rem; }\n.headerSecondary { font-size: 1.5rem; }.btn-primary { color: blue; }\n.btn-danger { color: red; }\n.header--main { font-size: 2rem; }\n.header--secondary { font-size: 1.5rem; }Clear naming conventions let you quickly find and change styles, making your website easier to build and maintain.
Think of organizing your closet: labeling boxes as "Winter Jackets" or "Summer Shirts" helps you find clothes fast. Naming conventions do the same for your CSS classes.
Naming conventions give your CSS classes clear, meaningful names.
This reduces confusion and errors when styling your website.
It makes your code easier to read, update, and share with others.
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
