当前位置: 首页 > news >正文

css选择器

目录

      • 1、基本选择器
        • (1)id选择器
        • (2)类选择器
        • (3)标签选择器
        • (4)逗号选择器
        • (5)*选择器(通配符选择器)
      • 2、包含选择器
        • (1)子代选择器
        • (2)后代选择器(空格选择器)
      • 3、属性选择器
      • 4、伪类选择器
      • 5、伪元素选择器
        • (1)::first-letter
        • (2):first-child
        • (3) :last-child

1、基本选择器

(1)id选择器

通过标签的id名称来选择标签

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title></title><style>#box{width: 300px;height: 300px;border: 1px solid red; }</style>
</head>
<body><div id="box"></div></body>
</html>

在这里插入图片描述

(2)类选择器

class选择器选择一个类名

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title></title><style>.msg{width: 200px;height: 100px;border: 1px solid green;}</style>
</head>
<body><!-- <div id="box"></div> --><div class="msg"></div><div class="msg"></div><div class="msg"></div>
</body>
</html>

在这里插入图片描述

(3)标签选择器

用标签名直接选择标签

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title></title><style>ul{list-style: none;}</style>
</head>
<body><ol><li>国内新闻</li><li>国内新闻</li><li>国内新闻</li></ol><ul><li>国外新闻</li><li>国外新闻</li><li>国外新闻</li></ul>
</body>
</html>

在这里插入图片描述

(4)逗号选择器

是一个复合选择器

       ul,ol{list-style: none;}

在这里插入图片描述

(5)*选择器(通配符选择器)

匹配所有标签

2、包含选择器

(1)子代选择器

用 > 表示父子关系

 ul.news > li {height: 50px;width: 400;border: 1px solid red;}

(2)后代选择器(空格选择器)

表示后代关系

        ul.news li {height: 50px;width: 400;border: 1px solid red;}

包含选择器的总代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title></title><style>ul{list-style: none;}ul.news {/*选择中的ul*/width: 400px;border: 1px solid red;min-height: 50px;}ul.news > li {height: 50px;width: 400;border: 1px solid red;}ul.news li {height: 50px;width: 400;border: 1px solid red;}</style>
</head>
<body><div class="news"></div><ul class="news"><li>这是列表1</li><li>这是列表2</li><li>这是列表3</li><li>这是列表4</li><li>这是列表5</li><li>这是列表6</li><li>这是列表7</li><li>这是列表8</li><ul><li>这是列表8</li><li>这是列表9</li><li>这是列表10</li></ul></ul>
</body></html>

3、属性选择器

 <style>[title]{/*将有title属性的颜色改为红色*/color: red;}</style>
        div[title] {color: red;}
/*将div标签里面的title属性的内容颜色改为红色*/

总代码:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>/* [title]{color: red;} */div[title] {color: red;}</style>
</head><body><div title="标题">这是一个div</div><div>这是一个div</div><div id="box">这是一个div</div><div>这是一个div</div><div>这是一个div</div><p title>这是有title的</p><p>这是没有title的</p>
</body></html>

在这里插入图片描述

4、伪类选择器

