CSS选取器详解
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">选取</span>器的种类有<span style="color: black;">非常多</span>,<span style="color: black;">能够</span>到 <a style="color: black;">w3cschool</a> 去<span style="color: black;">瞧瞧</span><span style="color: black;">认识</span>一下。<span style="color: black;">亦</span><span style="color: black;">能够</span>在 <a style="color: black;">慕课网</a> 中实践一下。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>上下文<span style="color: black;">选取</span>器</h2>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">标签<span style="color: black;">选取</span>器</h2>
<div style="color: black; text-align: left; margin-bottom: 10px;"><style>
h1 {font-size: 16px;}
p {color:red;}
</style>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码中的h1和p<span style="color: black;">便是</span><span style="color: black;">选取</span>器,<span style="color: black;">她们</span>是<span style="color: black;">选取</span>器里面最常用的一种,叫做标签<span style="color: black;">选取</span>器。<span style="color: black;">咱们</span><span style="color: black;">能够</span>直接<span style="color: black;">经过</span>标签元素来指定<span style="color: black;">必须</span>添加样式的位置。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">咱们</span><span style="color: black;">必须</span>为多个标签添加同一种样式时,<span style="color: black;">咱们</span><span style="color: black;">能够</span>把它们组合在<span style="color: black;">一块</span>,<span style="color: black;">每一个</span>标签<span style="color: black;">选取</span>器用“,”隔开,如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><style>
h1,h2,h3 {
font-weight: bold;
color: blue;
}
</style></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这般</span>的组合<span style="color: black;">咱们</span>叫它分组<span style="color: black;">选取</span>器。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">后代组合<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当<span style="color: black;">咱们</span>想为下面的article和aside的段落文本分别设置<span style="color: black;">区别</span>的字号时,<span style="color: black;">咱们</span>就<span style="color: black;">能够</span>用到后代<span style="color: black;">选取</span>器了,它们是在祖先元素和后代元素之间加了一个空格,如代码所示:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><style>
article p {
font-size: 12px;
}
aside p {
font-size: 14px;
}
</style></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">后代<span style="color: black;">选取</span>器有一个问题<span style="color: black;">便是</span>,祖先元素<span style="color: black;">选取</span>的后代元素都会带有样式,<span style="color: black;">然则</span><span style="color: black;">咱们</span>有时候并不<span style="color: black;">必须</span>所有的标签都带有样式,这个时候<span style="color: black;">咱们</span>就要用到其他的<span style="color: black;">选取</span>器了。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">子<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">能够</span>用DOM中的父子元素关系来<span style="color: black;">选取</span>,<span style="color: black;">亦</span><span style="color: black;">便是</span>子<span style="color: black;">选取</span>器,两个元素中间用“>”来连接,如代码所示:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><style>
article>p {
font-size: 12px;
}
</style></div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">同胞<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">经过</span>同胞关系来<span style="color: black;">选取</span>,叫做同胞<span style="color: black;">选取</span>器<span style="color: black;">或</span>是兄弟<span style="color: black;">选取</span>器,这就<span style="color: black;">寓意</span>着<span style="color: black;">选取</span>器的标签元素<span style="color: black;">必须</span><span style="color: black;">拥有</span>同一个父元素,它们之间用“+”来连接,例子:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><style>
h2+p {
font-size: 12px;
}
</style></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">并且<span style="color: black;">必须</span><span style="color: black;">重视</span>的是:p标签<span style="color: black;">必要</span>是紧跟在h2标签的后面。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">通常</span>同胞<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">通常</span>同胞<span style="color: black;">选取</span>器中间用“~”连接。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><style>
h2~p {
font-size: 12px;
}
</style></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">通常</span>同胞<span style="color: black;">选取</span>器和同胞<span style="color: black;">选取</span>器的区别<span style="color: black;">便是</span>,p标签不<span style="color: black;">必定</span>是紧跟在h2标签的后面。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">通用<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">通用<span style="color: black;">选取</span>器是<span style="color: black;">运用</span>通配符“ * ”,它<span style="color: black;">能够</span>匹配任何元素。<span style="color: black;">例如</span>:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">*{
color: green;
}
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">它会<span style="color: black;">引起</span>所有文本和边框都变成绿色。<span style="color: black;">这儿</span>有个小知识:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">color属性设定的是前景色。前景色既影响文本<span style="color: black;">亦</span>影响边框,但<span style="color: black;">一般</span><span style="color: black;">咱们</span>只用它设定文本颜色。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">这般</span><span style="color: black;">运用</span>通用<span style="color: black;">选取</span>器:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">p* {
color: green;
}
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这般</span>只会把p<span style="color: black;">包括</span>的所有元素的文本变成红色。</p>通用<span style="color: black;">选取</span>器还有一个有意思的用法,<span style="color: black;">便是</span><span style="color: black;">能够</span>用它<span style="color: black;">形成</span>一个非子<span style="color: black;">选取</span>器,<span style="color: black;">例如</span>:
<div style="color: black; text-align: left; margin-bottom: 10px;">section*a {
font-size: 1.3em;
}</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码所<span style="color: black;">暗示</span>的是,所有section的孙子元素,而非子元素的a标签都会被选中。这个从<span style="color: black;">规律</span>上<span style="color: black;">来讲</span><span style="color: black;">亦</span>好理解,左边的section<span style="color: black;">表率</span>通配符的父元素,右边的a<span style="color: black;">表率</span>通配符的子元素,而*<span style="color: black;">便是</span>所有中间元素的集合。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上<span style="color: black;">便是</span><span style="color: black;">经过</span>DOM的层次结构的“上下文”关系来<span style="color: black;">暗示</span>的<span style="color: black;">选取</span>器。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span>id和类<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">id和类<span style="color: black;">选取</span>器是<span style="color: black;">咱们</span>在CSS中常用的<span style="color: black;">选取</span>器,它们<span style="color: black;">能够</span>更精确的定位到<span style="color: black;">咱们</span>要添加样式的标签位置。只要在HTML标记中为元素添加id和class属性,就<span style="color: black;">能够</span><span style="color: black;">经过</span>id和类<span style="color: black;">选取</span>器直接<span style="color: black;">选取</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>给id和class属性设定任何值,但<span style="color: black;">不可</span>以数字或特殊符号开头。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">类<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">body标签<span style="color: black;">包括</span>的任何HTML元素都<span style="color: black;">能够</span>添加class属性,如:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><h1 class="specialtext">这是一个H1标签</h1>
<p>这是一个段落。</p>
<p class="specialtext featured">这是另一个段落</p>
</div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">1、类<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span><span style="color: black;">咱们</span>就<span style="color: black;">能够</span>用类<span style="color: black;">选取</span>器添加样式了,类<span style="color: black;">选取</span>器前面要加一个“ . ” ,后面跟着类名,如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><style>
p {
font</div>
大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。 你的见解独到,让我受益匪浅,非常感谢。 一看到楼主的气势,我就觉得楼主同在社区里灌水。 我赞同你的看法,你的智慧让人佩服,谢谢分享。 回顾历史,我们感慨万千;放眼未来,我们信心百倍。
页:
[1]