Target the First Visible Element After the First Active Element in CSS

Kirk Ouimet

Getting into some pretty gnarly CSS here. In the below scenario, I want to only match #4. This is what I have going on:

<ul class="tabMenu">
  <li class="hidden">1</li>
  <li class="active">2</li>
  <li class="hidden">3</li>
  <li>4</li> <!-- I want this one's blood -->
  <li class="hidden">5</li>
  <li class="hidden">6</li>
  <li>7</li>
  <li class="hidden">8</li>
  <li>9</li>
</ul>

Here are my attempts, which are failing:

/* Only matches the next immediate li (so it works if #3 is not hidden) */
.tabMenu li:not(.hidden).active + li:not(.hidden) a {
  color: red;
}

/* Matches #4 and #7, I just want #4 */
.tabMenu li:not(.hidden).active ~ li:not(.hidden) a {
  color: red;
}

After 30 minutes on this I have decided to bring my problem to you. Please help!

Raj Nathani

Taking advantage of the way CSS sibling selectors work will be the way to go here.

  • Step 1: Let us perform a selection of all the elements after the first active one which does not contain the class hidden or active.

    li.active ~ li:not(.hidden):not(.active) {
        color:red;
    }
    
  • Step 2: Let us remove the target of all the siblings after the targeted elements. The CSS selector ~ selects all sibling elements which come after it, and NOT including it. Thus by applying it to the li elements after the target, we successfully remove from all the elements except the first one which was targeted in step 1.

    li.active ~ li:not(.hidden):not(.active) ~ li {
        color:inherit;
    }
    

See this happening at: http://jsfiddle.net/nJHd2/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Targeting first visible element with pure CSS

From Dev

GEB find first visible element

From Dev

Getting first visible element with jQuery

From Dev

Target element if it is the first element inside a div

From Dev

how to only target a css class if it is the first element within the body tag?

From Dev

Only first element of response visible - php

From Dev

Clicking First Visible Element of a Certain Class in Selenium

From Dev

Find first completely visible element in recyclerview

From Dev

get first element after a space

From Dev

Add text after first element

From Dev

Reproduce element after first compiling

From Dev

CSS selector for the first element after another not being siblings

From Java

CSS selector for first element with class

From Dev

CSS first child hover with a element

From Dev

CSS selector for the first element of class

From Dev

How do I target a first-child that is visible (after children that are set to display:none), with only CSS

From Dev

Target first child after css class

From Java

Getting first element and returning after apply a function

From Dev

Select second element that is after first one

From Dev

Jsoup select elements after first element

From Dev

Getting first element and returning after apply a function

From Dev

Segmention Fault after entering the first element

From Dev

Get first element with class X after

From Dev

How to remove the first <br> after a certain element

From Dev

How do you find first visible element with jqlite under AngularJS?

From Dev

Assign CSS class to first and third html element

From Dev

Select the first CSS element using a pseudo class

From Dev

css select first element with specific tag

From Dev

Is there CSS to select blockquote only if it is the first element in a div?

Related Related

  1. 1

    Targeting first visible element with pure CSS

  2. 2

    GEB find first visible element

  3. 3

    Getting first visible element with jQuery

  4. 4

    Target element if it is the first element inside a div

  5. 5

    how to only target a css class if it is the first element within the body tag?

  6. 6

    Only first element of response visible - php

  7. 7

    Clicking First Visible Element of a Certain Class in Selenium

  8. 8

    Find first completely visible element in recyclerview

  9. 9

    get first element after a space

  10. 10

    Add text after first element

  11. 11

    Reproduce element after first compiling

  12. 12

    CSS selector for the first element after another not being siblings

  13. 13

    CSS selector for first element with class

  14. 14

    CSS first child hover with a element

  15. 15

    CSS selector for the first element of class

  16. 16

    How do I target a first-child that is visible (after children that are set to display:none), with only CSS

  17. 17

    Target first child after css class

  18. 18

    Getting first element and returning after apply a function

  19. 19

    Select second element that is after first one

  20. 20

    Jsoup select elements after first element

  21. 21

    Getting first element and returning after apply a function

  22. 22

    Segmention Fault after entering the first element

  23. 23

    Get first element with class X after

  24. 24

    How to remove the first <br> after a certain element

  25. 25

    How do you find first visible element with jqlite under AngularJS?

  26. 26

    Assign CSS class to first and third html element

  27. 27

    Select the first CSS element using a pseudo class

  28. 28

    css select first element with specific tag

  29. 29

    Is there CSS to select blockquote only if it is the first element in a div?

HotTag

Archive