BEM:这是写BEM的正确方法吗?

时间:2017-03-19 20:17:07

标签: css bem

刚刚开始关注BEM的新项目。有人可以通过下面的模板了解我是否正确理解了BEM方法?我得到了它,但我觉得它会更好。

我的模板

 <div id="wrapper" class="content">
  <header>
    <div class="site-logo">
      <img src="path/to/logo.png" class="site-logo__main">
    </div>
    <div class="site-navigation">
      <nav class="main-menu">
        <ul class="list">
          <li class="list-item">
            <a href="" class="main-menu__active">Nav link</a>
          </li>
          <li class="list-item">
            <a href="">Nav link</a>
          </li>
        </ul>
      </nav>
    </div>
    <div class="header-avatar">
         <img src="path/to/logo.png" class="header-avatar__main">
    </div>
  </header>
  <main>
    <aside class="user-details">
      <ul class="list">
        <li class="list-item">
          <a href="" class="user-details__active">Nav link</a>
        </li>
        <li class="list-item">
          <a href="">Nav link</a>
        </li>
        </ul>
      <div class="sidebar-avatar">
        <img src="path/to/logo.png" class="user-details-avatar">
      </div>
    </aside>
    <section class="contact-us">
      <div class="container">
        <div class="row">
          <div class="contact-details">xxx</div>
          <div class="contact-image"></div>
        </div>
      </div>
    </section>
  </main>
  <footer>
    <div class="footer-avatar">xxx</div>
    <div class="copyright">xxx</div>
    <div class="footer-links">
                    <ul class="list">
          <li class="list-item">
            <a href="">Nav link</a>
          </li>
          <li class="list-item">
            <a href="">Nav link</a>
          </li>
        </ul>
    </div>
  </footer>
</div>

1 个答案:

答案 0 :(得分:4)

从OO的角度来看,这可能更有意义。

<div id="wrapper">
    <header class="header">
        <div class="header__site-logo">
            <img src="path/to/logo.png">
        </div>
        <div class="header__site-navigation">
            <nav class="main-menu">
                <ul class="main-menu__list list">
                    <li class="list-item">
                        <a href="" class="main-menu__link active">Nav link</a>
                    </li>
                    <li class="list-item">
                        <a href="" class="main-menu__link">Nav link</a>
                    </li>
                </ul>
            </nav>
        </div>
        <div class="header__avatar">
             <img src="path/to/logo.png">
        </div>
    </header>
    <main id="content">
        <aside class="user-details">
            <ul class="user-details__list list">
                <li class="list-item">
                    <a href="" class="user-details__link active">Nav link</a>
                </li>
                <li class="list-item">
                    <a href="" class="user-details__link">Nav link</a>
                </li>
                </ul>
            <div class="user-details__avatar">
                <img src="path/to/logo.png">
            </div>
        </aside>
        <section class="contact-us">
            <div class="container">
                <div class="row">
                    <div class="contact-us__details">xxx</div>
                    <div class="contact-us__image"></div>
                </div>
            </div>
        </section>
    </main>
    <footer class="footer">
        <div class="footer__avatar">xxx</div>
        <div class="footer__copyright">xxx</div>
        <div class="footer__links">
            <ul class="footer__list list">
                <li class="list-item">
                    <a href="" class="footer__link">Nav link</a>
                </li>
                <li class="list-item">
                    <a href="" class="footer__link">Nav link</a>
                </li>
            </ul>
        </div>
    </footer>
</div>

尝试将您的DOM想象为一组高级组件:

wrapper div
  > header
    > main-menu
  > content
    > user-details
    > contact-us
  > footer

将这些用作&#34;块&#34;,这意味着它们会在您的班级名称中的双下划线之前。

对于每个块所具有的每个组件,请将友好名称附加到该组件并将其用作类名。

header
  > header__site-logo
  > header__site-navigation
  > header__avatar