The height of the button is greater than the height of the nested content
I am using bootstrap 4 alpha.
<button class="btn btn-link p-0">
<div style="display:inline-block; background-color:#c2f5ff; width: 100px; height: 100px;">
</div>
</button>
I have nested a div inside a button. I was setting height and width on a div. My buttons width fits the div, but the button height is larger than it needs to be. When you click the button, the blue outline doesn't match the content.
why does this behavior occur?
source to share
It has to do with character inline-block
. Internal button div
- inline-block
. A default value vertical-align: baseline
that creates an extra space. If you have installed for you div
, then the value vertical-align
other than baseline
( top
, middle
, bottom
), would be expected 102x102
size (width Content + 1px
boundaries).
Explanation about vertical-align: baseline
from this answer :
How browsers evaluate the property vertical-align
on baseline
by default, this is the default behavior. The following illustration shows where the baseline is in the text:
Elements aligned to the baseline should contain space for descenders , which extend below the baseline (eg j
, p
, g
etc.), as you can see in the above image.
So just remove display: inline-block
from your internal div
to see the expected result.
source to share
It's just that the button isn't 100px
wide or tall.
You need to set the size of the button, then make a div inside, fill the button.
body {
margin: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<button class="btn btn-link p-0" style="border-style:none;width:100px;height:100px;">
<div style="background-color:#c2f5ff; width: 100%; height: 100%;">
</div>
</button>
We do this by setting width:100px
and height:100px
on the button itself, and then making the div inside fill the available width and height with width:100%
and height:100%
.
There is also a 1px
general border around the button, which you should remove withborder-style:none
source to share