2

The general problem I am trying to solve is the alignment of stacked boxes (<div>) when some of them have a different border width.

Consider the two boxes below

.box {
  border-style: solid;
  border-color: green;
  width: 100px;
  height: 100px;
}

.thick {
  border-width: 30px;
}
<div class="box"></div>
<div class="box thick"></div>

They are both of the same sizes, but the border is growing out of the box, breaking their alignment.

I can compensate for this with some tricks (shifting the box, having a thick transparent border, ...) but I would ideally like to keep the total size fixed, and grow the border "in".

Is this possible?

WoJ
  • 27,165
  • 48
  • 180
  • 345

3 Answers3

2

Maybe I do not understand what you mean, but if I do, you could maybe accomplish what you want with the "box-shadow" property. Like this:

.box {
  border-style: solid;
  border-color: green;
  width: 100px;
  height: 100px;
}

.thick {
  box-shadow: inset 0px 0px 0px 29px green;
}
<div class="box"></div>
<div class="box thick"></div>
Leonardum
  • 444
  • 4
  • 8
2

There is a way of accomplishing this by using the outline and outline-offset rule as shown in the example below.

*{
   box-sizing: border-box;
 }

.box {
  border-style: solid;
  border-color: green;
  width: 100px;
  height: 100px;
}

.thick {
   outline: 10px solid red;
   outline-offset: -10px;
}
<div class="box"></div>
<div class="box thick"></div>

EDIT I also highly suggest you always use the border-box as shown in the snippet. This documentation about box-sizing will explain it more in detail.

2

.box {
  border-style: solid;
  border-color: green;
  width: 100px;
  height: 100px;
  box-sizing: border-box;
 -moz-box-sizing: border-box;
 -webkit-box-sizing: border-box;
}

.thick {
  border-width: 30px;
  margin-top: 10px;
}
<div class="box"></div>
<div class="box thick"></div>
Arezou Saremian
  • 508
  • 3
  • 8