Yacomini
Yacomini

Reputation: 157

CSS borders problems

Im currently trying to make a square with 4 small squares inside, and I have been having troubles with a way I was trying to do. So this is the code:

#grandbox {
  position: absolute;
  width: 204px;
  height: 204px;
  border: solid thin black;
  left: 40%;
  top: 8%;
}
div.smallbox {
  border: solid thin black;
  text-align: center;
  width: 100px;
  height: 100px;
  float: left;
  line-height: 100px;
}
<div id="grandbox">
  <div class="smallbox"></div>
  <div class="smallbox"></div>
  <div class="smallbox"></div>
  <div class="smallbox"></div>
</div>

I wanted to make the css style of the borders:

border: 2px solid black

But if I do that the boxes just break out of the bigger box and are display vertically.

I'm pretty newbie with this, as I currently started my carreer, but I cannot understand why doesn't it work.

PS: Sorry if bad english, not my first language.

Upvotes: 2

Views: 143

Answers (3)

Adam
Adam

Reputation: 518

EDIT: My answer is more of a hack solution. The accepted answer above that incorporates the box-sizing automatically including borders into the width is a better answer.

In your original calculation of height and width (204) I don't think you were accounting for both sides of each square being an additional 4 pixels larger.

Adjusting the width and height to 208px should solve your problem.

#grandbox
    {
        position: absolute;
        width:208px;
        height:208px;
        border: 2px solid black;
        left:40%;
        top: 8%;
    }

div.smallbox
    {
        border: 2px solid black;
        text-align: center;
        width: 100px;
        height: 100px;
        float: left;
        line-height: 100px;

    }
<body>
  <div id="grandbox">
    <div class="smallbox">

    </div>
    
    <div class="smallbox">

    </div> 
    
    <div class="smallbox">

    </div>
    
    <div class="smallbox">

    </div>
  </div>
</body>

Upvotes: 2

Tiberiu Petcu
Tiberiu Petcu

Reputation: 852

The outer box should be positioned relative and the four inside boxes absolute. Then you just need to position them using left right top bottom properties.

#grandbox {
  position: relative;
  width: 204px;
  height: 204px;
  border: solid thin black;
  left: 40%;
  top: 8%;
}
div.smallbox {
  border: solid thin black;
  text-align: center;
  position: absolute;
  width: 100px;
  height: 100px;
  float: left;
  line-height: 100px;
}
div.sb1 {
  top: 0;
  left: 0;
}
div.sb2 {
  top: 0;
  right: 0;
}
div.sb3 {
  left: 0;
  bottom: 0;
}
div.sb4 {
  right: 0;
  bottom: 0;
}
<div id="grandbox">
  <div class="smallbox sb1">

  </div>
  <div class="smallbox sb2">

  </div>
  <div class="smallbox sb3">

  </div>
  <div class="smallbox sb4">

  </div>
</div>

Here's a jsbin version.

Upvotes: 1

andreas
andreas

Reputation: 16946

Normally, border widths are added to the given width. With the box-sizing: border-box; rule, you can include the border into the width, so that you have no break anymore. See this snippet:

#grandbox {
  position: absolute;
  width: 200px;
  height: 200px;
  border: solid thin black;
  left: 40%;
  top: 8%;
}
div.smallbox {
  border: solid thin black;
  text-align: center;
  width: 100px;
  height: 100px;
  float: left;
  line-height: 100px;
  box-sizing: border-box;
}
<div id="grandbox">
  <div class="smallbox"></div>
  <div class="smallbox"></div>
  <div class="smallbox"></div>
  <div class="smallbox"></div>
</div>

See https://developer.mozilla.org/de/docs/Web/CSS/box-sizing for more information about box-sizing.

Upvotes: 3

Related Questions