example
Combinator in CSS
Combinator represents the relationship between selectors, allowing combining selectors together as strings.
Combinator (combination) shows the relationship between selectors, allowing combining selectors together as characters.
In CSS, there are 4 familiar combinators as follows:
- Descendant combinator (space): Select elements that are descendants of another element.
- Child combinator (>): Select elements that are children of another element.
Note here that "descendants" are used to denote an element in another element, while "child" is the element directly located inside of another element. Eg
only span is the "child" of the div, and both span and p are "descendants" of the div.
- Adjacent sibling combinator (+): Select elements located adjacent and on par with other elements ("brother" elements are adjacent).
- General sibling combinator (~): Select element equal to another element ("brother" elements).
Descendant combinator
Descendant combinator (space) allows combining elements as descendants of another element.
div p {
background-color: yellow;
}
Here, descendants are p tags, the first element is div. The p tag in the div tag will have background-color: yellow.
For example:
Example Descendant combinator
Paragraph 1 is in the div.
Paragraph 2 is in the div.
Paragraph 3 is in the div.
Paragraph 4 is not in the div.
Paragraph 5 is not in the div.
Child combinator
Child combinator (>) allows combining "direct" sub-elements of another element (as explained above).
div > p {
background-color: yellow;
}
Here, the child element is p tag , the first element is div. The p tag in the div tag will have a background-color: yellow, and the other tags in p will not.
For example:
Example Descendant combinator
Paragraph 1 is in the div.
Paragraph 2 is in the div.
Paragraph 3 is in the div.
Paragraph 4 is not in the div.
Paragraph 5 is not in the div.
Adjacent sibling combinator
Adjacent sibling combinator (+) allows to select elements that are adjacent and on par with the specified element (adjacent "brother" elements).
div + p {
background-color: yellow;
}
Here, the element p is adjacent to and equal to the div, there will be background-color: yellow , and the other p tags in the div or p- cards are equal but not adjacent to the div .
For example:
Example Adjacent sibling combinator
Paragraph 1 is in the div.
Paragraph 2 is in the div.
Paragraph 3 is in the div.
Paragraph 4 is not in the div.
Paragraph 5 is not in the div.
General sibling combinator
General sibling combinator (~) allows to select elements that are equal to the specified element ("brother" elements).
div ~ p {
background-color: yellow;
}
Here, the element p is on the same level as the div will have background-color: yellow , and the other p tags in the div will not.
For example:
Example General sibling combinator
Paragraph 1 is in the div.
Paragraph 2 is in the div.
Paragraph 3 is in the div.
Paragraph 4 is not in the div.
Paragraph 5 is not in the div.
Previous post: Alignment - Align in CSS
Next lesson: Pseudo-class in CSS
You should read it
- 18 start-up killing mistakes from founder Y Combinator (part 1)
- 18 start-up mistakes from Y Combinator founder (part 2)
- 18 mistakes killing start-ups from founder Y Combinator (the last part)
- Dropbox Notes - group collaboration online notes service
- Attribute Selector - Attribute Selector in CSS
- Variable in PHP
- [General] What are the types of sinusitis?
- Syntax and Selector in CSS
- 3 main types of Linux distributions that you should know
- 2 types of foam should be skimmed, 3 types of foam should be kept when cooking
- Data types in SQL Server
- The most common types of SSD hard drives and how to distinguish them
Maybe you are interested
Hubble Telescope extremely rare photo of the moment of the Supernova explosion The Hubble Space Telescope: A Retrospective With Dr. Steven Hawley How to Create a Shortcut (Alias) for a File or Folder on Mac The day you were born, is there anything special about space? NASA will help you observe with Hubble Admire the giant stars forming in the corner of the Tarantula Nebula Admire the latest Hubble masterpiece