The documentation for block-level elements specifies that each block element will start on a new line even if there is enough space left, but what if there is an inline element after the block element? I tried it in my local with below:
<div className="border-2 border-blue-400 w-1/6">Line 1</div>
<span className="border-2 border-emerald-500">Line 2</span>
The above is resulting in two lines, even though there is enough space to fit both elements on the same line. Why is Line 2
being rendered on a separate line?
The documentation for block-level elements specifies that each block element will start on a new line even if there is enough space left, but what if there is an inline element after the block element? I tried it in my local with below:
<div className="border-2 border-blue-400 w-1/6">Line 1</div>
<span className="border-2 border-emerald-500">Line 2</span>
The above is resulting in two lines, even though there is enough space to fit both elements on the same line. Why is Line 2
being rendered on a separate line?
1 Answer
Reset to default 1To achieve what you want to achieve, style your <div>
with display: inline-block
.
div {
border: 2px solid blue;
width: 200px;
display: inline-block;
}
span {
border: 2px solid red;
}
<div>Line 1</div>
<span>Line 2</span>
For more information on the various values of the display
property and what they do, this article explains it fairly thoroughly.
display: block
) always consume all of the space in the inline direction, even when you specify a width. – Brett Donald Commented Mar 5 at 1:18