语法
:only-child { css declarations; }
:only-child 选择器的例子:
<!DOCTYPE html> <html> <head> <title>文档的标题</title> <style> p:only-child { background: #1c87c9; } </style> </head> <body> <h2>:only-child selector example</h2> <div> <p>Lorem ipsum is simply dumnmy text.</p> </div> <div> <p>生活终归还得继续。.</p> <p>生活终归还得继续。.</p> </div> </body> </html>
<li> 标记的 :only-child 选择器示例:
<!DOCTYPE html> <html> <head> <title>文档的标题</title> <style> ul li { list-style-type: square; } li:only-child { color: blue; list-style-type: disc; } </style> </head> <body> <h2>:only-child selector example</h2> <ol> <li> One element <ul> <li>This list has just one element.</li> </ul> </li> <li> Two elements <ul> <li>This list has two elements.</li> <li>This list has two elements.</li> </ul> </li> <li> Three elements <ul> <li>This list has three elements.</li> <li>This list has three elements.</li> <li>This list has three elements.</li> </ul> </li> </ol> </body> </html>
<em> 标签的 :only-child 选择器示例:
<!DOCTYPE html> <html> <head> <title>文档的标题</title> <style> em:only-child { background-color: #8ebf42; } </style> </head> <body> <h2>:only-child selector example</h2> <p>This is a <em>paragraph</em>.</p> <p>This is a paragraph.</p> </body> </html>
<div> 标签的 :only-child 选择器示例:
<!DOCTYPE html> <html> <head> <title>文档的标题</title> <style> div:only-child { color: #8ebf42; font-weight: bold; } div { display: block; margin: 6px; padding: 5px; outline: 1px solid #1c87c9; } div div { display: inline-block; } </style> </head> <body> <h2>:only-child selector example</h2> <div> <div>I am an only child.</div> </div> <div> <div>I am the 1st sibling.</div> <div>I am the 2nd sibling.</div> <div> I am the 3rd sibling, <div>but this is an only child.</div> </div> </div> </body> </html>
:only-child 伪选择器匹配一个元素,如果它是其父元素的唯一子元素。
仅当其父元素没有任何类型的其他子元素时才会选择该元素。
:only-child 伪类与 :first-child、:last-child 或者 :nth-child()、:nth-last-child() 相同,但具有较低的特异性。
日期:2020-06-02 22:14:41 来源:oir作者:oir