Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:before to all children except :first-child

Tags:

html

css

I have a horizontal navigation. I'm trying to separate each nav item with a "|" (pipe) using only css.

HTML

<ul>
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
    <li>Fourth</li>
    <li>Fifth</li>
</ul

Right now it looks like this

First   Second   Third   Fourth   Fifth

I want it to look like this

First | Second | Third | Fourth | Fifth

I'm able to use css in order to put a "|" before each <li>

li:before {
    content:"|";
}

How ever the result is

|  First | Second | Third | Fourth | Fifth

How do I do this with out adding the "|" to the first item?

like image 367
xslibx Avatar asked Nov 04 '15 22:11

xslibx


People also ask

How do I select every child except first?

This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element.

How do I not include my first child in CSS?

Use the :not(selector) Selector Not to Select the First Child in CSS. We can use the :not(selector) selector to select every other element that is not the selected element. So, we can use the selector not to select the first child in CSS.

How do I select all children except one in CSS?

To select all the children of an element except the last child, use :not and :last-child pseudo classes.

What is not first child?

ul:not(:first-child) means literally "any ul element that is not first child of its parent", so it won't match even the 1st ul if it's preceded by another element ( p , heading etc.). On the contrary, ul:not(:first-of-type) means "any ul element except the 1st ul in the container".


4 Answers

other option :)

li {
  display: inline-block
}
li + li:before {
  content: '|';
}
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>Fourth</li>
  <li>Fifth</li>
</ul>
like image 183
G-Cyrillus Avatar answered Oct 14 '22 00:10

G-Cyrillus


You can use the :not pseudo-class:

li:not(:first-child):before {
  content: "|";
}

ul {
  display: flex;
  list-style: none;
}
li:not(:first-child):before {
  content: "|";
  padding: 5px;
}
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>Fourth</li>
  <li>Fifth</li>
</ul>
like image 24
Oriol Avatar answered Oct 14 '22 00:10

Oriol


You could use the nth-child selector to target all elements after the first

li:nth-child(n+2):before {
  /* ... */
}
like image 37
Jeff Jenkins Avatar answered Oct 14 '22 02:10

Jeff Jenkins


Add this rule in addition to your current one.

li:before:first-child {
    content: none;
}

Pretty sure that will do it.

like image 29
Katana314 Avatar answered Oct 14 '22 01:10

Katana314