0

I have an element that takes 100% of its container's width:

enter image description here

How do I make it so the width of the element is as wide as the text?

I can't find the answer online because I don't know how to formulate the question proprely.

float: left; works but it messes up the surrounding elements as some of them have this porperty as well. Is there another way?

JOKKER
  • 502
  • 6
  • 21

1 Answers1

2

You can set the display: inline-block OR display: inline (depends on your use case) for the element containing the text like so :

.text {
  display: inline;
  padding: 10px;
  background: firebrick;
}
<div class="text">text</div>

<div class="text">text text text</div>
Hemant Parashar
  • 3,684
  • 2
  • 16
  • 23