Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1058555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:02:36+00:00 2026-05-16T18:02:36+00:00

I’m trying to select a first element of class ‘A’ in an element with

  • 0

I’m trying to select a first element of class ‘A’ in an element with id or class ‘B’. I’ve tried a combination of > + and first-child selectors, since it’s not a first element inside class element ‘B’. It worked, but … i’m trying to override some default css and i have no control over the server side and it seems that the class ‘A’ element is sometimes generated in a different position. Here’s an illustration:

<class-C>
  <class-B> might have a different name
      <some-other-classes> structure and element count might differ
      <class-A></class-A> our target
      <class-A></class-A> this shouldn't be affected
      <class-A></class-A> this shouldn't be affected
  </class-B>
</class-C>

Sometimes the name of the class ‘B’ differs and the elements before ‘A’ differ as well. So is there any way to select the first occurrence of ‘A’ in an element ‘C’? Because class ‘C’ is always there. I can’t use + > and first-child selectors since the path to first ‘A’ element differs, but element ‘C’ is always there and it would be a nice starting point.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:02:36+00:00Added an answer on May 16, 2026 at 6:02 pm

    CSS3 provides the :first-of-type pseudo-class for selecting the first element of its type in relation to its siblings. However it doesn’t have a :first-of-class pseudo-class.

    As a workaround, if you know the default styles for your other .A elements, you can use an overriding rule with the general sibling combinator ~ to apply styles to them. This way, you sort of “undo” the first rule.

    The bad news is that ~ is a CSS3 selector.
    The good news is that IE recognizes it starting from IE7, like CSS2’s >, so if you’re worried about browser compatibility, the only “major browser” this fails on is IE6.

    So you have these two rules:

    .C > * > .A {
        /* 
         * Style every .A that's a grandchild of .C.
         * This is the element you're looking for.
         */
    }
    
    .C > * > .A ~ .A {
        /* 
         * Style only the .A elements following the first .A child
         * of each element that's a child of .C.
         * You need to manually revert/undo the styles in the above rule here.
         */
    }
    

    How styles are applied to elements is illustrated below:

    <div class="C">
        <!--
        As in the question, this element may have a class other than B.
        Hence the intermediate '*' selector above (I don't know what tag it is).
        -->
        <div class="B">
            <div class="E">Content</div> <!-- [1] -->
            <div class="F">Content</div> <!-- [1] -->
            <div class="A">Content</div> <!-- [2] -->
            <div class="A">Content</div> <!-- [3] -->
        </div>
        <div class="D">
            <div class="A">Content</div> <!-- [2] -->
            <div class="E">Content</div> <!-- [1] -->
            <div class="F">Content</div> <!-- [1] -->
            <div class="A">Content</div> <!-- [3] -->
        </div>
    </div>
    
    1. This element does not have class A. No rules are applied.

    2. This element has class A, so the first rule is applied. However it doesn’t have any other such elements occurring before it, which the ~ selector requires, so the second rule is not applied.

    3. This element has class A, so the first rule is applied. It also comes after other elements with the same class under the same parent, as required by ~, so the second rule is also applied. The first rule is overridden.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.