标签{ 属性:值; }
注意:类名的第一个字符也不能使用数字。php
单类名调用:class="类名";
css
多类名调用:class="类名1 类名2 ...";
html
<!DOCTYPE html> <html> <head> <style> .center{ text-align:center; } .col{ color:red; } .font{ font-size:18px; font-family:"Microsoft YaHei"; } </style> </head> <body> <h1 class="center">class 选择器</h1> <p class="center col">我是一个段落。</p> <p class="center font">我是另外一个段落。</p> </body> </html>
<!DOCTYPE html> <html> <head> <style> .center{ color:blue; } p.center{ text-align:center; } p.col{ color:red; } .font{ font-size:18px; font-weight:bold; font-family:"Microsoft YaHei"; } </style> </head> <body> <h1 class="center col">class 选择器</h1> <p class="center col">我是一个段落。</p> <p class="center col font">我是另外一个段落。</p> <h2 class="center">h2 标题</h2> </body> </html>
注意: id 属性不能以数字开头。css3
#id{ 属性:值; }
*{ 属性:值; }
选择器1选择器2{ 属性:值; }
选择器1,选择器2{ 属性:值; } // h1, h2, h3, h4, h5, h6, p{color:red;}
先代选择器 后代选择器{ 属性:值; }
父选择器 > 子选择器{ 属性:值; }
只做用于父元素内的[直接子元素];浏览器
若是不但愿选择任意的后代元素,而是只选择某个元素的子元素,那么就使用子元素选择器框架
<head> <style> div>a{ color:red; } </style> </head> <body> <div> <a href="">div 中第一个子元素 a,显示为红色</a> <p>div 中第二个子元素 p<br/> <a>p 元素的子元素 a,该元素是 div 元素的孙元素</a> </p> <a href="">div 中第三个子元素 a,显示为红色</a> </div> </body>
伯选择器 + 仲选择器{ 属性:值; }
做用于紧接在伯元素后的[仲元素];字体
若是须要选择紧接在另外一个元素后的元素,并且两者有相同的父元素,就可使用相邻兄弟选择器网站
<head> <style> div+p{ background-color:red; } </style> </head> <body> <p>body 的第一个子元素 p</p> <div>body 的第二个子元素 div <p>div 中第一个子元素 p</p> <div> <p>div 中第二个子元素 div 的子元素 p</p> </div> <p>div 中第三个子元素 p,是 div 的相邻兄弟元素,相同父级 div</p> //红 </div> <p>body 的第三个子元素 p,是 div 的相邻兄弟元素,相同父级 body</p> // 红 <p>body 的第四个子元素 p,是 div 的普通兄弟元素</p> </div> </body>
<head> <style> div~p{ background-color:red; } </style> </head> <body> <p>body 的第一个子元素 p</p> <div>body 的第二个子元素 div <div>div 元素下的第一个子元素 div</div> <p>div 的相邻兄弟元素</p> <p>div 的普通兄弟元素</p> <p>div 的普通兄弟元素</p> </div> <p>body 下 div 的相邻兄弟元素</p> <p>body 下 div 的普通兄弟元素</p> <p>body 下 div 的普通兄弟元素</p> </body>
[属性]{ 属性:值; }
<head> <style> [title]{ color:red; } </style> </head> <body> <h1>h1 标题不带有 title 属性</h1> <h2 title="标题">h2 能够设置样式</h2> <a href="#" title="连接">超连接能够设置样式</a> </body>
标签[属性=值]{ 属性:值; }
<head> <style> [title=Hello]{ color:blue; } </style> </head> <body> <h1 title="Hello world">h1 标题 title="Hello world"</h1> <h2 title="Hello">h2 能够设置样式</h2> <a href="#" title="Hello">超连接能够设置样式</a> </body>
<head> <style> input[type="text"]{ width:150px; display:block; margin-bottom:5px; background-color:yellow; } input[type="button"]{ width:120px; margin-top:5px; background-color:green; } </style> </head> <body> <form name="input" action="demo.php" method="get"> 用户名:<input type="text" name="user" placeholder="请输入登陆名"> 昵 称:<input type="text" name="name" placeholder="请输入角色名"> <input type="button" value="查询"> </form> </body>
选择器:伪类{ 属性:值; }
<a>
超连接伪类:a:link
:未访问的连接;url
a:visited
:已访问的连接;spa
a:hover
:鼠标移动到连接;
a:active
:鼠标点击时的链接;
a:hover
必须跟在 a:link
和 a:visited
后面。
a:active
必须跟在 a:hover
后面。
<head> <style> a.tint:hover{ color:red; } </style> </head> <body> <a href="#">超连接</a> <a class="tint" href="#">超连接</a> </body>
:first-child
:第一个子元素;
:last-child
:最后一个子元素;
:nth-child(n)
:第n个元素(n=1,2,3...);
:nth-last-child(n)
:倒数第n个元素(n=1,2,3...);
[n=odd
:奇数 | n=even
:偶数]
注意:
- 不是第一个HTML标签,而是第一个HTML元素
- html元素:文本,图像,连接;
<head> <style> p:first-child{ font-weight:bold; color:blue; } </style> </head> <body> <p>第一个 p 元素</p> // 蓝 <p>第二个 p 元素</p> <p>第三个 p 元素</p> </body>
/*:target 选择器用于选取当前活动的目标元素*/ :target{ 属性:值; }
标签::伪元素{ 属性:值; }
:first-line
:向文本的首行设置特殊样式;
注意::first-line 伪元素只能用于块级元素,下面的属性可应用于 :first-line 伪元素
①、font
②、color
③、background
④、line-height
⑤、clear
⑥、vertical-align
⑦:text-decoration
⑧:text-transform
⑨、letter-spacing
⑩、word-spacing
:first-letter
:向文本的首字母设置特殊样式;
注意::first-letter 伪元素只能用于块级元素,下面的属性可应用于 "first-letter" 伪元素
①、font、color、background、line-height、clear、vertical-align (only if "float" is "none")、text-decoration、text-transform
以上8个属性和 :first-line 伪元素相同,除了 letter-spacing 和 word-spacing 以外。
②、float
③、margin
④、padding
⑤、border
:before
:在标签以前添加 content:新内容
;
标签::before{ content:新内容; // constent:none 默认 // constent:'string' 插入文本 // constent:attr(属性) 插入标签属性值 // constent:url(路径) 插入一个外部资源 }
:after
:在标签以后添加 content:新内容
;标签::after{ content:新内容; }
:selection
:选中区域;<head> <style> p:first-letter{ color:red; font-size:20px; } p:first-line{ color:blue; text-decoration:underline; } </style> </head> <body> <h1>The best things in life are free, like hugs, smiles, friends, kisses, family, love and good memories.</h1> <p>The moon belongs to everyone,best things in life they're free,stars belong to everyone,they cling there for you and for me.Love can come to everyone,best things in life they're free,all of the good things,every one of the better things.</p> <p>The best things in life are free, and the second best things are very, very expensive.</p> </body>
①、表明内联样式,如 <p style="color:red"></p>
,权值为 1000。
②、表明 ID 选择器,如 #content
,权值为 100。
③、表明类,类选择器以及伪类和属性选择器,如 .main
,权值为 10。
④、表明类型选择器,标签和伪元素选择器,如 div p
,权值为 1。
p{ /* 权值为 1 */ color:red; } p span{ /* 权值为 1+1=2 */ color:green; } .main{ /* 权值为 10*/ font-size:14px; } div p .main{ /* 权值为 1+1+10=12 */ color:purple; } #footer{ /* 权值为 100 */ color:gray; } #footer .note p{ /* 权值为 100+10+1=111 */ color:white; }
<head> <style> p{ color:red!important; // 权值最高,p 元素显示红色 } .demo{ color:green; } </style> </head> <body> <p class="demo">在实际的网站开发中,有些特殊的状况须要为某些样式设置具备最高权值,这时候咱们可使用 !important 来解决。</p> </body>