Combined Selectors

In these examples, div is chosen to select any element that falls within a div, and p is chosen to select any element that is within a p. Either div or p could be replaced by ul, ol, or some other element as selector.

The w3schools web page lists two other combinations of selectors; however, I have never found a need to use them although I have written html code since the infancy of style sheets. I have used them for experiments but could have produced the effect as easy and with greater clarity for readers by selecting in a different manner.

Selector and any descendant selector, written as selector second-selector, or (div p)

The descendant selector matches all elements that are descendants of a specified element. As implied in the introduction, this could be div ul or even div p, div ul to select two descendants. In the following, the text must be tagged by p that is within, i.e., a descendant of, div for it to be selected. The text in this paragraph is tagged by a p, bit it is not within a div. Thus, there is no blue background.

Text within a div has a blue background only if it is also within the second selector, p. This text does not have a p tag.

This text is within the same div as that above and also has a p tag.

A title with an arrowhead that can be clicked on for additional details

This example has the p within not only the div but also another tag, actually two more tags. The background is blue even though p is within details and summary as well as within div.

This text is not within a div and does not have a blue background although it is tagged by a p.

Style

div p {
  background-color: Lightblue;
}