Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Borders only between elements

Tags:

css

border

I need to know how to make a borders between my items like the following image:

enter image description here

I tried making it using border-right and -left but the last item shouldn't have a border-right.

My CSS:

border-top: 1px solid #000;
border-right: 1px solid #000;

How can I apply border-right to all but the last element on the row?

like image 427
TheDarkCode Avatar asked Jul 16 '14 23:07

TheDarkCode


People also ask

How do you make a border within an element?

Answer: Use the CSS box-shadow property If you want to place or draw the borders inside of a rectangular box there is a very simple solution — just use the CSS outline property instead of border and move it inside of the element's box using the CSS3 outline-offset property with a negative value.

Is the space between element content and its border?

Padding is the distance between the element's content area and any border that might be applied to the element. Margin is extra space around the element.


2 Answers

There is a better way to do it that works in older browsers: http://jsfiddle.net/mxV92/.

You simply apply a border on the left for every item that immediately follows another item:

ul > li + li {
    margin-left: 5px;
    padding-left: 5px;
    border-left: 1px solid #bbb;
}
like image 178
DRD Avatar answered Sep 20 '22 06:09

DRD


If I understand correctly, what you want is to have borders on the right of all the items, except the last item.

You can use the 'last-child' selector for that. For example, if your objects were in a 'div' with the class 'foo', your CSS might look like:

div.foo {
   border-width: 1px 1px 0 0;
   border-color: #000;
   border-style: solid;
}

div.foo:last-child { border-width: 1px 0 0 0; }

This says that divs of class 'foo' should have solid black borders, with a width of 1px on top and right ('border-width' is followed by widths in the order top, right, bottom, left), except on the last item, where the width is '1px' only on the top.

':last-child' should be supported by most modern browsers.

like image 25
slamci Avatar answered Sep 19 '22 06:09

slamci