博主信息
博文 34
粉丝 0
评论 0
访问量 29211
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
元素样式及选择器
OC的PHP大牛之路
原创
493人浏览过

元素的样式

颜色、字体、背景可继承
优先级:自定义>继承>默认

  1. 内联样式:style属性,适用于当前特定的某个元素

    1. <body>
    2. <h1 style="color:red">xxx</h1>
    3. <h1 style="color:red">xxx</h1>
    4. <h1 style="color:red">xxx</h1>
    5. </body>
  2. 文档样式:<style></style>标签,适用于当前html文档

    1. <body>
    2. <h1 >xxx</h1>
    3. <h1 >xxx</h1>
    4. <h1 >xxx</h1>
    5. <style>
    6. h1{
    7. color: red;
    8. }
    9. </style>
    10. </body>
  3. 外部样式:<link rel="stylesheet" href="css文件">首选

    1. <link rel="stylesheet" href="01.css">
    2. </head>
    3. <body>
    4. <h1 >xxx</h1>
    5. <h1 >xxx</h1>
    6. <h1 >xxx</h1>
    7. </body>

选择器

基本选择器

  1. 标签选择器
    标签选择器
    html:
    1. <link rel="stylesheet" href="01.css">
    2. </head>
    3. <body>
    4. <h1 >xxx1</h1>
    5. <h1 >xxx2</h1>
    6. <h1 >xxx3</h1>
    7. <h1 >xxx4</h1>
    8. <h1 >xxx5</h1>
    9. <h1 >xxx6</h1>
    10. </body>
    css:
    1. /* 标签选择器 */
    2. h1{
    3. color: red;
    4. }

2.属性选择器id# class.
属性选择器
html

  1. <link rel="stylesheet" href="01.css">
  2. </head>
  3. <body>
  4. <h1>xxx1</h1>
  5. <h1 title="welcome">xxx2</h1>
  6. <h1 class="three">xxx3</h1>
  7. <h1 id="active">xxx4</h1>
  8. <h1>xxx5</h1>
  9. <h1>xxx6</h1>
  10. </body>

css

  1. /* 标签选择器 */
  2. h1{
  3. color: red;
  4. }
  5. /* 属性选择器 */
  6. h1[title="welcome"]{
  7. color:green;
  8. }
  9. /* h1[class="three"]{ */
  10. /* color: blue; */
  11. /* } */
  12. h1.three{
  13. color: blue;
  14. }
  15. /* h1[id="active"]{ */
  16. /* color: blueviolet; */
  17. /* } */
  18. h1#active{
  19. color: blueviolet;
  20. }

3.群组选择器,
群组选择器
html

  1. <link rel="stylesheet" href="01.css">
  2. </head>
  3. <body>
  4. <h1>xxx1</h1>
  5. <h1 title="welcome">xxx2</h1>
  6. <h1 class="three">xxx3</h1>
  7. <h1 id="active">xxx4</h1>
  8. <h1 id="hello">xxx5</h1>
  9. <h1 class="world">xxx6</h1>
  10. </body>

css

  1. /* 标签选择器 */
  2. h1{
  3. color: red;
  4. }
  5. /* 属性选择器 */
  6. h1[title="welcome"]{
  7. color:green;
  8. }
  9. /* h1[class="three"]{ */
  10. /* color: blue; */
  11. /* } */
  12. h1.three{
  13. color: blue;
  14. }
  15. /* h1[id="active"]{ */
  16. /* color: blueviolet; */
  17. /* } */
  18. h1#active{
  19. color: blueviolet;
  20. }
  21. /* 群组选择器 */
  22. h1#hello,h1.world{
  23. background-color: aqua;
  24. }

4.通配选择器*
通配选择器
!important用于临时提权,用于调试
html

  1. <link rel="stylesheet" href="01.css">
  2. </head>
  3. <body>
  4. <h1>xxx1</h1>
  5. <h1 title="welcome">xxx2</h1>
  6. <h1 class="three">xxx3</h1>
  7. <h1 id="active">xxx4</h1>
  8. <h1 id="hello">xxx5</h1>
  9. <h1 class="world">xxx6</h1>
  10. <h2>大家看这里</h2>
  11. </body>

