标签:lis link sel active 不同类 The top 基本 visit
下面所有的基本选择器都是在demo1.html的基础上面添加部分样式实现的。
demo1.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> .demo{ width:400px; height:50px; border:1px solid #ccc; padding:10px; } ul{ height:30px; } li{ text-decoration: none; list-style: none; float:left; height:20px; line-height: 20px; width:20px; border-radius:10px; text-align:center; background:#f36; color:green; margin-right:5px; } </style> </head> <body> <div class="demo"> <ul class="clearfix"> <li id="first" class="first">1</li> <li class="active important">2</li> <li class="important items">3</li> <li class="important">4</li> <li class="items">5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li id="last" class="last">10</li> </ul> </div> </body> </html>
*{margin:0;padding:0;}
.demo *{border:1px solid blue;}
li{background-color:grey;color:orange;}
.important{font-weight:bold;color:yellow;}
li.important{font-weight:bold;color:yellow;}
#first{color:blue;}
.demo li{color:blue;}
ul>li{color:yellow;}
li + li{border:1px solid blue;}
li ~ li{border:1px solid blue;}
.first,.last{background-color:red;}
常见的属性选择器包括以下几类:
下面所有的属性选择器都是在demo2.html的基础上加入相关样式实现的。
demo2.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> .demo{ width:300px; height:40px; border:1px solid #ccc; padding:10px; } .demo a{ float:left; display:block; height:20px; line-height:20px; width:20px; border-radius:10px; text-align:center; background:#f36; color:green; margin-right:5px; text-decoration:none; } </style> </head> <body> <div class="demo clearfix"> <a href="http://www.w3cplus.com" target="_blank" class="links item first" id="first" title="123"> 1</a> <a href="" class="links active item" title="test website" target="_blank" lang="zh">2</a> <a href="sites/file/test.html" class="links item" title="this is a link" lang="zh-cn">3</a> <a href="sites/file/test.png" class="links item" target="_blank" lang="zh-tw">4</a> <a href="sites/file/image.png" class="links item" title="zh-cn">5</a> <a href="mailto:w3cplus@hotmail" class="links item" title="website link" lang="zh">6</a> <a href="" class="links item" title="open the website" lang="cn">7</a> <a href="" class="links item" title="close the website" lang="en-zh">8</a> <a href="" class="links item" title="http://www.sina.com" >9</a> <a href="" class="links item last" id="last">10</a> </div> </body> </html>
.demo a[id]{background:blue;color:yellow;}
.demo a[href][title]{background: yellow;}
.demo a[id="first"]{background:blue;}
.demo a[href="http://www.w3cplus.com"][title]{background-color: #fff;}
.demo a[title~="website"]{background:orange;}
.demo a[href^="sites/"]{background:#eee;}
.demo a[href$="png"]{background:#070707;}
.demo a[title*="site"]{background-color: violet;}
.demo a[lang|="zh"]{background:gray;}
下面所有的伪类选择器都是在demo3.html的基础上加入相关样式实现的。
demo3.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> .demo{ width:400px; height:50px; border:1px solid #ccc; padding:10px; } li{ border:1px solid #ccc; padding:2px; float:left; margin-right:4px; list-style: none; } a{ float:left; display:block; height:20px; line-height:20px; width:20px; border-radius:10px; text-align:center; background:#f36; color:green; text-decoration:none; } </style> </head> <body> <div class="demo clearfix"> <ul class="clearfix"> <li class="first links" id="first"><a href="">1</a></li> <li class="links"><a href="">2</a></li> <li class="links"><a href="">3</a></li> <li class="links"><a href="">4</a></li> <li class="links"><a href="">5</a></li> <li class="links"><a href="">6</a></li> <li class="links"><a href="">7</a></li> <li class="links"><a href="">8</a></li> <li class="links"><a href="">9</a></li> <li class="links last" id="last"><a href="">10</a></li> </ul> </div> </body> </html>
.demo a:link{color:gray;}
.demo a:visited{color:yellow;}
.demo a:hover{color:green;}
.demo a:active{color:blue;}
input[type="text"]:disabled {border:1px solid red;}
.demo li:first-child{background:green;}
.demo li:nth-child(n){background:green;} /*选择demo下的所有li*/
.demo li:nth-child(n+5){background:green;} /*选择demo下从第五个开始的li*/
.demo li:nth-child(-n+5){background:green;} /*选择demo下的前五个li*/
.demo li:nth-child(5){background:green;} /*选择demo下的第五个li*/
.demo li:nth-child(5n){background:green;} /*选择demo下5的倍数的li*/
.demo li:nth-child(3n+1){background:green;} /*每隔3个选一个*/
.demo li:nth-child(even){background:green;} /*偶数个li*/
.demo li:nth-child(odd){background:green;} /*奇数个li*/
p:empty{display: none}
a:has( img ){border:1px solid red;} /*为所有包含img的a添加边框*/
li:not(:has(p)){padding-top:10px;} /*选择不包含p的li元素*/
p:matches(.alert,.error,.warn){color:red;} /*只要元素p带有.alert,.error,.warn中的任何一个类名,文本颜色就变为red*/
标签:lis link sel active 不同类 The top 基本 visit
原文地址:https://www.cnblogs.com/yuyujuan/p/10444805.html