Bird
Raised Fist0
CSSmarkup~5 mins

Naming conventions in CSS

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Naming conventions help keep your CSS organized and easy to understand. They make it simple to find and change styles later.

When creating styles for buttons, headings, or other page parts to keep names clear.
When working with a team so everyone uses the same style names.
When building a website that will grow bigger and needs easy maintenance.
When you want to avoid style conflicts between different parts of your site.
When you want your CSS to be easy to read and follow like a story.
Syntax
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.

Examples
Class name for the main header using hyphens to separate words.
CSS
.header-main {
  font-size: 2rem;
}
Using BEM style with double underscore to show 'item' belongs to 'nav'.
CSS
.nav__item {
  padding: 1rem;
}
BEM modifier with double hyphen to show a variation of the button.
CSS
.btn--large {
  padding: 1.5rem 3rem;
}
ID selector naming for a unique page section like footer.
CSS
#footer {
  background-color: #333;
  color: #eee;
}
Sample Program

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.

CSS
<!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>
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. Which of the following is the best practice for naming CSS classes?
easy
A. Use camelCase, like mainHeader.
B. Use uppercase letters and underscores, like Main_Header.
C. Use lowercase letters and hyphens to separate words, like main-header.
D. Use spaces between words, like main header.

Solution

  1. Step 1: Understand common CSS naming styles

    CSS classes should be easy to read and consistent. Lowercase with hyphens is widely accepted.
  2. Step 2: Compare options

    Use lowercase letters and hyphens to separate words, like main-header. uses lowercase and hyphens, which is recommended. Options B and C use uppercase or camelCase, less common in CSS. Use spaces between words, like main header. uses spaces, which is invalid in class names.
  3. Final Answer:

    Use lowercase letters and hyphens to separate words, like main-header. -> Option C
  4. Quick Check:

    Lowercase + hyphens = best CSS naming [OK]
Hint: Use lowercase and hyphens for CSS classes [OK]
Common Mistakes:
  • Using spaces in class names
  • Using uppercase letters
  • Using camelCase instead of hyphens
2. Which CSS class name follows the BEM naming convention?
easy
A. button--primary
B. button_primary
C. buttonPrimary
D. button primary

Solution

  1. Step 1: Recall BEM naming rules

    BEM uses block__element--modifier format. Double hyphens separate modifiers.
  2. 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.
  3. Final Answer:

    button--primary -> Option A
  4. Quick Check:

    BEM modifier uses double hyphens [OK]
Hint: BEM modifiers use double hyphens (--modifier) [OK]
Common Mistakes:
  • Using underscores instead of hyphens
  • Using camelCase for BEM
  • Using spaces in class names
3. Given this CSS class name: card__title--large, what does it represent in BEM?
medium
A. Block: card, Modifier: title, Element: large
B. Modifier: card, Block: title, Element: large
C. Element: card, Block: title, Modifier: large
D. Block: card, Element: title, Modifier: large

Solution

  1. Step 1: Break down the BEM parts

    In card__title--large, card is the block, title is the element (after double underscore), and large is the modifier (after double hyphen).
  2. Step 2: Match parts to options

    Block: card, Element: title, Modifier: large correctly identifies block, element, and modifier. Other options mix these parts incorrectly.
  3. Final Answer:

    Block: card, Element: title, Modifier: large -> Option D
  4. Quick Check:

    BEM = block__element--modifier [OK]
Hint: BEM: block__element--modifier order [OK]
Common Mistakes:
  • Confusing element and modifier positions
  • Mixing block and element names
  • Ignoring double underscores and hyphens
4. Identify the error in this CSS class name: nav__item_active
medium
A. Class name is too long
B. Underscore used instead of double hyphen for modifier
C. Spaces are used instead of underscores
D. Double underscore missing between block and element

Solution

  1. Step 1: Check BEM syntax

    BEM requires modifiers to be separated by double hyphens --, not underscores.
  2. Step 2: Analyze the class name

    The class nav__item_active uses a single underscore for the modifier, which is incorrect. It should be nav__item--active.
  3. Final Answer:

    Underscore used instead of double hyphen for modifier -> Option B
  4. Quick Check:

    Modifier separator = double hyphen [OK]
Hint: Modifiers need double hyphens, not underscores [OK]
Common Mistakes:
  • Using single underscore for modifiers
  • Forgetting double underscore between block and element
  • Using spaces in class names
5. You want to create CSS classes for a card component with a title and a highlighted state using BEM. Which naming set is correct?
hard
A. card, card__title, card--highlighted
B. card, card_title, card_highlighted
C. card, card-title, card-highlighted
D. card, card__title--highlighted, card__highlighted

Solution

  1. Step 1: Understand BEM structure for blocks, elements, and modifiers

    Block is card. Element is card__title. Modifier on block is card--highlighted.
  2. Step 2: Evaluate options

    card, card__title, card--highlighted correctly uses BEM naming. card, card_title, card_highlighted uses underscores, not BEM. card, card-title, card-highlighted uses hyphens but not BEM format. card, card__title--highlighted, card__highlighted incorrectly applies modifier to element and duplicates modifier.
  3. Final Answer:

    card, card__title, card--highlighted -> Option A
  4. Quick Check:

    BEM block, element, modifier naming [OK]
Hint: BEM: block, block__element, block--modifier [OK]
Common Mistakes:
  • Using underscores instead of BEM separators
  • Applying modifiers to elements incorrectly
  • Mixing hyphen and underscore styles