博主信息
博文 37
粉丝 0
评论 0
访问量 44357
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
1215作业
手机用户1607314868
原创
803人浏览过

css的三种引入方式

1.内部样式,仅对当前页面的元素生效,使用style标签

  1. <style>
  2. h1{background-color:red;}
  3. </style>

2.外部样式,适用于所有引入了这个css样式表的html文档

  1. <head>
  2. <link rel="stylesheet" href="style.css"></head>

3.行内样式,仅适用于当前页面中指定的元素,使用style属性

  1. <h1 style="color:teal">php中文网</h1>

实例:
实例

样式表的模块化

  1. <style>
  2. @import url(header.css);
  3. @import url(footer.css);
  4. </style>

1.将公共样式部分进行分离,并创建一些独立的样式表来保存它
2.使用@import指令将这些独立的公共样式表引入到指定的css文档或标签中

选择器

1.标签选择器
2.class选择器
3.id选择器
4.上下文选择器
5.结构伪类选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>伪类选择器</title>
  7. <style>
  8. /* 1.标签选择器,返回一组*/
  9. /** li{
  10. background-color:violet;
  11. }
  12. */
  13. /* 2.类选择器:返回一组*/
  14. li.on{
  15. background-color: blue;
  16. }
  17. /* 3.id选择器:返回一个 */
  18. #foo{
  19. background-color: violet;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <ul>
  25. <li id="foo">iteml1</li>
  26. <li class="on">iteml2</li>
  27. <li>iteml3</li>
  28. <li class="on">iteml4</li>
  29. <li class="on">iteml5</li>
  30. </ul>
  31. </body>
  32. </html>

上下文选择器

  • 后代选择器 所有层级
    ul li{ background-color: lightblue;}
  • 子元素选择器 仅父子层级
    body>ul>li{background-color: teal;}
  • 同级相邻选择器,仅选中与之相邻的第一个兄弟元素
    .start+li{background-color: lightgreen;}
  • 同级相邻选择器,仅选中与之相邻的后面所有兄弟元素
    .start~li{background-color: lightgreen;}

    结构伪类选择器

    匹配任意位置的元素 nth-of-type(an+b)
    a代表一个循环的大小数字,n是一个计数器(从0开始) b是偏移量
    an+b是 a 乘于 n 加 b
  1. ul li:nth-of-type(2n+1){
  2. background-color:red;
  3. }
  4. 2n-1 如下:
  5. 2*0+1=1
  6. 2*1+1=3
  7. 2*2+1=5
  8. ...
  9. ...
  10. 具体某个标签
  11. ul li:nth-of-type(3){}选中第三个
  12. 选择前三个
  13. ul li:nth-of-type(-n+3)
  14. 选择偶数行
  15. ul li:nth-of-type(even){};
  16. 选择奇数行
  17. ul li:nth-of-type(odd){};
  18. 选择第一个
  19. ul li:first-of-type{};
  20. 选择最后一个
  21. ul li:last-of-type{};
  22. 选择最后三个
  23. ul li:nth-last-of-type(-n+3){};

总结:
nth-of-type 是正序选择
nth-last-of-type 是倒序选择
last-of-type 是最后一个
first-of-type 是第一个

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学