0

I am trying to figure out if there is a way to force an inline-block element to fit the true size of the text inside of it, but I can't find any way to do this. Once text reaches the end of line and wraps to a new one, the inline-block width appears to automatically stay at the maximum allowed width of the container. If you insert a < br > or otherwise manually line break, the size of the inline-block is what I expect it to be.

Unfortunately, my use case does not allow me to manually update the content and also must be responsive, so line breaks may occur between different words at different viewport widths or devices--so I'm hopeful that I can accomplish this with CSS or reliably with JS.

The snippet below illustrates this. The grey text does not line wrap, and therefore is exactly the width of the contained text. The red text does line wrap, and maintains the maximum width of the container even though there is white space to the right of both the first and second lines. The green text acts like I want it to, but I have used a < br > just for demonstration.

Is there any way to make an element actually match the size of the text inside it? Any help would be appreciated, or a definitive no would also be great so I can stop searching haha.

.container {
  width: 200px;
  background-color: #000;
}

.ib {
  display: inline-block;
}

.grey {
  background-color: #ccc;
}

.red {
  background-color: #e00;
}

.green {
  background-color: #0e0;
}
<div class="container">
  <span class="ib grey">
    Short text.
  </span>
  
  <span class="ib red">
    Longer text with alongwordforanewline.
  </span>
  
  <span class="ib green">
    Longer text with<br />alongwordforanewline.
  </span>
</div>
Nicholas
  • 151
  • 2
  • 10
  • `white-space: shrinkwrap;` might be what you're looking for. Unfortunately it doesn't exist. – John Boker Sep 26 '19 at 18:01
  • I'm sure this can be accomplished with flexbox – GammaGames Sep 26 '19 at 18:04
  • @GammaGames This may be one of the few cases where I can use flexbox, though typically we have to support all the way back to IE8. My understanding of flexbox is that I would also need an additional wrapper element, right? I think I'll get to work on trying some of that out, because I think some extra space on an IE8 fallback wouldn't be terrible. Thanks for the suggestion. – Nicholas Sep 26 '19 at 18:12
  • @Nicholas Oof that's some hellish support, I'm sorry to hear about that. You also might be able to use some `display: table` magic – GammaGames Sep 26 '19 at 18:23
  • 2
    you can stop searching, even with flexbox this is not possible unless you consider very particular cases with a lot of hacks – Temani Afif Sep 26 '19 at 19:24
  • @TemaniAfif thank you, glad to have the answer at least! – Nicholas Sep 26 '19 at 20:29
  • 1
    @Nicholas this was marked as a duplicate and the duplicate has a potential answer with css grid: https://stackoverflow.com/a/50718701/3903479 Unfortunately it doesn't have great support on IE – GammaGames Sep 27 '19 at 14:07
  • 1
    @GammaGames you hero, you. Thanks for pointing this out, that is absolutely the best solution available at the moment--at least that I'm aware of. Honestly not much to learn here either, so it's nice that I won't have to read a book to familiarize myself with these properties haha. – Nicholas Sep 27 '19 at 15:54

0 Answers0