How can i select an html element based on type and not hierarchy using only CSS

Anass igli

I'm wondering if it is possible to target the last element of a particular type in CSS, knowing that there is no hierarchy so I can't use: the last-child CSS selector.

I want to apply a red font color to the last <p> tag following each <h1> tag.

The HTML is composed of one <h1> tag followed by multiple (number may vary) <h1> tags, as shown in the code below

I want to do this only with CSS! (no JS or SCSS ...)

<body>
<h1>hhhhhhhhh</h1>
<p>ppppp</p>
<p>ppppp</p>

<h1>hhhhhhhhh</h1>
<p>ppppp</p>
<p>ppppp</p>
<p>ppppp</p>

<h1>hhhhhhhhh</h1>
<p>ppppp</p>
<p>ppppp</p>
<p>ppppp</p>
<p>ppppp</p>

I tried :

h1 ~ p:last-of-type {
      color: red;
    }

but then it only selects the very last p, which is logical and understandable

I also tried this :

h1 ~ p:nth-last-of-type(3n) {
      color: blue;
    }

it works only when I have 3 'p' tags in each 'h1' tag

JavierMDQ

I think this is what you want:

  p:has(+ h1), h1 ~ p:last-of-type {
      color: red;      
    }
<body>
<h1>hhhhhhhhh</h1>
<p>ppppp</p>
<p>ppppp</p>

<h1>hhhhhhhhh</h1>
<p>ppppp</p>
<p>ppppp</p>
<p>ppppp</p>

<h1>hhhhhhhhh</h1>
<p>ppppp</p>
<p>ppppp</p>
<p>ppppp</p>
<p>ppppp</p>

  • p:has(+ h1) targets a p that has a h1 after it
  • h1 ~ p:last-of-type, as you said, targets the last one

EDIT: As Mihai pointed out, this is not a good idea because of browser compatibility. We need a previous sibling selector, which is not here yet...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the height of an element using css only

How can I print out HTML markup based on an object hierarchy?

How can I register a (boundless) type hierarchy using Autofac?

How can i do this type of design using html, css

How to select an element only if the next element is of a specific type using Nokogiri

How can I control the visible items in an html select element using "old" (pre-HTML5) HTML?

Can I select a DOM element based on an HTML comment it contains?

How can an html element fill out 100% of the remaining screen height, using css only?

How can you select an HTML element associated only with a specific class

How can I select an object type to deserialize using Json.Net based on property values in the data

How to select only an element in the last row of a table using CSS selectors

How can I display a div on hover of another element using only CSS

Can I target this element on hover using CSS only?

Can I detect element visibility using only CSS?

How can I restructure HTML elements that are side by side and stacked using only css and html?

How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

On hover of any element how can I select all sibling elements using CSS3 selector?

How can i select a element from a javascript/CSS website using Excel vba?

How I can select previous element in Selenium based of condition after?

How can I select only one element from a map function?

How can I select an element which has only a specific class?

Jquery how can I select only the .clicked DOM Element

How can I select only ONE element in a list (jetpack compose)?

How can I select only the first 3 children of an element?

How can I make BeautifulSoup using `select` html element with row color <tr bgcolor="#EFEFEF">

How can I populate a html select element with the contents of an array using vanilla javascript?

How can i insert an element in the HTML BODY using Js then select the same element using Js and then append some more data into it?

How can I show only the services of that category using select option of html in vue js?

How can I code this menu icon with only CSS and one element?