Search code examples
csspseudo-class

Is there a pseudo class selector for the first of 2 elements?


I know the plus sign + selects the second of 2 elements that are next but not in one another. Is there a selector for the first element?

I have 2 headings right after one another. h3 then h4. I want to remove the bottom margin from the h3 if an h4 follows. Is something like that possible or do I need to just provide a negative top margin to the h4?


Solution

  • Quoting CSS tricks:

    Let's be clear here, just in case someone is finding this from a search engine: there are no parent selectors in CSS, not even in CSS3.

    https://css-tricks.com/parent-selectors-in-css/ This article lays out a proposal for a possible parent selector, but due to various performance reasons, browsers would find it difficult to implement

    So, the best you can do is negative margins. That's actually a fairly decent solution

    h3 + h4 {
     margin-top: -10px;
    }