Blogger Information
Blog 3
fans 0
comment 0
visits 2144
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
选择器的使用和模块化组件思想
Nicole
Original
566 people have browsed it

1.选择器的优先级

行内样式 > style标签设置的内部样式
如果优先级相同,显示最后的样式。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <!--内部样式,仅作用于当前文档。-->
  9. <style>
  10. h1 {
  11. color: red;/*不建议用important,一般用作调试。*/
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!--行内样式,优先级高于style标签设置的内部样式-->
  17. <h1 style="color: blue;">Hello World!</h1>
  18. <h1 style="color: green;">你好,PHP!</h1>
  19. <h1 style="color: yellow;">你好,CSS!</h1>
  20. </body>
  21. </html>


id > class > tag

2.前端组件样式模块化的原理与实现

模块化之前:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>模块化样式表</title>
  8. <style>
  9. header{
  10. min-height: 3em;
  11. background-color: #999;
  12. }
  13. main{
  14. min-height: 35em;
  15. background-color:lightblue;
  16. }
  17. footer{
  18. min-height: 4.2em;
  19. background-color: #ddd;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <header>页眉</header>
  25. <main>主体</main>
  26. <footer>页脚</footer>
  27. </body>
  28. </html>


模块化之后:

3.常用伪类选择器的使用方式

属性选择器语法:

class属性选择器语法:
元素[class = “”]{
样式
}

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>test</title>
  8. <style>
  9. li[class = "on"]{
  10. color: rebeccapurple;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <ul>
  16. <li class="on">1</li>
  17. <li>2</li>
  18. <li class="on">3</li>
  19. <li>4</li>
  20. <li class="on">5</li>
  21. </ul>
  22. </body>
  23. </html>


简化:

  1. li.on{
  2. color: rebeccapurple;
  3. }

id属性选择器语法:

  1. li[id = "on"]{
  2. color:blue;
  3. }
  4. 可以简化为:
  5. li.one{
  6. color:blue;
  7. }

并用伪类来模块化元素组件(例如表单或列表)
伪类选择元素语法:
选择任何一个:nth-of-type(n)
选择第一个:first-of-type
选择最后一个:last-of-type
选择倒数某一个:nth-last-of-type()
唯一子元素的元素:only-of-type

用伪类来模块化表单:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. /**导入表单样式**/
  10. @import url(../html/css/login.css);
  11. </style>
  12. </head>
  13. <body>
  14. <form action="" style="display: grid;gap:1rem" class="login">
  15. <input type="text" placeholder="username">
  16. <input type="password" placeholder="password">
  17. <input type="email" placeholder="email">
  18. <button>提交</button>
  19. </form>
  20. </body>
  21. </html>

css文件代码如下:

  1. /**用伪类选择器选择唯一的元素(button)**/
  2. .login :only-of-type{
  3. background-color:lightblue;/**设置背景颜色为蓝色**/
  4. }

效果图:

Correcting teacher:欧阳克欧阳克

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post