ホームページ  >  記事  >  ウェブフロントエンド  >  CSSでのnthの使用法は何ですか

CSSでのnthの使用法は何ですか

WBOY
WBOYオリジナル
2022-04-27 18:34:393922ブラウズ

n 番目の使用法: 1. ":nth-child(n)" は n 番目の子要素と一致します; 2. ":nth-last-child(n)" は n 番目の子要素と相互に一致します; 3. ": nth-of-type(n)" は、同じ型の n 番目のサブ要素と一致します。 4. ":nth-last-of-type(n)"。

CSSでのnthの使用法は何ですか

このチュートリアルの動作環境: Windows 10 システム、CSS3&&HTML5 バージョン、Dell G3 コンピューター。

css での nth の使用法は何ですか

1.:nth-child(n)

:nth-child(n) セレクターのマッチング親要素内の n 番目の子要素。要素の種類に制限はありません。

n には、数値、キーワード、または数式を指定できます。

例は次のとおりです。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>123</title> 
<style> 
p:nth-child(2)
{
background:#ff0000;
}
</style>
</head>
<body>
<h1>这是一个标题</h1>
<p>这是第一个段落。</p>
<p>这是第二个段落。</p>
<p>这是第三个段落。</p>
<p>这是第四个段落。</p>
<p><b>注意:</b> Internet Explorer 8 以及更早版本的浏览器不支持 :nth-child()选择器.</p>
</body>
</html>

出力結果:

CSSでのnthの使用法は何ですか

2、:nth -last- child(n)

:nth-last-child(n) セレクターは、要素のタイプに関係なく、最後の要素から数えて、その要素の N 番目の子要素であるすべての要素と一致します。子要素 。

n には、数値、キーワード、または数式を指定できます。

例は次のとおりです:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>123</title> 
<style> 
p:nth-last-child(2)
{
background:#ff0000;
}
</style>
</head>
<body>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
<p><b>注意:</b> Internet Explorer 8 以及更早版本的浏览器不支持:nth-last-child()选择器.</p>
</body>
</html>

出力結果:

CSSでのnthの使用法は何ですか

##3, :nth-of-type(n)

:nth-of-type(n) セレクターは、同じ型の n 番目の兄弟要素と一致します。

n には、数値、キーワード、または数式を指定できます。

例は次のとおりです:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>123</title> 
<style> 
p:nth-of-type(2)
{
background:#ff0000;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
</body>
</html>

出力結果:

CSSでのnthの使用法は何ですか

##4, :nth-last-of-type( n)

:nth-last-of-type(n) セレクターは、同じ型の下から n 番目の兄弟要素と一致します。

n には、数値、キーワード、または数式を指定できます。

例は次のとおりです:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>123</title> 
<style> 
p:nth-last-of-type(2)
{
background:#ff0000;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
<p><b>注意:</b> Internet Explorer 8以及更早版本的浏览器不支持:nth-last-of-type() 选择器.</p>
</body>
</html>

出力結果:

CSSでのnthの使用法は何ですか(学習ビデオ共有:

css ビデオ チュートリアル

)

以上がCSSでのnthの使用法は何ですかの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。