批改状态:未批改
老师批语:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>css中常用的选择器</title>
<style>
/*标签选择器*/
ul {
width: 500px;
padding:0;
margin: 0;
border: :1px dashed #666;/*dashed是虚线;solid是实线*/
}
ul:after {/*子块撑开父块*/
content:'';/*在子元素尾部添加空内容元素*/
display: block;/*设置为块级元素*/
clear: both;/*清楚二边的浮动*/
}
ul li {
list-style: none;/*去掉默认列表项样式*/
float: left;/*向左浮动*/
width:40px;
height: 40px;
line-height: 40px;/*文本垂直居中*/
text-align:center;/*文本水平居中*/
background: skyblue;
border-radius: 50%;/*设置边框圆角,百分50%就是圆形*/
margin-right: 5px;/*设置每个球之间的右外边距*/
box-shadow: 2px 2px 2px #888;/*设置阴影及阴影颜色*/
}
/*id选择器*/
#item1 {
background-color: coral;
/*background-color只能设置背景色,background不仅可以设置背景色,还可以设置图片为为背景 body {background: url("images/1.jpg");}*/
}
/*类选择器*/
.item2 {background-color: gold;}
/*属性选择器:通过属性名*/
ul li[class] {background-color: blueviolet;}
/*属性选择器:通过属性值*/
ul li[class="item2"] {background-color: grey;}
/*属性选择器:以指定属性值开头*/
ul li[class^="dog"] {background-color: brown;}
/*属性选择器:以指定属性值结尾*/
ul li[class$="cat"] {background-color: red;}
/*属性选择器:属性值中包含指定字符串*/
ul li[class*="em"] {background-color: green;}/*如果把class改成id,思考为什么1球没变色?*/
/*第1个小球是id,它的优先级大于标签属性选择器,在green后面添加上!important就可以了*/
/*后代选择器*/
body ul li{border: 1px solid black;}
/*子选择器*/
ul>li[class$="cat"] {background-color: greenyellow;}/*只能识别属性值的尾部元素*/
/*相邻选择器*/
ul li[class*="pig"]~*{background-color: black;color: white;}
/*相邻兄弟选择器*/
ul li[class$="cat"] + li {background-color: pink;color: black;}
/*群组选择器*/
h1,p {font-size: 2rem;font-weight: lighter;margin: 0;}
/*伪类选择器: 链接*/
a {font-size: 2rem;}
/*访问前*/
a:link{coloer:red;}
/*访问后*/
a:visited{coloer:orange;}
/*获取焦点时*/
a:focus{color:purple;}
/*鼠标悬停时*/
a:hover {color: green;}
/*鼠标点击时*/
a:active {color: blue;}
/*伪类选择器: 根据位置*/
/*1.选择集合中的第一个元素*/
ul li:first-child{
background-color: #efefef;/*优先级低于之前的,所以无法显示*/
background-color: #efefef!important;/*后面加个!important提升优先级即可*/
}
/*2.选择集合中的最后一个元素*/
ul li:last-child{background-color: red;}
/*3.按索引选择指定的元素,注意从1开始计数,对第5个元素进行样式设置*/
ul li:nth-child(5) {background-color: red;}
/*4.选择所有的偶数小球变色 */
/* 2n偶数, even偶数, 2n+1奇数, odd奇数*/
ul li:nth-child(even) {background-color: purple!important;}
/*5.伪类选择器: 根据子元素数量*/
/*5.1选择具有唯一子元素的元素*/
ol :only-child{background-color: lawngreen;}
/*5.2选择指定类型的唯一子元素 ,同5.1的效果一样*/
ol li:only-of-type{background-color: lawngreen;}
/*6.倒数选择指定位置的元素 */
ul li:nth-last-child(3) {/*将倒数第3个小球变色,即第8号球*/
background-color: wheat!important;}
/*7.选择指定父级的第二个<li>子元素*/
ol li:nth-of-type(2) {background-color: wheat;}
/*选择页面中内容为空的元素*/
:empty {width: 220px;height: 271px;background-color: coral;}
/*在伪类中添加文字内容*/
:empty:after {content: '我在这里哦!';}
:empty:before{content: url("monkey.png");}
/*通过伪类添加的元素,默认插入的元素为行内元素,不支持宽度设定,如果一定要设置可以通过背景图片实现*/
</style>
</head>
<body>
<ul>
<li id="item1">1</li>
<li class="item2">2</li>
<li class="dog pig cat">3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
<h1>选择器大法</h1>
<p>css选择器非常重要,对于后面的jquery学习至关重要</p>
<a href="http://www.php.cn">php中文网</a>
<ol>
<li>列表项</li>
<!--现在给ol添加一个子元素<p>,有二个子元素了,所以子元素不再唯一,如何才能选中唯一的li元素呢?only-of-type-->
<p>我是一个段落</p>
</ol>
<ol>
<li>列表项1</li>
<li>列表项2</li>
<li>列表项3</li>
</ol>
<ol>
<li>列表项1</li>
<li>列表项2</li>
<li>列表项3</li>
<li>列表项4</li>
</ol>
<!-- 空区块 -->
<div></div>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号