Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS ul li: Avoiding double borders

I have a UL with

border: 1px solid grey;

it contains several LIs with

border-bottom: 1px dotted grey;

to keep the items apart visually. But now the last LI has the dotted border and the UL solid border! This looks annoying. How can I avoid that? Is there a way to put borders between LIs instead of after them?

like image 649
MrBubbles Avatar asked Nov 12 '10 14:11

MrBubbles


People also ask

How do you avoid double border in CSS?

Add a border-left and border-top to the parent. Add border-right and border-bottom to each of the children.

How will you handle a multiple borders in CSS?

Multiple borders in CSS can be done by box-shadow property. Generally, we can get a single border with border property. Box-shadow property is not for multiple borders, but still, we are creating multiple borders with box-shadow property.

How do I reduce the space between text and borders?

You can set all the values for border "spacing" to zero in the Border and Shading Options dialog box (click the arrow on the Border button on the Home tab, click Borders and Shading, and then click the Options button, found on the Borders tab).


2 Answers

A smooth solution is to use the plus (+) selector to style the list:

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
</ul>

You just add the following css:

li + li {
     border-top: 1px dotted grey;
}

You avoid adding an extra selector and are able to keep the code more clean. Though depending on your needs you might want to check browser compatibilty first.

like image 76
Mikkel R. Lund Avatar answered Sep 29 '22 01:09

Mikkel R. Lund


CSS3 selectors can target :first-child or :last-child, like this:

ul {
    border: 1px solid grey;
}
li {
    border-bottom: 1px dotted grey;
}
li:last-child {
    border:none;
}

A working example: http://api.fatherstorm.com/test/4165384.php

like image 22
FatherStorm Avatar answered Sep 29 '22 01:09

FatherStorm