search
HomeWeb Front-endHTML TutorialCSS那些事儿-阅读随笔2(选择符的组合与权重)_html/css_WEB-ITnose

在知道了CSS选择符最基础的知识后,就要综合利用它们了。这里就记录几种常见的用法。

1.针对性的使用类选择符或者ID选择符

  类选择符在一个页面中可能会在不同的地方应用,那么就需要有针对性地使用类选择符。如下例:

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>css-test</title> 6     <style> 7         .myContent{ 8             font-size: 12px; 9             color: #00f;10         }11         p.myContent{12             font-size: 25px;13             line-height: 5px;14             text-decoration:underline;15             font-weight:bold;16             color: #f00;17         }18     </style>19 </head>20 <body>21 <div class="myContent">css很强大,可以控制页面任何元素的样式</div>22 <p class="myContent">1与世界同步,做一个成功的页面仔</p>23 <span class="myContent">2让我们看看css多么奇妙吧</span>24 </body>25 </html>

  该例子在style中定义了一个.myContent的css类和组合选择符 p.myContent(注意p和.myContent没有空格),且页面中的div、p和span元素也都引用了myContent类,但是由于存在p.myContent,所以div和span的文字会是蓝色,而p中的文字会是红色,如下图所示:

在此种应用下,ID选择符和类选择符相似,只需将.换成#即可(p#myId),如下例

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>css-test</title> 6     <style> 7         .myContent{ 8             font-size: 12px; 9             color: #00f;10         }11         p#myId{12             font-size: 25px;13             line-height: 5px;14             text-decoration:underline;15             font-weight:bold;16             color: #f00;17         }18     </style>19 </head>20 <body>21 <!--<p>css很强大,<span>可以控制页面<strong>任何元素</strong>的样式</span><strong>dfd</strong></p>-->22 <div class="myContent">css很强大,可以控制页面任何元素的样式</div>23 <p id="myId">1与世界同步,做一个成功的页面仔</p>24 <span class="myContent">2让我们看看css多么奇妙吧</span>25 </body>26 </html>

运行结果和上图一样。

2.选择符群组

即将多个相同定义的选择符合并,如下例中将p,类选择符.myContent以及id选择符#myId共同定义成红色、加粗、带下划线、字体大小为25px的文字 p,.myContent,#myId {property:value;},注意它们之间使用逗号隔开的。

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>css-test</title> 6     <style> 7         p,.myContent,#myId{ 8             font-size: 25px; 9             text-decoration:underline;10             font-weight:bold;11             color: #f00;12         }13     </style>14 </head>15 <body>16 <!--<p>css很强大,<span>可以控制页面<strong>任何元素</strong>的样式</span><strong>dfd</strong></p>-->17 <div class="myContent">css很强大,可以控制页面任何元素的样式</div>18 <p>1与世界同步,做一个成功的页面仔</p>19 <span id="myId">2让我们看看css多么奇妙吧</span>20 </body>21 </html>

运行结果如下图

3.CSS的优先级

    多重样式(Multiple Styles):如果外部样式、内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况。一般情况下,此时的优先级如下:

标有!important样式>内嵌样式 (HTML元素中的style)> 内部样式表 (head中的style)> 外联样式表(head中外部引入的)>浏览器默认样式

例如:

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>css-test</title> 6     <style> 7         p,.myContent,#myId{ 8             font-size: 25px; 9             text-decoration:underline;10             font-weight:bold;11             color: #f00;12         }13     </style>14 </head>15 <body>16 <div class="myContent">css很强大,可以控制页面任何元素的样式</div>17 <p style="color: blue">1与世界同步,做一个成功的页面仔</p>18 <span id="myId">2让我们看看css多么奇妙吧</span>19 </body>20 </html>

虽然在head的style中设置了p标签内的文字样式颜色为红色,但是在HTML的p元素中又通过style属性对其颜色进行了设置(蓝色)。因为内嵌样式优先级高于内部样式表,所以p标签中的文字最终表现为蓝色,如下图所示。

4.CSS的权重

  为了在多个样式修饰同一元素时,更准确的判断到底利用哪个CSS,可以使用权重加权的方法,即为每类选择符赋予权重,然后计算出现的选择符组合的加权权重,最终得出的积分最高的就为最终的样式。

  • 标签选择符、伪类与伪对象:权重为1。
  • 类选择符、属性选择符:权重为10。
  • ID选择符:权重为100。
  • 内联style属性:权重为1000。
  • !important:权重为无穷。
  • 例子:

     1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>css-test</title> 6     <style> 7         p { 8             color:blue; 9         }10         /*p * {color: red;}*/11         p.myColor{12             color: black;13         }14         .myColor{15             color: yellow;16         }17         #myId{18             color: green;19         }20     </style>21 </head>22 <body>23 <p>Hello</p>24 <p class="myColor">css很强大,可以控制页面任何元素的样式</p>25 <p class="myColor" id="myId">1与世界同步,做一p st页面仔</p>26 <p style="color: red" class="myColor">2让我们看看css多么奇妙吧</p>27 </body>28 </html>

    分析:代码中,各个选择符的权重加权后所得的积分如下

    p=1
    p.myColor=1+10=11
    .myColor=10
    #myId=100
    style="color: red"=1000<br /><br />所以,“Hello”为蓝色(p=1);“css很强大,可以控制页面任何元素的样式”为黑色(p.myColor=1+10=11);“1与世界同步,做一p st页面仔”为绿色(#myId=100);“2让我们看看css多么奇妙吧”为红色(style="color: red"=1000),如下图所示。<br /><br /><strong>5.css引入顺序的影响</strong><br />在head中的style中定义样式(.myColor1和.myColor2)的顺序以及在HTML元素p中引用这些类时的顺序会产生怎样的影响呢?我们做个试验。<br />a.首先在head的style中首定义.myColor1,再定义.myColor2;然后在p中分别引入两个类,但顺序不同<p class="myColor1 myColor2">css很强大,可以控制页面任何元素的样式</p><p class="myColor2 myColor1">1与世界同步,做一个页面仔</p>,代码如下:<br />

     1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>css-test</title> 6     <style> 7         .myColor1{ 8             color: red; 9         }10         .myColor2{11             color: blue;12         }13     </style>14 </head>15 <body>16 <p class="myColor1 myColor2">css很强大,可以控制页面任何元素的样式</p>17 <p class="myColor2 myColor1">1与世界同步,做一个页面仔</p>18 <p>2让我们看看css多么奇妙吧</p>19 </body>20 </html>

        
    运行结果如下图:<br /><br />

     b.首先在head的style中首定义.myColor2,再定义.myColor1;然后在p中分别引入两个类,但顺序不同

    css很强大,可以控制页面任何元素的样式

    1与世界同步,做一个页面仔

    ,代码如下:

     1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>css-test</title> 6     <style> 7         .myColor2{ 8             color: blue; 9         }10         .myColor1{11             color: red;12         }13     </style>14 </head>15 <body>16 <!--<p>css很强大,<span>可以控制页面<strong>任何元素</strong>的样式</span><strong>dfd</strong></p>-->17 <p class="myColor1 myColor2">css很强大,可以控制页面任何元素的样式</p>18 <p class="myColor2 myColor1">1与世界同步,做一个页面仔</p>19 <p>2让我们看看css多么奇妙吧</p>20 </body>21 </html>

    运行结果如下图:

      通过上述两组实验可以看出,css只与定义的顺序有关系,而与在元素中引用的顺序并没有关系,并且后定义的样式会覆盖之前定义的样式,其实这也是css为什么叫做样式层叠表。

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

    The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

    How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

    This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

    What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

    How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)