简体   繁体   English

是否可以使用纯 CSS 选择出现在另一个 HTML 元素之后的任何地方的 HTML 元素?

[英]Is it possible to select HTML elements which appear anywhere after another HTML element using pure CSS?

Is it possible to select any/all <h1> element(s) which appear(s) after an <h2> element using pure CSS?是否可以使用纯 CSS 选择出现在<h2>元素之后的任何/所有<h1>元素?

I have combed through pages of documentation for all of the CSS selectors and pseudo-classes and am unable to determine if it's possible to select certain elements which appear AFTER certain elements, both within the same branch and within a different branch of the HTML markup.我已经梳理了所有 CSS 选择器和伪类的文档页面,并且无法确定是否可以选择出现在某些元素之后的某些元素,无论是在同一分支中还是在 HTML 标记的不同分支中。

It seems as though the selectors and pseudo-classes are designed to only locate elements which are:似乎选择器和伪类旨在仅定位以下元素:

  • Siblings兄弟姐妹
  • Children孩子们
  • Descendants子孙

I am in need of finding additional relationships:我需要寻找其他关系:

  • Nieces侄女
  • Nephews侄子
  • Cousins表亲
  • Third cousins twice removed三代堂兄弟两次被移除
  • Granduncles大叔们
  • Etc.等等。

Which are logically AFTER a certain element in the tree.这在逻辑上在树中的某个元素之后。 I never know:我从来都不知道:

  • The exact relationship确切的关系
  • How many there will be of the elements I desire to select.我想选择多少个元素。

 Example 1 - Niece <div class="example"> <h2>2</h2> <div> <h1>1</h1> </div> </div> Example 2 - Cousin <div class="example"> <div> <h2>2</h2> </div> <div> <h1>1</h1> </div> </div> Example 3 - Granduncle <div class="example"> <div> <div> <h2>2</h2> </div> </div> <h1>1</h1> </div>

Is it possible to select any/all element(s) which appear(s) after an element using pure CSS?是否可以使用纯 CSS 选择出现在元素之后的任何/所有元素?

Pure CSS?纯CSS? Yes是的
Browser support?浏览器支持? Only Safari and newer versions of Chrome仅限Safari 和较新版本的 Chrome

Using the :has() pseudo class we can check descendants without targeting them directly:使用:has()伪类,我们可以检查后代而不直接定位它们:

 :root { --good-browser-support: skyblue; --emerging-browser-support: coral; } .example :has(h2)+ :is(h1, * h1) { /* Ancestor to sibling */ color: var(--emerging-browser-support); } .example :has(h2)+* h1 { /* Ancestor to descendant */ color: var(--emerging-browser-support); } .example h2+h1 { /* Subsequent siblings */ color: var(--good-browser-support); } .example h2+* h1 { /* Nieces */ color: var(--good-browser-support); }
 Example 1 - Niece <div class="example"> <h2>2</h2> <div> <h1>1</h1> </div> </div> Example 2 - Cousin <div class="example"> <div> <h2>2</h2> </div> <div> <h1>1</h1> </div> </div> Example 3 - Granduncle <div class="example"> <div> <div> <h2>2</h2> </div> </div> <h1>1</h1> </div> Example 4 - Uncle <div class="example"> <div> <h2>2</h2> </div> <h1>1</h1> </div> Example 5 - Non-target: Prior Siblings <div class="example"> <h1>1</h1> <h2>2</h2> </div> Example 6 - Subsequent Siblings <div class="example"> <h2>2</h2> <h1>1</h1> </div> Example 7 - Non-target: Prior Niece <div class="example"> <div> <h1>1</h1> </div> <h2>2</h2> </div> Example 5 - Non-target: Prior Siblings shared parent <div class="example"> <div> <h1>1</h1> <h2>2</h2> </div> </div>

Condensed version:精简版:

 :is(.example :has(h2)+ :is(h1, * h1), .example :has(h2)+* h1, .example h2 + h1, .example h2 + * h1) { color: magenta; }
 Example 1 - Niece <div class="example"> <h2>2</h2> <div> <h1>1</h1> </div> </div> Example 2 - Cousin <div class="example"> <div> <h2>2</h2> </div> <div> <h1>1</h1> </div> </div> Example 3 - Granduncle <div class="example"> <div> <div> <h2>2</h2> </div> </div> <h1>1</h1> </div> Example 4 - Uncle <div class="example"> <div> <h2>2</h2> </div> <h1>1</h1> </div> Example 5 - Non-target: Prior Siblings <div class="example"> <h1>1</h1> <h2>2</h2> </div> Example 6 - Subsequent Siblings <div class="example"> <h2>2</h2> <h1>1</h1> </div> Example 7 - Non-target: Prior Niece <div class="example"> <div> <h1>1</h1> </div> <h2>2</h2> </div> Example 5 - Non-target: Prior Siblings shared parent <div class="example"> <div> <h1>1</h1> <h2>2</h2> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM