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?
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.
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.
To select all the children of an element except the last child, use :not and :last-child pseudo classes.
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".
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>
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>
You could use the nth-child selector to target all elements after the first
li:nth-child(n+2):before {
/* ... */
}
Add this rule in addition to your current one.
li:before:first-child {
content: none;
}
Pretty sure that will do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With