【发布时间】:2017-02-01 06:09:27
【问题描述】:
我正在尝试写一个非常简约的博客作为重新学习 html 和 css 的一种方式。我试图了解应该使用什么来在普通标签、id 或类之间选择页面元素。
例如,这是我的一个页面的样子:
<body>
<header>
<h1><a href="/">My fabulous blog</a></h1>
<nav>
<ul>
<li><a href="#">Page one</a></li>
<li><a href="#">Page two</a></li>
</ul>
</nav>
</header>
<section>
<h1>Latest articles</h1>
<article>
<h1>Title</h1>
<p>I like turtles</p>
</article>
<article>
<h1>Title</h1>
<p>I like potatoes too</p>
</article>
<a href="#">Browse the archive.</a>
</section>
<aside>
<h2>Social</h2>
<ul>
<li>Twitter</li>
<li>Facebook</li>
<li>Github</li>
</ul>
</aside>
<footer>© 1546-2032</footer>
</body>
所以很简单,正如你所见,我没有使用任何类或 id。
css 看起来像:
body {
@include outer-container;
}
header {
@include span-columns(12);
}
section
{
@include span-columns(9);
}
aside
{
@include span-columns(3);
}
footer{
@include span-columns(12);
}
header, aside, footer
{
@include omega;
}
我不确定我是否正确,但由于这是一组非常不具体的 css 规则,因此这是一个很好的做法。如果我必须在另一个上下文中设置其中一个标签的样式,例如 <header>tag 在 <article> 中,使用类应该会毫无问题地覆盖类型选择器。
.article-header {
background-color: #eee;
@include span-columns(12);
}
例如
但是,我不确定这是不是正确的方法,我听说过 BEM 方法,但我可以同时使用这两种方法,以获得良好的可扩展基础。
但是我的 css 应该有多少 BEMed 呢?
我应该先使用标签选择器,用于我的主要布局,然后使用 BEM 样式类进行样式设置?或者只是完全进入 BEM 并在任何地方使用类?
<header class="main-header">
<h1 class="main-header__title"><a href="/">My fabulous blog</a></h1>
<nav class="main-header__nav">
<ul class="main-header__links">
<li class="main-header__item"><a href="#">Page one</a></li>
<li class="main-header__item"><a href="#">Page two</a></li>
</ul>
</nav>
</header>
scss:
main-header {
&__title {
...
}
&__nav {
...
}
&__links {
...
}
&__items {
...
}
}
还有
<body class="body">
[...]
<header class="header">
[...]
</header>
<section class="last-articles">
<h1 class="last-articles__title">Latest articles</h1>
<article class="article">
<h1 class="article__title">Title</h1>
<p class="article__p">I like turtles</p>
<p class="article__p">But I also liek potatoes.</p>
<p class="article__p">But I don't like them both in the same dish.</p>
<p class="article__p">Except in soup.</p>
</article>
和(scss)
.body{
@include outer-container;
}
.header
{
@include span-columns(12);
}
.last-articles
{
@include span-columns(9);
&__title {
}
}
.article
{
&__title{
}
&__p{
}
}
或者只是:
还有
<body>
[...]
<header>
[...]
</header>
<section class="last-articles">
<h1 class="last-articles__title">Latest articles</h1>
<article class="article">
<h1 class="article__title">Title</h1>
<p class="article__p">I like turtles</p>
<p class="article__p">But I also liek potatoes.</p>
<p class="article__p">But I don't like them both in the same dish.</p>
<p class="article__p">Except in soup.</p>
</article>
和(scss)
body{
@include outer-container;
}
header
{
@include span-columns(12);
}
.last-articles
{
@include span-columns(9);
&__title {
}
}
.article
{
&__title{
}
&__p{
}
}
我知道这看起来像是一个特别基于意见的问题,我可能会对此投反对票,但我认为这更多的是了解 Bourbon/Neat 和 BEM 的工作原理以及代码的可读性、可重用性和性能如何优化。
【问题讨论】: