CSS的选取器
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">想要</span>给HTML元素设置样式,<span style="color: black;">首要</span><span style="color: black;">必须</span>找到这个元素。在CSS中,执行这个任务的<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>器、类<span style="color: black;">选取</span>器、id<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;">1、标记<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>HTML标记名<span style="color: black;">做为</span><span style="color: black;">选取</span>器,</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">标记名</span> <span style="color: black;">{</span><span style="color: black;">属性1:属性值1</span><span style="color: black;">;</span> <span style="color: black;">属性2:属性值2</span><span style="color: black;">;</span> <span style="color: black;">属性3:属性值3</span><span style="color: black;">;}</span></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该语法中,所有的HTML标记名都<span style="color: black;">能够</span><span style="color: black;">做为</span>标记<span style="color: black;">选取</span>器,如body、p、h2等等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如下代码为标记h2添加样式:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">head</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">style</span><span style="color: black;">></span>
<span style="color: black;">h2</span> <span style="color: black;">{</span>
<span style="color: black;">font-family</span><span style="color: black;">:</span> <span style="color: black;">Times New Roman</span><span style="color: black;">;</span> <span style="color: black;">//设置文本的字体系列</span>
<span style="color: black;">color</span><span style="color: black;">:</span><span style="color: black;">rgb</span><span style="color: black;">(</span><span style="color: black;">184</span><span style="color: black;">,</span> <span style="color: black;">201</span><span style="color: black;">,</span> <span style="color: black;">87</span><span style="color: black;">);</span> <span style="color: black;">//设置文本的颜色</span>
<span style="color: black;">font-size</span><span style="color: black;">:</span> <span style="color: black;">28</span><span style="color: black;">px</span><span style="color: black;">;</span> <span style="color: black;">//设置文本的<span style="color: black;">体积</span></span>
<span style="color: black;">}</span>
<span style="color: black;"></</span><span style="color: black;">style</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">head</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">h2</span><span style="color: black;">></span>今天天气好晴朗,处处好风光!<span style="color: black;"></</span><span style="color: black;">h2</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic3.zhimg.com/80/v2-054bd85512c47ca7c5a7fa06befc1d4e_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">2、类<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>在元素中设置class属性,class属性的属性值<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;"><span style="color: black;">.</span><span style="color: black;">类名</span> <span style="color: black;">{</span><span style="color: black;">属性1:属性值1</span><span style="color: black;">;</span> <span style="color: black;">属性2:属性值2</span><span style="color: black;">;</span> <span style="color: black;">属性3:属性值3</span><span style="color: black;">;}</span></div>如下代码展示一个class属性为box的类<span style="color: black;">选取</span>器:<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">head</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">style</span><span style="color: black;">></span>
<span style="color: black;">.</span><span style="color: black;">box</span> <span style="color: black;">{</span>
<span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span> <span style="color: black;">//设置盒子宽</span>
<span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span> <span style="color: black;">//设置盒子高</span>
<span style="color: black;">bac<span style="color: black;">公斤</span>round-color</span><span style="color: black;">:</span> <span style="color: black;">#bfc</span><span style="color: black;">;</span> <span style="color: black;">//设置盒子背景颜色</span>
<span style="color: black;">border</span><span style="color: black;">:</span><span style="color: black;">1</span><span style="color: black;">px</span> <span style="color: black;">solid</span> <span style="color: black;">red</span><span style="color: black;">;</span> <span style="color: black;">//设置盒子边框:宽1像素、实线、颜色为红色</span>
<span style="color: black;">}</span>
<span style="color: black;"></</span><span style="color: black;">style</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">head</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"box"</span><span style="color: black;">></span>
我是一个盒子
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic4.zhimg.com/80/v2-2c6a2b4086a63b332618e77b929aee77_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">3、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>在元素中设置id属性,id属性的属性值<span style="color: black;">便是</span>id名,<span style="color: black;">同期</span><span style="color: black;">运用</span>“#”进行标识,后面紧跟id名:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">#</span><span style="color: black;">id名</span> <span style="color: black;">{</span><span style="color: black;">属性1:属性值1</span><span style="color: black;">;</span> <span style="color: black;">属性2:属性值2</span><span style="color: black;">;</span> <span style="color: black;">属性3:属性值3</span><span style="color: black;">;}</span></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:元素的id名是<span style="color: black;">独一</span>的,只能对应于文档中某一个<span style="color: black;">详细</span>的元素。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">head</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">style</span><span style="color: black;">></span>
<span style="color: black;">#</span><span style="color: black;">btn</span> <span style="color: black;">{</span>
<span style="color: black;">border-radius</span><span style="color: black;">:</span> <span style="color: black;">20</span><span style="color: black;">%</span><span style="color: black;">;</span>
<span style="color: black;">border-color</span><span style="color: black;">:</span> <span style="color: black;">greenyellow</span><span style="color: black;">;</span>
<span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">68</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">height</span><span style="color: black;">:</span><span style="color: black;">38</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;"></</span><span style="color: black;">style</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">head</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">button</span> <span style="color: black;">id</span><span style="color: black;">=</span><span style="color: black;">"btn"</span><span style="color: black;">></span>请点击<span style="color: black;"></</span><span style="color: black;">button</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</span><span style="color: black;">></span>
</div>
楼主节操掉了,还不快捡起来! 我完全同意你的观点,说得太对了。 谢谢、感谢、感恩、辛苦了、有你真好等。 你的见解真是独到,让我受益匪浅。
页:
[1]