0

希望这是有道理的,但是否可以将一行 css 指向某个标签?

例如我有这个代码

.about h2 {
color: black;
font-weight: 100;
font-size: 26px;
}

我在另一个名为“项目”的类中有一个 H2,并且想要一个 margin-top:20px; 在上面的代码中^。因此,我可以将它添加到上面并使其仅适用于“项​​目”H2,而不是复制和粘贴它?

谢谢!

4

3 回答 3

1

这是:

<style>
.about h2 {
color: black;
font-weight: 100;
font-size: 26px;
}

.about.projects h2{
margin-top: 20px;
}
</style>

<div class="about">
    <h2>About</h2>
</div>

<div class="projects about">
    <h2>About projects</h2>
</div>

通过这种方式,您可以在 .projects 中重用 .about 样式

于 2013-11-04T17:00:25.923 回答
0

如果您希望您的 css 仅适用h2于 class 中的an Projects,您可以这样做:

h2.Projects {
    margin-top: 20px;
}
于 2013-11-04T16:57:29.620 回答
0

假设“想要一个margin-top:20px;在上面的代码中”意味着你想要关于类的margin-top,这就是你应该使用的

.about h2, .Projects h2 {
    color: black;
    font-weight: 100;
    font-size: 26px;
}

.about h2 {
    margin-top: 20px;
}
于 2013-11-04T17:06:19.083 回答