-1

I have a div which contains text inside of it. I want to add a background-color to the div, but I dont want the background-color to take up the entire row. Just the size of the div.

This is the current state:

div {
  background-color: blue;
}
<div>text is here<div>

this is the wanted state:

enter image description here

Thanks

Rani Giterman
  • 708
  • 2
  • 6
  • 17

2 Answers2

0

Use width: fit-content and it will have the width of what is inside of the element.

.container {
  background-color: yellow;
  width: fit-content;
}
<div class="container"> random text </div>
manjiro sano
  • 784
  • 3
  • 15
0

Set the display to inline-block:

div {
    background-color: blue;
    display: inline-block;
}
Robo Robok
  • 21,132
  • 17
  • 68
  • 126