how to name classes with BEM methodology? Give multiple nested example
- User
how to name classes with BEM methodology? Give multiple nested example
- 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
formblock that contains two nested elements -form__labelandform__input. Each of these nested elements has its own respective modifier -form__label-textfor the label, andform__input-elementfor 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
cardblock that contains two nested elements -card__titleandcard__content. Each of these elements has its own respective modifier, but thecard__title-textmodifier also has an additional nested modifier ofcard__title-text--boldto make the text bold. Similarly, thecard__content-textmodifier has an additional nested modifier ofcard__content-text--featuredto 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
pageblock that contains two nested elements -page__headerandpage__content. Within thepage__headerelement, we have a nestedpage__navigationblock that contains two links with thepage__navigation-linkclass. Within thepage__contentelement, we have a nestedcardblock with its own nested elements and modifiers. - User
what to do when it is more nested?
- 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
pageandcardblocks. Each level is separated by an additional dash (-) and the naming convention ofblock__element--modifieris still maintained. This approach allows for clear and consistent naming of each element, even in more complex nested structures.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.