1、id选择器
任何的标签都可以有id值,但是在同一个html文件当中,id的值是唯一的,所以说我们可以使用id值来定位到对应的元素。
- 语法:
-
- #id的名字{
- 属性1:值1;
- 属性2:值2;
- 属性3:值3;
- ...
- 属性n:值n;
- }
2、标签选择器
只要是改标签,就会有对应的效果
- 语法:
-
- 标签名{
- 属性1:值1;
- 属性2:值2;
- 属性3:值3;
- ...
- 属性n:值n;
- }
3、类选择器
只要是在该分组当中,就会有该分组当中设定的效果
- 语法:
-
- .类名{
-
- }
代码练习:
-
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>id选择器title>
-
-
-
-
- <style>
- /* 标签选择器:是该标签 都会有对应的效果
- 标签名{
- 属性1:值1;
- 属性2:值2;
- 属性3:值3;
- ...
- 属性n:值n;
- }
- */
- /*
- 类选择器:只要在该组当中,就有了组当中设定的效果
- */
- /*
- .类名{
-
- }
- */
- .cls{
- color: aqua;
- }
- input{
- color: crimson;
- background-color: aquamarine;
- /* 改变输入的东西的颜色 */
- }
- #id1{
- background: #000;
- }
- style>
-
- head>
- <body>
- <input class="cls"><br>
- <p class="cls">你p><br>
-
- <input class="cls" id="id1"><br>
- <p>haop><br>
- <input>
- body>
- html>
-
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>testtitle>
- head>
- <style>
- input{
- color: red;
- }
- .cls{
- color: green;
- }
- #input{
- color: blue;
- }
- style>
- <body>
- <input class="cls" id="input">
-
-
-
-
-
-
-
-
- body>
- html>
4、派生选择器(后代选择器)
比如说:给div当中的p标签的文字变为red
- /* div p{} 可以理解为div当中的p标签 */
- div p{
- color: aqua;
- }
代码练习:
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>派生选择器(后代选择器)title>
- head>
- <style>
- /* 给p标签的文字变为red */
- p{
- color: red;
- }
- /* 给div当中的p标签的文字变为red */
-
- /* div p{} 可以理解为div当中的p标签 */
- div p{
- color: aqua;
- }
-
- /* */
- div input{
- background-color: darkkhaki;
- /* 1和2 input都会改变 */
- }
- style>
- <body>
-
- <div>
- <p>test01p>
- <p>test02p>
-
- <input><br>
-
- <h1>
-
- <input>
- h1>
- div>
-
- <p>test03p>
- <p>test04p>
- body>
- html>
5、子选择器
子选择器:代表的就是直接的孩子
/* 父亲 > 孩子 */
- /* div > p{
- color:red;
- } */
- /* div的孩子p设置为red*/
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>子选择器title>
- head>
- <style>
- /* 子选择器:直接的孩子 */
- /* 给p标签的文字变为red */
- /* p{
- color: red;
- } */
- /* 给div当中的p标签的文字变为red */
-
- /* div p{} 可以理解为div当中的p标签 */
- /* div > p{
- color:red;
- } */
- /* div的孩子p设置为red*/
-
- /* 父亲 > 孩子 */
- div > input{
- background-color:green;
- }
- /* 1input会改变 */
- style>
- <body>
-
- <div>
- <p>test01p>
- <p>test02p>
-
- <input><br>
-
- <h1>
-
- <input>
- h1>
- div>
-
- <p>test03p>
- <p>test04p>
- body>
- html>
6、通用选择器
/* * {
color: aqua;
} */
/* 当前页面中的所有的文字颜 色都相同 */
div *{
color: rebeccapurple;
}
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>通用选择器title>
- head>
-
- <style>
- /* * {
- color: aqua;
- } */
- /* 当前页面中的所有的文字颜 色都相同 */
- div *{
- color: rebeccapurple;
- }
- style>
- <body>
-
- <div>
- <p>test01p>
- <p>test02p>
-
- <input><br>
-
- <h1>
-
- <input>
- h1>
- div>
-
- <p>test03p>
- <p>test04p>
-
- <span>你好span><br>
- <h2>中国h2>
-
- body>
- html>
7、群组选择器
/**
* 需求:
* input、h2、span的文字
* color:red
*/
- input,span,h2{
- color: chartreuse;
- }
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>群组选择器title>
- <style>
- /**
- * 需求:
- * input、h2、span的文字
- * color:red
- */
- /* input{
- color: red;
- }
- h2{
- color:red;
- }
- span{
- color: red;
- } */
- input,span,h2{
- color: chartreuse;
- }
- style>
- head>
- <body>
- <div>
- <p>test01p>
- <p>test02p>
-
- <input><br>
-
- <h1>
-
- <input>
- h1>
- div>
-
- <p>test03p>
- <p>test04p>
-
- <span>zhongspan>
- <h2>guoh2>
- body>
- html>
8、相邻选择器
p+h1{
background-color: aqua;
}
/* 代表的是和p标签相邻的h1标签 */
对h1起作用,对p不起作用
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>相邻选择器title>
- <style>
- p+h1{
- background-color: aqua;
- }
- /* 代表的是和p标签相邻的h1标签 */
- style>
- head>
- <body>
- <h1>你好h1>
-
-
- <p>我爱你p>
- <h1>他h1>
-
- <p>ni1p>
- <h1>大家h1>
- body>
- html>
9、属性选择器
[属性] 也可以包含属性值
[name]{
background-color: aquamarine;
}
或者
/* [type="text"]{
color: red;
} */
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>属性选择器title>
- head>
- <style>
- /**
- * [属性] 也可以包含属性值
- */
- /* [type]{
- color: red;
- } */
- /* [type="text"]{
- color: red;
- } */
- /* [type]{
- color: red;
- } */
- /* 标签上写有name属性的元素 */
- [name]{
- background-color: aquamarine;
- }
- style>
- <body>
-
- <input name="lname" id="">
- <br>
- <input name="rname" id="">
- <br>
- <input type="passwd" id="">
- <br>
- <input type="text" id="">
- <br>
- <input type="button" id="" value="按钮">
- <br>
- body>
- html>