批改状态:未批改
老师批语:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--<link rel="stylesheet" href="static/CSS/zuoye1style.css">-->
<title>演示CSS中的常用选择器</title>
<style>
/*标签选择器*/
ul {
border: 1px dashed blue;
margin: 0 auto;
padding-left: 0;
}
/*层级选择器:选择ul的后代元素*/
ul li {
list-style: none;
width: 50px;
height: 50px;
background-color: silver;
/*设置文本水平垂直居中*/
text-align: center;
line-height: 50px;
/*设置圆角度,50%或者宽度的一半都行*/
border-radius: 50%;
/*设置内联块,使多个块元素可以显示在一行中*/
display: inline-block;
/*增加左外边距*/
margin-left: 10px;
/*添加阴影,增加小球的美化效果*/
box-shadow: 2px 2px 1px #888888;
}
/*id选择器*/
#bg_red {
background-color: lightcoral;
}
/*类选择器*/
.bg_blue {
background-color: chartreuse;
}
/*属性选择器*/
li[id = bg_red]{
border: 2px solid darkmagenta;
}
/*群组选择器*/
#bg_red, .bg_blue {
border: 2px solid blue;
}
/*相邻选择器*/
/*第二个小球相邻的是第三个小球,可以用li,也可以用*号*/
#bg_red + * {
background-color: yellow;
}
/*兄弟选择器*/
/*第二个小球后面的所有同级兄弟元素都被选中*/
#bg_red ~ * {
background-color: -lightpink; /* 值前加-,令其失效 */
}
/*伪类,子元素选择器*/
ul :first-child {
background-color: red;
/*first-child,代表第一个元素,背景变为红色;*/
}
ul :last-child {
background-color: purple;
/*last-child,代表最后一个元素,背景变为紫色;*/
/* 因优先级问题,需要把前面的兄弟选择器注释掉 */
}
/*直接指定子元素,从1开始计数*/
ul :nth-child(5) {
background-color: cyan;
/*选择到第五个元素,指定背景色为青色;*/
}
ul :nth-child(9) {
background-color: lightcoral;
/*选择到第九个元素*/
}
/*:nth-last-child(n): 倒数第n个*/
ul :nth-last-child(3) {
background-color: blue;
/*倒数第三个元素背景色指定为蓝色;*/
}
ul :nth-last-child(5) {
background-color: purple;
/*倒数第五个元素背景色指定为紫色;*/
}
/*伪类:类型选择器*/
ul li:first-of-type {
background-color: lightblue;
color: white;
/* 第一个li类型的元素背景为浅蓝色,字体颜色为白色 */
}
ul li:last-of-type {
background-color: lightgreen;
color: red;
/* 最后一个li类型的元素背景改为浅绿色,字体为红色 */
}
ul li:nth-of-type(7) {
background-color: darkorchid;
color: white;
/* 选择第7个li类型的元素背景紫色,字体白色 */
}
/* 选中每个div中的第二个子元素背景设置为浅绿色, 李四、田七 */
div :nth-child(2) {
background-color: lightgreen;
}
/*先选中第一个div块, 在它内部再去选中第3个子素*/
div:first-of-type :nth-child(3){
background-color: -lightblue;
/*先让颜色失效,测试下面的方法*/
}
/*分析: 李四, 是第一个div中的第二个p标签内容, 而只有第一个div有二个p标签,第二个div是没有的*/
/* 所以以下选择器肯定会选中div中的第二个p元素 */
p:nth-of-type(2) {
background-color: yellow;
/*因为第二个div中没有第二个p标签,所以未选中第二个div中的p标签;*/
/*如果在第二个div中再添加一个p标签,重新刷新就会发现第二个p标签会被选中;*/
}
/* 注意: 类型伪类选择器应该是我们的首选,尽可能优先使用它 */
/* 如果要选择只有一个子元素且子元素为p,应该如何做?
此时, 第一个div有二个p元素,必须落选,所以只有第二个div中的p被选中*/
p:only-of-type {
background-color: pink;
/*背景色设置为粉色;*/
}
/*伪类:表单控件*/
form :enabled {
background-color: lightcyan;
/*当表单元素可用时,将背景设置成浅青色*/
}
/*将单选按钮中的文本前景色设置为红色,使用了伪类和相邻选择器*/
/*选择的过程是:先获取到当前被选中的按钮,再获取他的相邻子元素,也就是label标签,再将label的文本设置为红色*/
form :checked + label { /* 这里不用label, 用 '*' 号也可以 */
color: red;
}
/*当在控件中输入无效值时文本自动变为红色*/
/*例如在邮箱文本框中,如果输入的不是正确的邮箱格式,文本颜色自动变为红色,可以实时提示客户*/
form :invalid {
color: red;
}
/*设置控件获取到焦点时的样式*/
/*控件获取到焦点时,背景变为浅蓝色*/
form :focus {
background-color: lightblue;
}
/*设置鼠标悬停时的样式*/
button:hover {
width: 56px;
height: 28px;
background-color: crimson;
color: white;
}
</style>
</head>
<body>
<ul>
<li class="bg_blue">1</li>
<li id="bg_red">2</li>
<li class="bg_blue">3</li>
<li class = bg_blue>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
<!--演示伪类选择器中的子元素与类型选择器之间的区别与联系-->
<div>
<p>张三</p>
<li>李四</li>
<p>王五</p>
</div>
<div>
<p>赵六</p>
<li>田七</li>
</div>
<!--演示表单选择器-->
<form action="">
<label for="email">邮箱:</label>
<input type="email">
<label for="password">密码:</label>
<input type="password">
<input type="radio" id="week" name="save" value="7" checked><label for="week">保存一周</label>
<input type="radio" id="month" name="save" value="30"><label for="month">保存一月</label>
<button>登录</button>
</form>
</body>
</html>点击 "运行实例" 按钮查看在线实例
个人总结:
伪类中的子元素选择器与类型选择器的功能几乎是一样的;二种伪类选择器关注点不同, 子元素选择器的重点是 "child" 关键字上,关注的是子元素位置,而类型选择器关注点在 "type" 关键字上,重点是元素的类型;
nth-child(m): 关注位置;
nth-of-type(n): 除了关注关注位置外, 还需要元素的类型匹配
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号