希望这是有道理的,但是否可以将一行 css 指向某个标签?
例如我有这个代码
.about h2 {
color: black;
font-weight: 100;
font-size: 26px;
}
我在另一个名为“项目”的类中有一个 H2,并且想要一个 margin-top:20px; 在上面的代码中^。因此,我可以将它添加到上面并使其仅适用于“项目”H2,而不是复制和粘贴它?
谢谢!
这是:
<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 样式
如果您希望您的 css 仅适用h2
于 class 中的an Projects
,您可以这样做:
h2.Projects {
margin-top: 20px;
}
假设“想要一个margin-top:20px;在上面的代码中”意味着你想要关于类的margin-top,这就是你应该使用的
.about h2, .Projects h2 {
color: black;
font-weight: 100;
font-size: 26px;
}
.about h2 {
margin-top: 20px;
}