我想在一个页面上的所有h标签。我知道你可以这样做……
h1,
h2,
h3,
h4,
h5,
h6 {
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
但是是否有更有效的方法来使用先进的CSS选择器?例如:
[att^=h] {
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
(但显然这行不通)
我想在一个页面上的所有h标签。我知道你可以这样做……
h1,
h2,
h3,
h4,
h5,
h6 {
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
但是是否有更有效的方法来使用先进的CSS选择器?例如:
[att^=h] {
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
(但显然这行不通)
当前回答
不,在这种情况下,用逗号分隔的列表才是你想要的。
其他回答
不,在这种情况下,用逗号分隔的列表才是你想要的。
如果你想用一个选择器对文档中的所有标题进行分类,如下所示:
<h1 class="heading">...heading text...</h1>
<h2 class="heading">...heading text...</h2>
在CSS中
.heading{
color: #Dad;
background-color: #DadDad;
}
我并不是说这总是最佳实践,但它可能是有用的,对于目标语法来说,在许多方面都更容易,
所以如果你给所有h1到h6在html中相同的。heading类,那么你可以修改他们的任何html文档,利用css表。
好处是,与“section div article h1, etc{}”相比,更多的全局控制,
缺点,而不是在css中调用所有的选择器,你将在html中有更多的输入,但我发现,在html中有一个类的目标所有标题可以是有益的,只是要小心在css的优先级,因为冲突可能会产生
它不是基本的css,但如果你使用LESS (http://lesscss.org),你可以使用递归来做到这一点:
.hClass (@index) when (@index > 0) {
h@{index} {
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
.hClass(@index - 1);
}
.hClass(6);
Sass (http://sass-lang.com)将允许您管理这个,但不允许递归;下面这些实例有@for语法:
@for $index from 1 through 6 {
h#{$index}{
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
}
如果你使用的不是像LESS或Sass这样编译成CSS的动态语言,你绝对应该看看这些选项中的一个。它们可以真正简化并使您的CSS开发更加动态。
手写笔的选择插值
for n in 1..6
h{n}
font: 32px/42px trajan-pro-1,trajan-pro-2;
如果你正在使用SASS,你也可以使用这个mixin:
@mixin headings {
h1, h2, h3,
h4, h5, h6 {
@content;
}
}
像这样使用它:
@include headings {
font: 32px/42px trajan-pro-1, trajan-pro-2;
}
编辑:我个人最喜欢的方法是在每个标题元素上扩展一个占位符选择器。
h1, h2, h3,
h4, h5, h6 {
@extend %headings !optional;
}
然后我可以瞄准所有的标题,就像我瞄准任何单个类一样,例如:
.element > %headings {
color: red;
}