<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>常用选择器</title>
<style>
ul{
padding: 10px 5px;
margin: 0;
height: 50px;
width:550px;
border:1px dashed #666;
}
ul li{
list-style: none;
float: left;
width: 40px;
height: 40px;
line-height: 40px;
text-align:center;
border-radius: 30%; /*元素圆角,50%为正圆*/
box-shadow:2px 2px 2px #888;
background-color: aqua;
margin:5px;
}
#item1{
background-color: blanchedalmond;
}
.item2{
background-color: cornflowerblue;
}
/*属性选择器:属性名*/
ul li[class]{
background-color: cornsilk;
}
/*属性选择器:属性值*/
ul li[class="item2"]{
background-color: cornsilk;
}
/*属性选择器:属性值 开头查询 需要在class 后面添加 “^”*/
ul li[class^="cat"]{
background-color:brown;
}
/*属性选择器:属性值 结尾查询 需要在class 后面添加 “$”*/
ul li[class$="pig"]{
background-color:blue;
}
/*属性选择器:属性值 包含查询 需要在class 后面添加 “*”*/
ul li[class*="t"]{
background-color:yellowgreen;
}
/*后代选择器 明确第一级 和最后一级 即可选中 */
body ul li{
border:1px solid black;
}
/*子选择器 明确父子关系直接选中需要修改的 */
ul>li[class$="pig"]{
background-color: #888888;
}
/*相邻选择器 从当前元素往后选中(不包含当前) */
ul>li[class$="pig"]~*{
background-color: blueviolet;
}
/*相邻兄弟选择器 从当前元素往后选中(不包含当前) */
ul>li[class$="pig"]+li{
background-color: saddlebrown;
}
/*群组选择器*/
h1 p{
font-size:2rem;
font-weight: lighter;
margin: 0;
}
a{
font-size:2rem;
}
a:link /*访问前*/
{
color:orange;
}
a:visited /*访问后*/
{
color:red;
}
a:focus /*获取焦点的时候*/
{
color:purple;
}
a:hover /*鼠标悬停的时候*/
{
color:green;
}
a:active /*鼠标点击激活的时候*/
{
color:blue;
}
/*伪类选择器 位置 */
ul li:first-child{
background-color:lightseagreen!important; /*元素存在优先级问题 需要添加"!important"*/
}
ul li:last-child{
background-color:red; /*元素 不存在优先级 无需添加"!important"*/
}
ul li:nth-child(5){
background-color:red;
}
ul li:nth-child(odd){
/*偶数:even 奇数:odd */
background-color:plum;
}
ul li:nth-child(even){
background-color:dodgerblue;
}
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("http://mpic.tiankong.com/fbd/4d4/fbd4d4cafaea7a9a015f580c1d0179e8/640.jpg");
}
</style>
</head>
<body>
<ul>
<li id="item1">1</li>
<li class="item2">2</li>
<li class="cat doy 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>
</p>
<a href ="https://www.php.cn">PHP中文网</a>
<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号