Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css last-child with :not selector

I have a dynamic number of ul.column and some of these have the disabled class. For example:

<ul class="column"></ul>
<ul class="column"></ul>
<ul class="column disabled"></ul>

I don't know how many I can have, but I want to apply a border-right to the last .column which doesn't also have the .disabled class.

I've tried something like:

ul:not(.disabled):last-child {border-right:1px solid black}

and

ul:last-child:not(.disabled) {border-right:1px solid black}

but the style always applies to the last element, regardless of the :not(.disabled) selector. Is there another way to style the last .column that doesn't also have the .disabled class?

I'm fine using jQuery, but I don't know how achieve what I want.

like image 426
valerio0999 Avatar asked May 13 '16 12:05

valerio0999


People also ask

Why is last child not working CSS?

:last-child will not work if the element is not the VERY LAST element. In addition to Harry's answer, I think it's crucial to add/emphasize that :last-child will not work if the element is not the VERY LAST element in a container.

What is not () in CSS?

:not() The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the negation pseudo-class.

How do I get the last element in CSS?

The :last-child selector allows you to target the last element directly inside its containing element. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.


1 Answers

This isn't possible with just css selectors. You could use jQuery though.

Demo

$('ul:not(.disabled):last').css('border-right', '1px solid black');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="column">
  <li>test</li>
</ul>
<ul class="column">
  <li>test</li>
</ul>
<ul class="column disabled">
  <li>test</li>
</ul>
like image 194
Chris Avatar answered Sep 30 '22 19:09

Chris