1fy07h 发表于 2024-6-30 06:55:53

详解:5种CSS选取器的结合运用


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、元素<span style="color: black;">选取</span>器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、类<span style="color: black;">选取</span>器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、ID<span style="color: black;">选取</span>器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4、属性<span style="color: black;">选取</span>器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5、派生<span style="color: black;">选取</span>器</p>
    <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>的css<span style="color: black;">选取</span>器当属元素<span style="color: black;">选取</span>器了,在HTML文档中该<span style="color: black;">选取</span>器<span style="color: black;">一般</span><span style="color: black;">指的是</span>某种HTML元素,例如:p,h2,span,a,div乃至html。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">用法<span style="color: black;">非常</span>简单,例如:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以下css代码会对<span style="color: black;">全部</span>文档添加黑色背景;将所有p元素字体<span style="color: black;">体积</span>设置为30像素<span style="color: black;">同期</span>添加灰色背景;对文档中所有h2元素添加红色背景。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">1 html {bac<span style="color: black;">公斤</span>round-color: black;}
      2 p {font-size: 30px; bac<span style="color: black;">公斤</span>roud-color: gray;}
      3 h2 {bac<span style="color: black;">公斤</span>round-color: red;}</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>对多个html元素进行声明。例如:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以下css代码会对文档中所有的h1~h6以及p元素字体设置为”黑体”。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">1 h1, h2, h3, h4, h5, h6, p {font-family: 黑体;}</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>看出css的基本规则结构:由<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;">2、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>器有些类似,<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>的ID属性。其次一个ID值在一个HTML文档中只能<span style="color: black;">显现</span>一次,<span style="color: black;">亦</span><span style="color: black;">便是</span>一个ID只能<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<span style="color: black;">选取</span>器前<span style="color: black;">首要</span>要在元素中添加ID属性,例如:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">1 1 &lt;p id="top-para"&gt;...&lt;/p&gt;
      2 2 &lt;p id="foot-para"&gt;...&lt;/p&gt;</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>为井号”#”后面跟id值。<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>以上2个p元素如下:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">1 #top-para {}
      2 #foot-para {}</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>对以上2个段落进行<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>使其用法变得相对简单。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">3、类<span style="color: black;">选取</span>器</h2>
    <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>加粗<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><span style="color: black;">思虑</span><span style="color: black;">运用</span>类<span style="color: black;">选取</span>器(class selector)。</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>对文件元素添加一个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;">1 &lt;p class="deadline"&gt;...&lt;/p&gt;
      2 &lt;h2 class="deadline"&gt;...&lt;/h2&gt;</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>器了:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">1 p.deadline { color: red;}
      2 h2.deadline { color: red;}</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>器。以上2个<span style="color: black;">选取</span>器会<span style="color: black;">选取</span>所有<span style="color: black;">包括</span>”deadline”类的p元素和h2元素。而其余<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>省略.deadline前面的元素名,<span style="color: black;">那样</span>所有<span style="color: black;">包括</span>该类的元素都将被选中:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">1 .deadline { color: red;}</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>以上2者得到更加有趣的样式:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">1 .deadline { color: red;}
      2 span.deadline { font-style: italic;}</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>deadline的元素字体设置为红色,<span style="color: black;">同期</span>会对span元素中的文本添加额外的斜体效果。<span style="color: black;">这般</span>,<span style="color: black;">倘若</span>你<span style="color: black;">期盼</span>某处文本<span style="color: black;">持有</span>额外的斜体效果将它们放在&lt;span&gt;&lt;/span&gt;中就<span style="color: black;">能够</span>了。</p>
    <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;">在实践的做法中,元素的class属性可能不止<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>一个”warning”类,某些元素<span style="color: black;">包括</span>一个”important”类,某些元素<span style="color: black;">同期</span><span style="color: black;">包括</span>”warning important”类。属性名<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;">1 class = "warning important"
      2 cl</div>




vip51888 发表于 2024-8-27 17:40:29

我深感你的理解与共鸣,愿对话长流。

wrjc1hod 发表于 2024-9-8 15:12:26

你字句如珍珠,我珍藏这份情。

4lqedz 发表于 2024-10-5 07:50:09

谷歌外贸网站优化技术。

j8typz 发表于 2024-10-23 06:38:13

你的努力一定会被看见,相信自己,加油。

4lqedz 发表于 2024-10-27 01:38:37

楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/
页: [1]
查看完整版本: 详解:5种CSS选取器的结合运用