<style>.content{color: red;}a:link{/*未访问连接*/color: #333;text-decoration: none;/* 没有下划线 */}a:hover{/*鼠标移动到链接上*/color:mediumvioletred ;text-decoration: underline;}a:active{/*选中的链接被激活*/color: green;}a:visited{/*已访问的链接*/color: slateblue;}</style>

总代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.content{color: red;}a:link{color: #333;text-decoration: none;/* 没有下划线 */}a:hover{color:mediumvioletred ;text-decoration: underline;}a:active{color: green;}a:visited{color: slateblue;}</style>
</head>
<body><div class="content"><p>近日大学生返校高峰期</p><p>近日大学生返校高峰期</p><p>近日大学生返校高峰期</p><a href="#">连接1</a><a href="#">连接2</a><a href="#">连接3</a><a href="#">连接4</a><a href="#">连接5</a></div>
</body>
</html>

5、伪元素选择器

(1)::first-letter

 p::first-letter{/* 选择中一段的第一个单词 */text-transform: uppercase;/* 大写 */}

(2):first-child

        ul.news > li:first-child{/*选中ul标签下,news类的li标签的第一个color: green ;}

(3) :last-child

 ul.news > li:last-child{color:blue ;}

总代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title></title><style>p::first-letter{/* 选择中一段的第一个单词 */text-transform: uppercase;/* 大写 */}::first-line{/* 选中第一行 */color: red;}ul.news > li:first-child{color: green ;}ul.news > li:last-child{color:blue ;}</style>
</head>
<body><p>this is a book!!!<ul class="news"><li>这都是列表1</li><li>这都是列表2</li><li>这都是列表3</li><li>这都是列表4</li><li>这都是列表5</li></ul></p><p>this is a book!!!</p><p>石家庄伊能静 不是大家都说的就是真的吴刚回应整容风波颖儿手滑点赞付辛博井柏然相关微博俄罗斯李秀满 我的心好痛经纪人改口否认刘文正死讯王诗龄好瘦景甜方声明北京多个地铁口有人扫码送大鹅他和女友就要结婚了,竟没有一张合影男生买8个小笼包6个都没馅,当事人:开始只是以为皮很厚中国神秘千年古树,中间竟藏着一“小孩”,专家至今都无法解释香港教育局:将成立一所提供内地课程的学校国外一女子养了条彪悍的“宠物”,给它穿粉嫩公主服,还做美甲价值190万的主板被盗!民警迅速找回外媒:美国家安全委员会中国事务主任将离职,"与气球事件无关"穿着毛茸茸外套在草地里爬!玻利维亚一囚犯装扮成羊越狱失败英媒:为什么家里乱一点对你挺好?继拜登后哈里斯也在情人节发图"秀恩爱",网友发现图中"第三者"</p>
</body>
</html>

最后这段文字是我在网上随表找的新闻。

http://www.lryc.cn/news/9517.html

相关文章:

  • MyBatis详解2——增删改查操作
  • 最大连续子列和
  • 线性基 学习笔记
  • 算法-回溯算法-组合问题
  • ABAP中的Null值与space 以及 BW中ADSO的Key值
  • JavaScript库之Lodash常用方法
  • Kotlin新手教程二(Kotlin基本数据类型及基础语法)
  • git idea创建新分支,获取/合并主支代码的2个方法
  • CF1714A Everyone Loves to Sleep 题解
  • oracle官方下载历史版本JDK版本
  • 双击-jar包无法运行解决方法
  • 程序员的自我修养第七章——动态链接 (下)
  • 蓝桥杯刷题——基础篇(二)
  • PTA L1-049 天梯赛座位分配(详解)
  • Linux部分参数作用讲解
  • Java kafka
  • DBA之路---Stream数据共享同步机制与配置方法
  • CF1790E Vlad and a Pair of Numbers 题解
  • 漏洞预警|Apache Kafka Connect JNDI注入漏洞
  • 企业小程序开发步骤【教你创建小程序】
  • 刚性电路板的特点及与柔性电路板的区别
  • 扫码过磅+车牌识别,内蒙古蒙维过磅实现信息化管理
  • 蒙特卡洛计算圆周率
  • 生物信息场景下的用户需求
  • linux su(switch user)和sudo(superuser do)的区别?(sudo su与su的区别)
  • PostgreSQL的学习心得和知识总结(一百二十三)|深入理解PostgreSQL数据库开源扩展pg_dirtyread的使用场景和实现原理
  • ubuntu清理挖矿病毒
  • 【代码随想录训练营】【Day16】第六章|二叉树|104.二叉树的最大深度|559.n叉树的最大深度|111.二叉树的最小深度|222.完全二叉树的节点个数
  • transformer总结
  • dart flutter入门教程,开发手册 分享