14

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?

0

1 Answer 1

17

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

Sign up to request clarification or add additional context in comments.

1 Comment

display: inline-block was the key. thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.