0

This is not necessary to get an answer, but it's more of a problem I've encountered. Let's say I have a piece of text and I want that the div element that contains it to resize it width property proportionally to the amount of text. Is there a elegant way to do so? I've read about min-width, but doesn't work in my case.

Justplayit94
  • 385
  • 2
  • 19

2 Answers2

2

Text typically flows to fill it's container. I don't think CSS is going to be the best way to accomplish this. The very concept is too dynamic for the limitations of CSS. The best way that I can think to do this would be to use javascript to adjust the width of the div based on the length of the string contained within.

bboysupaman
  • 1,274
  • 1
  • 13
  • 20
  • Yeah, I was thinking the same. I hoped there will be a way without JS. Thanks for confirming my suspicions. – Justplayit94 Jun 29 '15 at 15:15
  • No problem. When I first started developing, I hated going to JS for anything. I tried to keep things pure CSS wherever possible. But, this is just one of those things that is better accomplished with JS. – bboysupaman Jun 29 '15 at 15:16
0

You may be able to implement a pure CSS solution if you have a target width/height ratio in mind. Take a look at this previous post on the subject.

The trick is to add a parent <div> with padding-bottom applied.

Community
  • 1
  • 1
mac
  • 858
  • 1
  • 6
  • 11