首页 > 开发 > CSS > 正文

CSS代码组合和嵌套

2024-07-11 08:21:30
字体:
来源:转载
供稿:网友

css的组合和嵌套

css组合

你不必重复有相同属性的多个选择符,你只要用英文逗号(,)隔开选择符就可以了。

比如,你有如下的代码:

h2 { color: red; }
.thisotherclass { color: red; }
.yetanotherclass { color: red; }

则你可以这样写:

h2, .thisotherclass, .yetanotherclass { color: red; }

使用组合,你可以一次定义多个css,为你节省很多字节和时间.

css嵌套

css结构好的话,没有必要使用过多的类或者标识选择符。这是因为你可以指定在选择符内的选择符。(或者更好的说法,上下文选择符--译者著)

比如:

#top { background-color: #ccc; padding: 1em }
#top h1 { color: #ff0; }
#top p { color: red; font-weight: bold; }

这就减去不必要的类或者标识选择符,如果应用到像这样的html中:

<div id="top">
 <h1>chocolate curry</h1> <p>this is my recipe for making curry purely with chocolate</p> <p>mmm mm mmmmm</p>
</div>

这是因为,用英文半角空格间隔选择符,我们指明了在标识id内的h1有“#ff0”的颜色,而p则是红色red和粗体bold。

这可能也会有些复杂(因为可能不止两级,比如在内在内在内在内等等)。你有必要多加练习。

使用嵌套,可以让你的css代码读起来更清晰,能给你制定的元素定制css.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表