内容简介:實務上會遇到有些 CSS Class,並沒有設定任何 Property,目的在於建立出 Scope,方便 Selector 選擇到我們要的 Element。CSS 3
實務上會遇到有些 CSS Class,並沒有設定任何 Property,目的在於建立出 Scope,方便 Selector 選擇到我們要的 Element。
Version
CSS 3
Class Selector
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box {
color: #f00;
}
</style>
</head>
<body>
<div class="box">box 1</div>
<div class="box">box 2</div>
</body>
</html>
box 1 與 box 2 都使用 .box ,若我們想讓 box 2 為 紅色 ,直接使用 class selector 選取 .box ,則 box 1 與 box 2 都會被選到,這顯然不是我們要的。
Empty Class
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.post .box {
color: #f00;
}
</style>
</head>
<body>
<div class="box">box 1</div>
<div class="post">
<div class="box">box 2</div>
</div>
</body>
</html>
13 行
<div class="post"> <div class="box">box 2</div> </div>
實務上會在要選擇的 <div> 外面再加上一層 <div> ,並加上一個新的 class。
第 6 行
.post .box {
color: #f00;
}
如此就可使用 descendant selector 同時選取 .post 與 .box 兩個 class,這樣只會選到 box 2 ,類似替 CSS selector 創建了 scope。
Conclusion
- CSS 的 class 並不止用在設定 property,也可以使用 empty class 建立出 scope,方便 CSS selector 選擇到我們要的 element
以上所述就是小编给大家介绍的《使用 Empty Class 建立 Selector Scope》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 使用 Numpy 建立 CNN
- 使用 Gradle 建立 Kotlin 專案
- 第二章 使用VUX建立日历
- webpack项目使用eslint建立代码规范
- [译] 在 iOS 上使用 Carthage 建立依赖
- 如何使用React Hooks建立一个待办事项列表
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。