gpt4 book ai didi

html - 具体类名与类层次结构

转载 作者:技术小花猫 更新时间:2023-10-29 12:04:18 25 4
gpt4 key购买 nike

这些示例的最佳实践是什么?

1)

<style type="text/css">
.block .title {color:red}
.anotherBlock .title {color:blue}
</style>
...
<div class="block">
<h3 class="title">SomeTitle</h3>
</div>
<div class="anotherBlock">
<h3 class="title">anotherTitle</h3>
</div>
...

2)

<style type="text/css">
.blockTitle {color:red}
.anotherBlockTitle {color:blue}
</style>
...
<div class="block">
<h3 class="blockTitle">SomeTitle</h3>
</div>
<div class="anotherBlock">
<h3 class="anotherBlockTitle">anotherTitle</h3>
</div>
...

第一个代码看起来更干净,但它会让一些人认为 h3 标签将具有相同的样式属性。

最佳答案

<style type="text/css">
.block h3 {color:red}
.anotherBlock h3 {color:blue}
</style>
...
<div class="block">
<h3>SomeTitle</h3>
</div>
<div class="anotherBlock">
<h3>anotherTitle</h3>
</div>

为什么?因为它与您的示例一样可读,但编写的代码更少 = 在用户浏览器中呈现的时间更少

关于html - 具体类名与类层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3127466/

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