0

I've come across this strange problem whereby the font-size in one div somehow effects the positioning of the next div. My code is:

<div class="first">
  1
</div>  
<div class="second">
  2
</div>  

And CSS:

.first{
    border: 1px blue solid;
    display: inline-block;
    font-size: 47px;
    height: 80px;
    width: 105px;
}

.second{
    border: 1px red solid;
    height: 80px;
    width: 105px;
    display: inline-block;
}

The result is:

enter image description here

The second div is not horizontally aligned with the first div.

The jsfiddle here. If I remove the font-size, they both align horizontally. Why is this happening?

Mark
  • 4,428
  • 14
  • 60
  • 116

2 Answers2

0

Change The vertical-align value is default baseline

Hi now define your first and second class vertical-align:top as like this

.first,.second{vertical-align:top;}

Demo

.first{
    border: 1px blue solid;
    display: inline-block;
    font-size: 47px;
    height: 80px;
    width: 105px;
}

.second{
    border: 1px red solid;
    height: 80px;
    width: 105px;
    display: inline-block;
}
.first, .second{vertical-align:top;}
<div class="first">
  1
</div><div class="second">
  2
</div>  
Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97
0

It happens because the baselines change when you change the font-size or height. Give vertical-align in your CSS:

.first, .second {vertical-align: top;}

Snippet

.first {
  border: 1px blue solid;
  display: inline-block;
  font-size: 47px;
  height: 80px;
  width: 105px;
}
.second {
  border: 1px red solid;
  height: 80px;
  width: 105px;
  display: inline-block;
}
.first,
.second {
  vertical-align: top;
}
<div class="first">
  1
</div>
<div class="second">
  2
</div>

Preview

Fiddle: https://jsfiddle.net/pgrwg0dw/

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252