本次博客带领大家学习CSS中的选择器,如基本选择器、层次选择器、结构伪类选择器和属性选择器。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
/*标签选择器,会选择到页面上所有的这个标签*/
h1{
color: #000000;
}
p{
font-size: 80px;
}
style>
head>
<body>
<h1>学Javah1>
<h1>学Javah1>
<p>上B站p>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
/*类选择器的格式 .class的名称{}
好处,可以多个标签归类,是同一个class,可以复用
*/
.ld{
color: #5afa46;
}
.kkk{
color: #5cc1fb;
}
style>
head>
<body>
<h1 class="ld">标题1h1>
<h1 class="kkk">标题2h1>
<h1 class="ld">标题3h1>
<p class="ld">段落p>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
/* id选择器 : id必须保证全局唯一!
#id名称{}
不遵循就近原则,固定的
id选择器 > class 选择器 > 标签选择器
*/
#ld{
color: #5cc1fb;
}
.kkk{
color: #f77f5a;
}
h1{
color: #ff75f6;
}
style>
head>
<body>
<h1 id="ld" class="kkk">标题1h1>
<h1 class="kkk" >标题2h1>
<h1 class="kkk">标题3h1>
body>
html>
/*后代选择器*/
body p{
background: red;
}
/*子选择器*/
body>p{
background: blue;
}
/*相邻兄弟选择器:只有一个,相邻(向下)*/
.active + p{
background: brown;
}
/*通用兄弟选择器:当前选中元素的向下的所有兄弟元素*/
.active~p{
background: #ff75f6;
}
伪类用于定义元素的特殊状态。
伪类的语法:
selector:pseudo-class {
property: value;
}
/*选中p1 定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效。
*/
p:nth-child(2){
background: blueviolet;
}
/*
选中父元素下的p元素的第二个,类型。
*/
p:nth-of-type(2){
background: aquamarine;
}
/*鼠标悬停的颜色*/
a:hover{
background: darkorange;
}
CSS [attribute] 选择器
[attribute] 选择器用于选取带有指定属性的元素。
实例
a[target] {
background-color: yellow;
}
CSS [attribute=“value”] 选择器
[attribute=“value”] 选择器用于选取带有指定属性和值的元素。
实例
a[target="_blank"] {
background-color: yellow;
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width:50px;
border-radius:10px;
background: #ff75f6;
text-align: center;
color: #ff304f;
text-decoration: none;
margin-right:5px;
font:bold 20px/50px Arial;
}
/*[]里面可以是属性名,也可以是属性名 = 属性值(正则)
= 绝对等于
*= 包含这个元素
^= 以这个开头
$= 以这个结尾
*/
/*存在id属性的元素*/
a[id]{
background-color: #5afa46;
}
/*id=first的元素*/
a[id=first]{
background: coral;
}
/*class中有links的元素*/
a[class*="links"]{
background: brown;
}
a[href^=http]{
background: darkgray;
}
a[href$=pdf]{
background: indigo;
}
style>
head>
<body>
<p class="demo">
<a href="http://www.baidu.com" class="links item first" id="first">1a>
<a href="" class="links item active" target="_blank" title="test">2a>
<a href="images/123.html" class="links item">3a>
<a href="images/123.png" class="links item">4a>
<a href="images/123.jpg" class="links item">5a>
<a href="abc" class="links item">6a>
<a href="/a.pdf" class="links item">7a>
<a href="/abc.pdf" class="links item">8a>
<a href="abc.doc" class="links item">9a>
<a href="abcd.doc" class="links item last">10a>
p>
body>
html>