博主信息
博文 3
粉丝 0
评论 0
访问量 3032
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
选择器的使用和模块化组件思想
Nicole
原创
778人浏览过

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. }

效果图:

批改老师:欧阳克欧阳克

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学