This article teaches you how to play with CSS combination selectors

This article teaches you how to play with CSS combination selectors

CSS combination selectors include various combinations of simple selectors.

There are four combinations in CSS3:

  • Descendant selectors (space separated)
  • Child element selector (separated by greater than sign)
  • Adjacent sibling selectors (separated by plus signs)
  • Normal sibling selectors (dash-separated)

1. Descendant Selector

The descendant selector is used to select descendant elements of an element.

The following example selects all <p> elements and inserts them into a <div> element:

div p{
      background-color:yellow;
    }

2. Child element selector

Compared to descendant selectors, child selectors can only select elements that are children of another element.

The following example selects all <p> elements that are direct children of a <div> element:

 div>p {
      background-color:yellow;
    }

3. Adjacent sibling selector

The adjacent sibling selector selects an element that is immediately after another element and has the same parent.

If you need to select an element that is immediately after another element and both have the same parent, you can use the adjacent sibling selector.

The following example selects all <p> elements that are located after the first <div> element:

div+p{
      background-color:yellow;
    }

4. Subsequent sibling selector

The following sibling selector selects all the adjacent sibling elements following the specified element.

The following example selects all adjacent sibling elements <p> after all <div> elements:

div~p {
      background-color:yellow;
    }

This is the end of this article about teaching you how to use CSS combination selectors. For more relevant CSS combination selector content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Some understanding of absolute and relative positioning of page elements

>>:  Two ways to start Linux boot service

Recommend

The phenomenon of margin-top collapse and the specific solution

What is margin-top collapse Margin-top collapse i...

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the f...

JS practical object-oriented snake game example

Table of contents think 1. Greedy Snake Effect Pi...

Complete MySQL Learning Notes

Table of contents MyISAM and InnoDB Reasons for p...

Solution to mysql prompt "got timeout reading communication packets"

Error message: user: 'root' host: `localh...

Detailed explanation of vue.js dynamic components

:is dynamic component Use v-bind:is="compone...

How to query the intersection of time periods in Mysql

Mysql query time period intersection Usage scenar...

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...

Share 9 Linux Shell Scripting Tips for Practice and Interviews

Precautions 1) Add interpreter at the beginning: ...

Ubuntu Docker installation in vmware (container building)

1. Mind Map 2. How to build a container 2.1 Prepa...

VMware installation of Centos8 system tutorial diagram (Chinese graphical mode)

Table of contents 1. Software and system image 2....

How to use Greek letters in HTML pages

Greek letters are a very commonly used series of ...