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 8874039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:37:18+00:00 2026-06-14T18:37:18+00:00

I have some HTML that looks like this: <div class=SomeContainer> <div class=MyClass></div> <div class=MyClass></div>

  • 0

I have some HTML that looks like this:

<div class="SomeContainer">
    <div class="MyClass"></div>
    <div class="MyClass"></div>
    <div class="MyClass"></div>
    <div class="MyOtherClass"></div>
</div>

And I use the following CSS

.MyClass{...}
.MyClass:last-child{box-shadow:0px 5px 10px black;}

I wrongly assumed that last-child operated on the last child of a certain class but it actually operates on the last-child of the container if it’s of the same class.

Is there some convenient way around? I know I could add a wrapper around the MyClass div and add the CSS for the shadow to the wrapper but I was wondering if there’s a better way to do it.

Thanks.

  • 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-06-14T18:37:19+00:00Added an answer on June 14, 2026 at 6:37 pm

    Since you’ve already tried using CSS3 selectors then this is the correct pseudo class to use

    .MyClass:last-of-type
    

    According to CSS3 specification (at least as I understand it) this should work but it seems it doesn’t. At least not in latest Chrome (v23) and Firefox (v17) browser versions.

    WARNING: Upper information doesn’t work and may be misleading that there’s a pure CSS solution to this problem… Because as of CSS3 there’s none. CSS4 on the other hand will have an additional pseudo class :nth-last-match(n) that will likely cover this exact scenario. But it’s too early to say when not all CSS3 selectors have been implemented by browsers.

    Additional investigation

    Here’s a JSFiddle that shows how this pseudo class selector actually works:

    1. get all elements that satisfy CSS selector (without pseudo)
    2. get their different HTML element types (tags)
    3. select all sibling elements of the same type (tag) for each type (tag) group
    4. Reduce each type (tag) group to the last element in the group
    5. Does this remaining element (of each group) satisfy selector (without pseudo)?
    6. If true then apply style otherwise don’t

    This means (as my example also shows) that several sibling elements can satisfy this selector as long as they’re of different type.

    This seems to work the same way in Chrome and Firefox. And if we carefully read specification it does say

    The :last-of-type pseudo-class represents an element that is the last sibling of its type in the list of children of its parent element.

    If it said that it’s the last sibling that satisfied selector then my upper solution would work. So lets test my steps.

    Run steps through using your HTML

    1. gets first 3 elements
    2. only one type: div
    3. four element (including the last one)
    4. reduce to the last one
    5. satisfies? No
    6. don’t set style

    Other couple of examples in my fiddle

    Just to test this pseudo class selector behaviour I added two additional examples. Here’s the HTML:

    <!-- First -->
    <div class="SomeContainer">
        <div class="MyClass">Content</div>
        <div class="MyClass">Content</div> 
        <p class="MyClass">Content</p>
        <p class="MyClass">Content</p>
    </div>
    
    <!-- Second -->
    <div class="SomeContainer">
        <div class="MyClass">Content</div>
        <div class="MyClass">Content</div>
        <div class="MyClass">Content</div>
        <p class="MyOtherClass">Content</p>
    </div>
    

    In the first one two elements satisfy selector (last div and last p).
    In the second one one element satisfies selector (last div)

    Outcome

    Having HTML that you provided and assuming that there can be an arbitrary number of elements with .MyClass there’s no way to write a general selector in CSS3 that would actually target the last element with particular CSS class.

    The best way is to add additional class to elements that represent the last one with particular class and write a selector to target that.

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

Sidebar

Related Questions

I have some HTML that looks like this: <div class=MyClass></div> <div class=MyClass></div> <div class=MyClass></div>
I have some HTML and CSS that looks like this: <div id=TheContainer> <div class=MyDivs></div>
I have some HTML that looks like this: <h2>Heading</h2> <div class='myClass'> <h2>Another Heading</h2> </div>
I have some HTML that looks like this: <div> <div class=value> <a href=# class=clicker>Some
I have some HTML that looks like this: <div class=product> Content In Here <div
So I have some html that looks like the following: <div class='something unknown' id='something_unknown_1'>
I have a some html that looks like this <div id=main> <div id=sub_main_1 class=sub_main>
I have some html that looks like this: <div class=generated> <p><label for=id_group>Group</label> <select name=group
I have some html that looks like this: <div class=block> <div id=item1></div> <div id=item2></div>
Hello I have some HTML that looks like this, <div id=music_interests> <ul class=interests> <li

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.