I have several div
s that are all styled as inline-blocks, and therefore sit in a row next to each other. These divs have a max-width
of 140px, and contain text that cause them to vary in width up to that size.
As demonstrated in this fiddle, text that is wider than the max-width
property wraps, as you would expect. The problem is that it also seems to force its container div
to stay at the max-width
, even though the wrapped text doesn't technically require that much space.
Is there a cross-browser, HTML/CSS-only way to "shrink-wrap" these blocks to their smallest width once the text wraps? I'm aware that I can force it with appropriately placed <br>
s, but these blocks are supposed to contain user-entered text.
.block {
display: inline-block;
vertical-align: top;
max-width: 140px;
background-color: lightgrey;
text-align: center;
}
<div class="block">A single line</div>
<div class="block">Two distinctively different lines</div>
<div class="block">A somewhat egregious demonstrative amount of text</div>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…