Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collapse the borders of a set of <div> tags using CSS?

Tags:

html

css

I have a grid made of DIV's with a fixed width and a border of 1 px. Now where two DIV's touch each other, the border becomes 2px, obviously.

How can I just get a 1px border in the whole grid?

This is what I mean:

http://jsfiddle.net/Before/4uPtj/

HTML:

<div class="gridcontainer">
  <div class="griditem"></div>
  <!-- 15 more times -->
</div>

CSS:

div.gridcontainer
{
  width: 80px;
  line-height: 0;
}

div.griditem
{
  display: inline-block;            
  border: 1px solid black;
  width: 18px;
  height: 18px;
}
like image 693
ONOZ Avatar asked Apr 05 '12 06:04

ONOZ


People also ask

Which CSS property is used to make table borders collapse?

The border-collapse property in CSS is used to set the borders of the cell present inside the table and tells whether these cells will share a common border or not. Syntax: border-collapse: separate|collapse|initial|inherit; Default Value : Its default value is separate.

How does border-collapse work?

When borders are “collapsed”, adjacent table cells share borders, and the cells at the edges of the table share their borders with the borders of the table itself. The two tables in the image are identical, except that their border model is specified using the border-collapse property.


Video Answer


3 Answers

Try this:

div.griditem
{
    display: inline-block;            
    border: 1px solid black;
    width: 18px;
    height: 18px;
    margin-left: -1px;
    margin-bottom: -1px;
}
like image 188
Tobi Avatar answered Oct 16 '22 20:10

Tobi


Hi you define you your gridcontainer with according to your griditem div

as like this

css

   div.gridcontainer
    {
        width: 76px;
        line-height: 0;
        border: solid black;
        border-width: 0 1px 1px 0;
    }

    div.griditem
    {
        display:inline-block;           
        border: solid black;
        border-width: 1px 0 0 1px;
        width: 18px;
        height: 18px;
    }

HTML

<div class="gridcontainer">
    <div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div><div class="griditem"></div>
</div>

​Live demo here http://jsfiddle.net/rohitazad/4uPtj/1/

like image 17
Rohit Azad Malik Avatar answered Oct 16 '22 20:10

Rohit Azad Malik


Because of the title probably many ppl will end up here looking for solution for an actual css grid layout problem like myself. For them here's a workaround using a combination of grid-gap and box-shadow

.bar {
    max-width: 200px;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    grid-gap: 1px;
}
.foo {
  box-shadow: 0 0 0 1px #000;
}
<div class="bar">
    <div class="foo">1</div>
    <div class="foo">2</div>
    <div class="foo">3</div>
    <div class="foo">4</div>
    <div class="foo">5</div>
    <div class="foo">6</div>
    <div class="foo">7</div>
    <div class="foo">8</div>
    <div class="foo">9</div>
</div>
like image 7
Laszlo Avatar answered Oct 16 '22 20:10

Laszlo