Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove top border of first child and bottom border of last child in CSS

Tags:

html

css

I have the following HTML:

<ul>
<li>FIRST</li>
<li>SECOND</li>
<li>THIRD</li>
<li>FOURTH</li>
<ul>

And the following CSS:

li
{
 border-top:1px solid #FFF;
 border-bottom: 1px solid #D0D0D0;       
}​

How can I remove the top border for the first LI element, and the bottom border for the last LI element? ​

like image 477
Hommer Smith Avatar asked Jun 15 '12 17:06

Hommer Smith


People also ask

How do you remove top and bottom border in CSS?

We can specify the no border property using CSS border: none, border-width : 0, border : 0 properties. Approach 1: We will give border-color, border-style properties to both headings, for showing text with border and no-border. For no border heading, we will use the border-width : 0 which will result in no border.

How do you put a border on top and bottom only in CSS?

You can use the border-left , border-right , border-top and border-bottom properties to give different border styles to each side of your container. In your case, you want to apply your style to border-top and border-bottom .


2 Answers

li:first-child {
  border-top: none;
}
li:last-child {
  border-bottom: none;
}

Just like that.

like image 145
CKKiller Avatar answered Oct 19 '22 21:10

CKKiller


Use the CSS :not selector to apply styles only where required—rather than adding then removing them.

For example,

body {
  background: #acc;
}

li:not(:first-child) {
  border-top: 1px solid #fff;
}

li:not(:last-child) {
  border-bottom: 1px solid #000;
}
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>Fourth</li>
</ul>
like image 38
Reggie Pinkham Avatar answered Oct 19 '22 23:10

Reggie Pinkham