From b25bc3d0b656ae56fdd9277af12c1cb60017267a Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Tue, 4 Jan 2022 21:58:53 +0900 Subject: ツリー構造擬似クラスを変換準備 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- files/ja/web/css/_colon_first-child/index.md | 132 +++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 files/ja/web/css/_colon_first-child/index.md (limited to 'files/ja/web/css/_colon_first-child/index.md') diff --git a/files/ja/web/css/_colon_first-child/index.md b/files/ja/web/css/_colon_first-child/index.md new file mode 100644 index 0000000000..e7f9680e4d --- /dev/null +++ b/files/ja/web/css/_colon_first-child/index.md @@ -0,0 +1,132 @@ +--- +title: ':first-child' +slug: 'Web/CSS/:first-child' +tags: + - CSS + - Layout + - Pseudo-class + - Reference + - Selector + - Web +translation_of: 'Web/CSS/:first-child' +--- +
{{CSSRef}}
+ +

:first-childCSS擬似クラスで、兄弟要素のグループの中で最初の要素を表します。

+ +
/* 兄弟要素の中で最初の <p> を
+   すべてを選択 */
+p:first-child {
+  color: lime;
+}
+ +
+

注: 最初の定義では、親のある要素のみが選択されていました。 Selectors Level 4 の初期に、これは必要なくなりました。

+
+ +

構文

+ +{{csssyntax}} + +

+ +

基本的な例

+ +

HTML

+ +
<div>
+  <p>This text is selected!</p>
+  <p>This text isn't selected.</p>
+</div>
+
+<div>
+  <h2>This text isn't selected: it's not a `p`.</h2>
+  <p>This text isn't selected.</p>
+</div>
+
+ +

CSS

+ +
p:first-child {
+  color: lime;
+  background-color: black;
+  padding: 5px;
+}
+
+ +

結果

+ +

{{EmbedLiveSample('Basic_example', 500, 200)}}

+ +

リストのスタイル付け

+ +

HTML

+ +
<ul>
+  <li>Item 1</li>
+  <li>Item 2</li>
+  <li>Item 3
+    <ul>
+      <li>Item 3.1</li>
+      <li>Item 3.2</li>
+      <li>Item 3.3</li>
+    </ul>
+  </li>
+</ul>
+ +

CSS

+ +
ul li {
+  color: blue;
+}
+
+ul li:first-child {
+  color: red;
+  font-weight: bold;
+}
+ +

結果

+ +

{{EmbedLiveSample('Styling_a_list')}}

+ +

仕様書

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
仕様書状態備考
{{SpecName('CSS4 Selectors', '#first-child-pseudo', ':first-child')}}{{Spec2('CSS4 Selectors')}}選択する要素に親を必要としないようにした。
{{SpecName('CSS3 Selectors', '#first-child-pseudo', ':first-child')}}{{Spec2('CSS3 Selectors')}}変更なし。
{{SpecName('CSS2.1', 'selector.html#first-child', ':first-child')}}{{Spec2('CSS2.1')}}初回定義
+ +

ブラウザーの互換性

+ +

{{Compat("css.selectors.first-child")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf