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

  • Home
  • SEARCH
  • 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 8052839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:39:48+00:00 2026-06-05T07:39:48+00:00

Using CSS, is there any way to select an element’s nearest descendent that matches

  • 0

Using CSS, is there any way to select an element’s nearest descendent that matches a certain selector?

<div class="foo"> <!-- if we're inside this -->
  <div>
    <br />
    <div class="bar"> <!-- match this -->
      <div class="bar"> <!-- but not this -->
        <div class="bar"> <!-- or this -->
        </div>
      </div>
    </div>
    <div class="bar"> <!-- I don't really care whether we match this -->
    </div>
  </div>
</div>

That is, something that would select the first .bar within any .foo (no matter how many child or sibling elements are in between), but not the second or third .bar.

  • 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-05T07:39:50+00:00Added an answer on June 5, 2026 at 7:39 am

    Here’s my hacktastic answer: http://jsfiddle.net/thirtydot/gqJdP/2/

    /*repeat for as many levels as there could be*/
    .foo > .bar,
    .foo > :not(.bar) > .bar,
    .foo > :not(.bar) > :not(.bar) > .bar {
        border-color: blue;
    }
    .bar {
        border: 1px solid red;
        padding: 10px;
    }
    

    Quoting myself from a comment:

    My suggestion is viable, although distasteful, if you can place an upper limit on the possible number of parents between .foo and the .bar you want to match. Even if you had to repeat the selector (let’s say) 10 times, it’s not too bad.

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

Sidebar

Related Questions

Is there any way only using CSS (no script) to check if an element
Is there any way to get the following effect using CSS? When container's width
Is there any way to theme an HTML table (CSS) with using the jQuery
Is there any way to emulate the display of a pre element via CSS?
Is there any way to select/manipulate CSS pseudo-elements such as ::before and ::after (and
Is there any way, how to make rounded HTML <select> , without using any
Is there any way to do the opposite of :hover using only CSS? As
Is there any pre-built sample of iphone like bottom navigation using CSS 3? is
Is there way to animate each letter in a word separately, using only CSS
Is there a way to detect a Operating system using JQuery or CSS? With

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.