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>
最后这段文字是我在网上随表找的新闻。