I have an inline element nested within another inline element. In this case I am using a b
element nested within a label
. Now when I give both elements a border, I expect the outer element to fully contain the inner element. But when I apply some padding to the inner element, it ends up sticking out of it's parent. I must admit that after almost 2 decades of HTML/CSS, I still don't understand why this happens. Can anyone here:
- Explain why this happens
- Propose a fix
I want both borders to align exactly, without having to specify any dimensions on the label
.
label {
border: 1px solid red;
}
b {
border: 1px solid green;
padding: 20px;
}
<label><b>TEST</b></label>
You might think that label
has some magic that causes this, but AFAICT this happens with all inline elements. For example, when using span
instead of label, we get the exact same effect:
span {
border: 1px solid red;
}
b {
border: 1px solid green;
padding: 20px;
}
<span><b>TEST</b></span>