gpt4 book ai didi

css - Bourbon 整洁和语义化的 HTML5,BEM 呢?

转载 作者:行者123 更新时间:2023-11-28 05:09:16 25 4
gpt4 key购买 nike

我正在尝试写一个非常简约的博客,作为重新学习 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> <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>

CSS:

    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 的工作原理以及如何优化代码的可读性、可重用性和性能。

最佳答案

只使用类选择器,它会让你的代码更具可读性。

关于css - Bourbon 整洁和语义化的 HTML5,BEM 呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39659796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com