Home > Web Front-end > CSS Tutorial > How Can We Group Descendants in CSS Selectors?

How Can We Group Descendants in CSS Selectors?

Susan Sarandon
Release: 2024-11-01 10:30:02
Original
248 people have browsed it

How Can We Group Descendants in CSS Selectors?

Grouping Descendants in CSS Selectors

Despite the common need to style groups of descendants, CSS lacks a straightforward approach for this task. Consider a simple HTML table:

`


<th></th>
<th></th>
<th></th>
Copy after login


.
.
.

<td></td>
<td></td>
<td></td>
Copy after login


`

Traditionally, styling all column headings and cells requires the verbose selector:

#myTable th, #myTable td {}

Why can't we use a syntax like this instead:

#myTable (th,td) {}

Proposed Solutions

Until recently, there was no standard solution for grouping descendants in CSS selectors. However, the following pseudo-classes have been proposed:

  • :-moz-any(): Introduced by Mozilla in 2010.
  • :-webkit-any(): Suggested for WebKit browser but not widely adopted.

However, using these prefixed selectors requires duplicating rulesets and is not recommended for public-facing code.

Future Developments

The Selectors level 4 working draft proposes the :matches() pseudo-class, based on the original :any() proposal. This new pseudo-class may offer enhancements in future revisions.

Workarounds

For the specific case of styling both th and td elements, the * selector can be used:

#myTable tr > * {}

This assumes that tr elements only contain cell elements. For performance-conscious developers, however, this is still not as efficient as the verbose method.

The above is the detailed content of How Can We Group Descendants in CSS Selectors?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template