Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a div box with dynamic width based on text content? [duplicate]

Tags:

html

css

I want to create a simple div that shrinks and expands according to the content contained.

https://jsfiddle.net/qa5dr0x8/

<body>
  <div style="border: 1px solid red; padding: 15px; width:auto;">
    test text
  </div>
</body>

Result: the red box expands to the full width of the page. Why?

like image 291
membersound Avatar asked Jan 13 '16 12:01

membersound


Video Answer


1 Answers

Try this:

<div style="border: 1px solid red; padding: 15px; min-width:100px; display: inline-block">
  test text
</div>

Here's an explanation on display: inline & display: inline-block - a good read to learn

like image 130
StudioTime Avatar answered Sep 22 '22 06:09

StudioTime