超级详细,玩转css选择器,这一篇就够了

    科技2022-07-14  152

    导语: css其实也能玩出花样来,只要学的够深入,够深刻,轻轻松松写出令人惊艳的样式出来,本文来写一写css选择器。

    所有的css选择器整理:

    选择器例子例子描述.class.xiaomizhou选择 class=“xiaomizhou” 的所有元素#id#firstname选择 id=“firstname” 的所有元素**选择所有元素elementp选择所有 < p > 元素element,elementdiv,p选择所有 < div > 元素和所有 < p > 元素element elementdiv p选择 < div > 元素内部的所有 < p > 元素element>elementdiv>p选择父元素为 < div > 元素的所有 < p > 元素element+elementdiv+p选择紧接在 < div > 元素之后的所有 < p > 元素。[attribute][target]选择带有 target 属性所有元素。[attribute=value][target=_blank]选择 target="_blank" 的所有元素。[attribute~=value][title~=flower]选择 title 属性包含单词 “flower” 的所有元素。[attribute]=value][lang:linka:link选择所有未被访问的链接。:visiteda:visited选择所有已被访问的链接。:activea:active选择活动链接。:hovera:hover选择鼠标指针位于其上的链接。:focusinput:focus选择获得焦点的 input 元素。:first-letterp:first-letter选择每个 < p > 元素的首字母。:first-linep:first-line选择每个 < p > 元素的首行。:first-childp:first-child选择属于父元素的第一个子元素的每个 < p > 元素。:beforep:before在每个 < p > 元素的内容之前插入内容。:afterp:after在每个 < p > 元素的内容之后插入内容。:lang(language)p:lang(it)选择带有以 “it” 开头的 lang 属性值的每个 < p > 元素。element1~element2p~ul选择前面有 < p > 元素的每个 < ul > 元素。[attribute^=value]a[src^=“https”]选择其 src 属性值以 “https” 开头的每个 < a > 元素。[attribute$=value]a[src$=".pdf"]选择其 src 属性以 “.pdf” 结尾的所有 < a > 元素。[attribute*=value]a[src*=“abc”]选择其 src 属性中包含 “abc” 子串的每个 < a > 元素。:first-of-typep:first-of-type选择属于其父元素的首个 < p > 元素的每个 < p > 元素。:last-of-typep:last-of-type选择属于其父元素的最后 < p > 元素的每个 < p > 元素。:only-of-typep:only-of-type选择属于其父元素唯一的 < p > 元素的每个 < p > 元素。:only-childp:only-child选择属于其父元素的唯一子元素的每个 < p > 元素。:nth-child(n)p:nth-child(2)选择属于其父元素的第二个子元素的每个 < p > 元素。:nth-last-child(n)p:nth-last-child(2)同上,从最后一个子元素开始计数。:nth-of-type(n)p:nth-of-type(2)选择属于其父元素第二个 < p > 元素的每个 < p > 元素。:nth-last-of-type(n)p:nth-last-of-type(2)同上,但是从最后一个子元素开始计数。:last-childp:last-child选择属于其父元素最后一个子元素每个 < p > 元素。:root:root选择文档的根元素。:emptyp:empty选择没有子元素的每个 < p > 元素(包括文本节点)。:target#news:target选择当前活动的 #news 元素。:enabledinput:enabled选择每个启用的 < input > 元素。:disabledinput:disabled选择每个禁用的 < input > 元素:checkedinput:checked选择每个被选中的 < input > 元素。:not(selector):not§选择非 < p > 元素的每个元素。::selection::selection选择被用户选取的元素部分。

    1,class

    <!DOCTYPE html> <html> <head> <style> .intro{ background-color:yellow; } </style> </head> <body> <div class="intro"> <p>我是小米粥。</p> </div> </body> </html>

    2,element+element

    <!DOCTYPE html> <html> <head> <style> div+p{ background-color:yellow; } </style> </head> <body> <div class="intro"> <p>我是小米粥。</p> </div> <p>我是小米粥。2</p> </body> </html>

    3,[attribute]

    //[target] 有target属性 //[target=_blank] target属性为_blank //[title~=flower] title属性包含flower //[lang|=en] lang属性以en开头 //下面演示第二种target属性为_blank <!DOCTYPE html> <html> <head> <style> a[target=_blank]{ background-color:yellow; } </style> </head> <body> <p>带有 target 属性的链接会得到黄色背景:</p> <a href="http://www.w3school.com.cn">w3school.com.cn</a> <a href="http://www.disney.com" target="_blank">disney.com</a> <a href="http://www.wikipedia.org" target="_top">wikipedia.org</a> </body> </html>

    4,a:link,:visited,:active,:hover,:focus

    <head> <style> a:link{ background-color:white; } a:visited{ background-color:yellow; } a:active{ background-color:yellow; } a:hover{ background-color:yellow; } input:focus{ background-color:yellow; } </style> </head> <body> <a href="http://www.w3school.com.cn">W3Sschool</a> <a href="http://www.google.com">Google</a> <a href="http://www.wikipedia.org">Wikipedia</a><br> First name: <input type="text" name="firstname" /><br> <p><b>注释:<br></b>:link 选择器为未被访问过的链接设置样式。</p><br> </b>:visited 选择器为已被访问过的链接设置样式。</p><br> </b>:active 选择器为点击时候链接设置样式。</p><br> </b>:hover 选择器为鼠标悬浮的时候链接设置样式。</p><br> </b>input:focus 选择器为input聚焦时候的设置样式。</p><br> </body> </html>

    5,first-letter,first-line

    <!DOCTYPE html> <html> <head> <style> p:first-letter{ font-size:200%; color:#8A2BE2; } p:first-line{ background-color:yellow; } </style> </head> <body> <div> <p>My name is Donald.My name is Donald.My name is Donald.My name is Donald.My name is Donald.My name is Donald.My name is Donald.My name is Donald.</p> <p>I live in Duckburg.</p> </div> </body> </html>

    6,before,after

    <!DOCTYPE html> <html> <head> <style> p:before{ content:"台词:"; } p:after{ content:"哈哈哈哈"; } </style> </head> <body> <p>我是小米粥。</p> <p>我住在中国。</p> <b>注释:</b>对于在 IE8 中工作的 :before,必须声明 DOCTYPE。 </body> </html>

    7,input[type=“text”]:enabled,:disabled

    <!DOCTYPE html> <html> <head> <style> input[type="text"]:enabled { background:#ffff00; } input[type="text"]:disabled { background:#dddddd; } </style> </head> <body> <form action=""> First name: <input type="text" value="Mickey" /><br> Last name: <input type="text" value="Mouse" /><br> Country: <input type="text" disabled="disabled" value="Disneyland" /><br> </form> </body> </html>

    补充: 微信搜索【web小馆】,回复全栈博客项目,即可获取项目源码和后续的实战文章教程。每天用最简单朴实的语言,潜移默化的提升你的计算机基础知识和前端技术。小米粥,一个专注的web全栈工程师,我们下期再见!

    Processed: 0.013, SQL: 8