Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 :after Overriding :last-child

I have the following CSS to style a simple list:

ul.menu_list li {
display: inline;
}
ul.menu_list li:after {
content:" | ";
}
ul.menu_list li:last-child  {
content:"";
}

<ul class="menu_list">
  <li><a href="">Link</a></li>
  <li><a href="">Link</a></li>
  <li><a href="">Link</a></li>
</ul>

I get the desired effect except for the last-child is not losing it's vertical bar, " | ".

Link | Link | Link |

I've tried combining :after:last-child & vice-versa but the first :after declaration always takes precedence.

like image 550
dv8withn8 Avatar asked May 25 '11 15:05

dv8withn8


People also ask

How do you select all but last child in CSS?

When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes.

Is last child 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

Do:

ul.menu_list li:after {
    content:" | ";
}
ul.menu_list li:last-child:after  {
    content:"";
}
like image 170
timdream Avatar answered Sep 20 '22 15:09

timdream