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

CSS Houdini achieves dynamic wave effect

CSS Houdini is known as the most exciting innovat...

Undo log in MySQL

Concept introduction: We know that the redo log i...

JavaScript canvas Tetris game

Tetris is a very classic little game, and I also ...

A brief discussion on the magical uses of CSS pseudo-elements and pseudo-classes

CSS plays a very important role in a web page. Wi...

Linux RabbitMQ cluster construction process diagram

1. Overall steps At the beginning, we introduced ...

Detailed explanation of overlay network in Docker

Translated from Docker official documentation, or...

HTML meta viewport attribute description

What is a Viewport Mobile browsers place web page...

Solution for creating multiple databases when Docker starts PostgreSQL

1 Introduction In the article "Start Postgre...

What to do if you forget your password in MySQL 5.7.17

1. Add skip-grant-tables to the my.ini file and r...

Implementation of whack-a-mole game in JavaScript

This article shares the specific code for JavaScr...

How to deploy springcloud project with Docker

Table of contents Docker image download Start mys...

Use of Linux usermod command

1. Command Introduction The usermod (user modify)...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...

Docker volume deletion operation

prune To use this command, both the client and da...