CSSオンラインマニュアル
/ CSS 分組 和 嵌套
CSS 分組 和 嵌套
CSS 分組 和 嵌套 選擇器
分組選擇器
在樣式表中有很多具有相同樣式的元素。
h1 { color:green; } h2 { color:green; } p { color:green; }
為了盡量減少代碼,你可以使用分組選擇器。
每個(gè)選擇器用逗號(hào)分隔.
在下面的例子中,我們對(duì)以上代碼使用分組選擇器:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h1,h2,p { color:green; } </style> </head> <body> <h1>Hello World!</h1> <h2>Smaller heading!</h2> <p>This is a paragraph.</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
嵌套選擇器
它可能適用于選擇器內(nèi)部的選擇器的樣式。
在下面的例子設(shè)置了三個(gè)樣式:
p{ }:為所有p元素指定一個(gè)樣式
.marked{ }:為所有class="marked"的元素指定一個(gè)樣式
.marked p{ }: 為所有 class="marked" 元素內(nèi)的 p 元素指定一個(gè)樣式。
p.marked{ }: 為所有 class="marked" 的 p 元素指定一個(gè)樣式。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p { color:blue; text-align:center; } .marked { background-color:red; } .marked p { color:white; } </style> </head> <body> <p>This paragraph has blue text, and is center aligned.</p> <div class="marked"> <p>This paragraph has not blue text.</p> </div> <p>p elements inside a "marked" classed element keeps the alignment style, but has a different text color.</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例