css

  1. /* 标签选择器 */
  2. h1{
  3. color: red;
  4. }
  5. /* 属性选择器 */
  6. h1[title="welcome"]{
  7. color:green;
  8. }
  9. /* h1[class="three"]{ */
  10. /* color: blue; */
  11. /* } */
  12. h1.three{
  13. color: blue;
  14. }
  15. /* h1[id="active"]{ */
  16. /* color: blueviolet; */
  17. /* } */
  18. h1#active{
  19. color: blueviolet;
  20. }
  21. /* 群组选择器 */
  22. h1#hello,h1.world{
  23. background-color: aqua;
  24. }
  25. /* 通配选择器 */
  26. body *{
  27. background-color: grey !important;
  28. }

上下文选择器

1.子元素>
html

  1. <link rel="stylesheet" href="demo.css">
  2. </head>
  3. <body>
  4. <ul class="list">
  5. <li class="item">item1</li>
  6. <li class="item">item2</li>
  7. <li class="item">item3</li>
  8. <li class="item">item4</li>
  9. <li class="item">item5</li>
  10. <li class="item">item6</li>
  11. <li class="item">item7</li>
  12. <li class="item">item8</li>
  13. </ul>
  14. </body>

css

  1. /* 1.子元素 */
  2. .list>.item{
  3. border: 1px solid;
  4. }

2.后代元素空格
html

  1. <link rel="stylesheet" href="demo.css">
  2. </head>
  3. <body>
  4. <ul class="list">
  5. <li class="item">item1</li>
  6. <li class="item">item2</li>
  7. <li class="item">item3
  8. <ul>
  9. <li class="item">item3-1</li>
  10. <li class="item">item3-2</li>
  11. <li class="item">item3-3</li>
  12. </ul>
  13. </li>
  14. <li class="item">item4</li>
  15. <li class="item">item5</li>
  16. <li class="item">item6</li>
  17. <li class="item">item7</li>
  18. <li class="item">item8</li>
  19. </ul>
  20. </body>

css

  1. /* 1.子元素 */
  2. .list>.item{
  3. border: 1px solid;
  4. }
  5. /* 2.后代元素 */
  6. .list .item{
  7. border: 1px solid;
  8. }

3.相邻兄弟+
html

  1. <link rel="stylesheet" href="demo.css">
  2. </head>
  3. <body>
  4. <ul class="list">
  5. <li class="item">item1</li>
  6. <li class="item">item2</li>
  7. <li class="item">item3</li>
  8. <li class="item">item4</li>
  9. <li class="item five">item5</li>
  10. <li class="item">item6</li>
  11. <li class="item">item7</li>
  12. <li class="item">item8</li>
  13. </ul>
  14. </body>

css

  1. /* 1.子元素 */
  2. .list>.item{
  3. border: 1px solid;
  4. }
  5. /* 2.后代元素 */
  6. .list .item{
  7. border: 1px solid;
  8. }
  9. /* 3.相邻兄弟 */
  10. .list>.item.five {
  11. background-color:aqua;
  12. }
  13. .list>.item.five + *{
  14. background-color: aquamarine;
  15. }

4.所有兄弟~
html

  1. <link rel="stylesheet" href="demo.css">
  2. </head>
  3. <body>
  4. <ul class="list">
  5. <li class="item">item1</li>
  6. <li class="item">item2</li>
  7. <li class="item">item3</li>
  8. <li class="item">item4</li>
  9. <li class="item five">item5</li>
  10. <li class="item">item6</li>
  11. <li class="item">item7</li>
  12. <li class="item">item8</li>
  13. </ul>
  14. </body>

css

  1. /* 1.子元素 */
  2. .list>.item{
  3. border: 1px solid;
  4. }
  5. /* 2.后代元素 */
  6. .list .item{
  7. border: 1px solid;
  8. }
  9. /* 3.相邻兄弟 */
  10. .list>.item.five {
  11. background-color:aqua;
  12. }
  13. .list>.item.five + *{
  14. background-color: aquamarine;
  15. }
  16. /* 4.所有兄弟 */
  17. .list>.item.five ~ *{
  18. background-color: blueviolet;
  19. }

选择器的权重

(0,0,0)表示选择器因子的权重,
对应(百位<id>,十位<class>,个位<tag>)

增加权重的方法:
1.增加标签(可以是父级标签),这样就可以不依
序,如html body h1{xxx}

2.增加代码,如.title{xxx}可增加至.title.go {xxx}

注:尽量少用或者不用id,权重过大

批改老师:PHPzPHPz

批改状态:合格

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

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

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