批改状态:合格
老师批语:
样式冲突
!important>行内样式>内联样式>外联样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>样式冲突</title>
<!--引入外部样式表文件:style-->
<!--定义字体颜色为红色-->
<!--内联样式与行内样式什么都不写,字体颜色为红色-->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!--1.与样式声明的位置相关-->
<!--内部样式声明,仅适用当前文档-->
<style>
h2 {
color: cyan;
/*3重要性级别最高 !important,内联样式都会被覆盖*/
/*color: cyan!important;*/
/*2.与样式声明的顺序相关,相同样式规则之间互相层叠覆盖*/
color: gold; /*金色覆盖青色以最后一个颜色为准*/
}
</style>
<!--内联样式声明,仅适用于当前元素-->
<!--优先级: 内联 > 内部 > 外部 (特殊性越小,优先级别越低)-->
<!--<h2 style="color:blue">《CSS权威指南》</h2>-->
<h2 >《CSS权威指南》
</body>
</html>点击 "运行实例" 按钮查看在线实例
选择器的实际操作
id class的用法,以及本级元素下级元素的操作
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>8月15日作业</title>
<style type="text/css">
ul{
margin: 0px;
padding: 0px;
border: 1px solid black;/*设置黑色1px的边框*/
width: 600px;
background: #ffffff;
}
ul:after{
content: '';/*在子元素尾部添加为空内容的元素*/
display: block;/*转换成块元素*/
clear: both;/*清除2边的浮动*/
}
ul li{
list-style: none;/*去掉默认样式列表*/
width: 50px;/*设置高度为50px*/
height: 50px;
background: lightblue;/*设置背景颜色*/
float: left;/*向左浮动*/
line-height:50px;/*文本垂直居中*/
text-align: center;/*文本水平居中*/
border-radius: 50%;/*设置边框为圆角*/
border: 1px solid black;/*设置黑色1px的边框*/
box-shadow: 2px 2px 2px #888;/*设置阴影*/
margin-right: 2px;/*设置右边边距*/
}
/*id选择器*/
#one {
background-color: coral;
}
/*类选择器*/
.two {
background-color: gold;
}
/*属性选择器: 属性名*/
ul li[class]{
background-color: red;
}
/*属性选择器: 属性值*/
ul li[class="three"] {
background-color: grey;
}
/*属性选择器: 以指定属性值开头*/
ul li[class^="dog"] {
background-color: blue;
}
/*属性选择器: 以指定属性值结束*/
ul li[class$="pig"] {
background-color: red;
}
ul li[class*="o"] {
/*第1,2小球会变色,思考为什么1球没变色?*/
/*第1个小球是id,它的优先级大于标签属性选择器,改成class就会有效果*/
background-color: green;
}
/*后代选择器*/
body ul li {
border: 2px solid yellow;
}
/*子选择器*/
ul > li[class$="pig"] {
background-color: greenyellow;
}
/*相邻选择器*/
ul li[class$="pig"] ~ * {
/*选择当前元素之后的所有同级元素(不含当前)*/
/*以第一个出现的元素开始*/
background-color: black;
color: white;
}
/*相邻兄弟选择器*/
ul li[class$="pig"] + li{
background-color: pink;
color: black;
}
/*相邻兄弟选择器*/
ul li[class$="pig"] + li+li{
background-color: pink;
color: black;
}
</style>
</head>
<body>
<ul>
<li id="one">1</li>
<li class="two">2</li>
<li class="three">3</li>
<li class="dog cat pig">4</li>
<li class="dog">5</li>
<li class="pig">6</li>
<li class="o">7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
</body>
</html>点击 "运行实例" 按钮查看在线实例
伪元素的用法
访问前
a:link
访问后
a:visited
获取焦点时
a:focus
鼠标悬停时
a:hover
鼠标点击时
a:active
伪类选择器
选择第一个元素first-child
选择最后一个元素last-child
按索引指定nth-child(指定数字)
选择奇数或偶数nth-child(2n偶数 2n+1奇数 odd奇数 even偶数)
选择具有唯一子元素only child
指定类型唯一的子元素only-of-type
指定倒数选择nth-last-child(数字)
指定类型有2个且第二个nth-of-type(数字)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>8月15日作业</title>
<style type="text/css">
/*标签选择器*/
ul {
padding: 0;
margin: 0;
width: 500px;
border: 1px dashed #666;
padding: 10px 5px;
}
ul:after { /*子块撑开父块*/
content:''; /*在子元素尾部添加空内容元素*/
display: block; /*并设置为块级显示*/
clear:both; /*清除二边的浮动*/
}
ul li {
list-style: none; /*去掉默认列表项样式*/
float: left; /*左浮动*/
width: 40px; /*设置宽度*/
height: 40px; /*设置高度*/
line-height: 40px; /*文本垂直居中*/
text-align: center; /*文本水平居中*/
border-radius: 50%; /*设置边框圆角*/
box-shadow: 2px 2px 2px #888;
background: skyblue; /*背景色天蓝*/
margin-right: 5px; /*每个球之间的右外边距*/
}
/*群组选择器,注意要加逗号*/
h1, p {
font-size: 2rem;
font-weight: lighter;
margin: 0;
}
/*伪类选择器: 链接*/
a {
font-size: 2rem;
}
/*访问前*/
a:link {
color:red;
}
/*访问后*/
a:visited {
color: orange;
}
/*获取焦点时*/
a:focus {
color: purple;
}
/*获取焦点时*/
input:focus {
background-color: yellow;
}
/*鼠标悬停时*/
a:hover {
color: green;
}
/*鼠标点击时*/
a:active {
color: blue;
}
/*伪类选择器: 位置*/
/*选择集合中的第一个元素*/
ul li:first-child {
background-color: #efefef;
background-color: #efefef!important;
}
/*选择集合中的最后一个子元素*/
ul li:last-child {
background-color: red;
}
/*按索引选择指定的元素,注意从1开始计数*/
ul li:nth-child(5) {
background-color: red;
}
/* 选择所有的偶数小球变色 */
/* 2n偶数, even偶数, 2n+1奇数, odd奇数*/
ul li:nth-child(odd) {
background-color: purple!important;
}
/*伪类选择器: 根据子元素数量*/
/*选择具有唯一子元素的元素*/
ol:only-child {
background-color: lawngreen;
}
/* 选择指定类型的唯一子元素 */
ol li:only-of-type {
background-color: lawngreen;
}
/* 倒数选择指定位置的元素 */
ul li:nth-last-child(3) {
/*将倒数第3个小球变色,实际上第8号球*/
background-color: wheat!important;
}
/*选择指定父级的第二个<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="cat dog pig">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>css选择器大法</h1>
<p>css选择器非常重要,对于后面的jquery学习至关重要</p>
<a href="http://php.cn">PHP中文网</a>
获取焦点背景变黄色色
<br>
First name: <input type="text" name="firstname" /><br>
Last name: <input type="text" name="lastname" />
<ol>
<li>列表项1</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号