結合子

The final selectors we will look at are called combinators, because they combine other selectors in a way that gives them a useful relationship to each other and the location of content in the document.

前提条件: 基本的なコンピューターリテラシー、基本的なソフトウェアがインストールされていること、ファイルの扱い、HTML の基本(HTML 入門)および CSS に関するアイデア(CSS の第一歩)に関する基本的な知識を得ている。
目的: CSS で使用できるさまざまな結合子セレクターについて学ぶ。

Descendant selector

You have already come across descendant selectors in previous lessons — selectors with spaces in between them:

body article p

These selectors select elements that are descendants of others selector. They do not need to be direct children to match.

In the example below we are matching only the <p> element which is inside an element with a class of .box.

Child combinator

The child combinator is a greater-than symbol (>), which matches only when the selectors select elements that are direct children. Descendants further down the hierarchy don't match. 例えば、to select only <p> elements that are direct children of <article> elements:

article > p

In this next example we have an unordered list, nested inside of which is another unordered list. I am using the child combinator to select only the <li> elements which are a direct child of a <ul>, and have given them a top border.

If you remove the > that designates this as a child combinator, you end up with a descendant selector and all <li> elements will get a red border.

Adjacent sibling

The adjacent sibling selector (+) is used to select something if it is right next to another element at the same level of the hierarchy. 例えば、to select all <img> elements that come right after <p> elements:

p + img

A common use case is to do something with a paragraph that follows a heading, as in my example below. Here we are looking for a paragraph which is directly adjacent to an <h1>, and styling it.

If you insert some other element such as a <h2> in between the <h1> and the <p>, you will find that the paragraph is no longer matched by the selector and so does not get the background and foreground color applied when the element is adjacent.

General sibling

If you want to select siblings of an element even if they are not directly adjacent, then you can use the general sibling combinator (~). To select all <img> elements that come anywhere after <p> elements, we'd do this:

p ~ img

In the example below we are selecting all <p> elements that come after the <h1>, and even though there is a <div> in the document as well, the <p> that comes after it is selected.

Using combinators

You can combine any of the selectors that we discovered in previous lessons with combinators in order to pick out part of your document. 例えば、if we want to select list items with a class of "a", which are direct children of a <ul>, I could use the following.

ul > li[class="a"]  {  }

Take care however when creating big lists of selectors that select very specific parts of your document. It will be hard to reuse the CSS rules as you have made the selector very specific to the location of that element in the markup.

It is often better to create a simple class and apply that to the element in question. That said, your knowledge of combinators will come in very useful if you need to get to something in your document and are unable to access the HTML, perhaps due to it being generated by a CMS.

Moving on

This is the last section in our lessons on selectors. Next we will move on to another important part of CSS — the CSS Box Model.

このモジュール

  1. カスケードと継承
  2. CSS セレクター
  3. ボックスモデル
  4. 背景と枠線
  5. テキスト方向の操作
  6. 要素のはみ出し(オーバーフロー)
  7. CSS の値と単位
  8. CSS によるサイズ設定
  9. 画像・メディア・フォーム要素
  10. 表のスタイリング
  11. CSS のデバッグ
  12. CSS の整理