Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

how to name classes with BEM methodology? Give multiple nested example

  1. User

    how to name classes with BEM methodology? Give multiple nested example

  2. Assistant

    According to the BEM methodology, class names should be structured in the format of block__element--modifier. Here are some examples of multiple nested classes using BEM notation:

    Example 1: Nested block

    <div class="form">
      <div class="form__label">
        <label class="form__label-text">Name</label>
      </div>
      <div class="form__input">
        <input class="form__input-element" type="text">
      </div>
    </div>
    

    In this example, we have a form block that contains two nested elements - form__label and form__input. Each of these nested elements has its own respective modifier - form__label-text for the label, and form__input-element for the input.

    Example 2: Nested modifier

    <div class="card">
      <div class="card__title">
        <h2 class="card__title-text card__title-text--bold">Title</h2>
      </div>
      <div class="card__content">
        <p class="card__content-text card__content-text--featured">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
    </div>
    

    In this example, we have a card block that contains two nested elements - card__title and card__content. Each of these elements has its own respective modifier, but the card__title-text modifier also has an additional nested modifier of card__title-text--bold to make the text bold. Similarly, the card__content-text modifier has an additional nested modifier of card__content-text--featured to highlight the text.

    Example 3: Multiple nested blocks

    <div class="page">
      <div class="page__header">
        <h1 class="page__title">Page Title</h1>
        <div class="page__navigation">
          <a class="page__navigation-link" href="#">Link 1</a>
          <a class="page__navigation-link" href="#">Link 2</a>
        </div>
      </div>
      <div class="page__content">
        <div class="card">
          <div class="card__title">
            <h2 class="card__title-text card__title-text--bold">Card Title</h2>
          </div>
          <div class="card__content">
            <p class="card__content-text card__content-text--featured">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
          </div>
        </div>
      </div>
    </div>
    

    In this example, we have a page block that contains two nested elements - page__header and page__content. Within the page__header element, we have a nested page__navigation block that contains two links with the page__navigation-link class. Within the page__content element, we have a nested card block with its own nested elements and modifiers.

  3. User

    what to do when it is more nested?

  4. Assistant

    When dealing with more nested elements, it is important to maintain the BEM naming convention of block__element--modifier. One approach is to use additional dashes to separate each level of nesting. For example:

    <div class="page">
      <div class="page__header">
        <h1 class="page__heading">Page Heading</h1>
        <div class="page__navigation">
          <a class="page__navigation-link" href="#">Link 1</a>
          <a class="page__navigation-link" href="#">Link 2</a>
        </div>
        <div class="page__search">
          <div class="page__search-input">
            <input class="page__search-input-field" type="text" placeholder="Search">
          </div>
          <div class="page__search-button">
            <button class="page__search-button-element button--primary">Search</button>
          </div>
        </div>
      </div>
      <div class="page__content">
        <div class="card">
          <div class="card__header">
            <h2 class="card__heading">Card Heading</h2>
            <div class="card__actions">
              <button class="card__action button--small>Save</button>
              <button class="card__action button--small>Cancel</button>
            </div>
          </div>
          <div class="card__body">
            <p class="card__text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
          </div>
          <div class="card__footer">
            <span class="card__timestamp">Posted 2 hours ago</span>
          </div>
          <div class="card__comments">
            <div class="card__comment">
              <span class="card__comment-author">John Doe</span>
              <p class="card__comment-text">Great article!</p>
            </div>
            <div class="card__comment">
              <span class="card__comment-author">Jane Doe</span>
              <p class="card__comment-text">Keep up the good work!</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    

    In this example, we have multiple levels of nested elements within the page and card blocks. Each level is separated by an additional dash (-) and the naming convention of block__element--modifier is still maintained. This approach allows for clear and consistent naming of each element, even in more complex nested structures.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.