[html] How do I set a background-color for the width of text, not the width of the entire element, using CSS?

Option 1

display: table;

  • no parent required

_x000D_
_x000D_
h1 {_x000D_
    display: table; /* keep the background color wrapped tight */_x000D_
    margin: 0px auto 0px auto; /* keep the table centered */_x000D_
    padding:5px;font-size:20px;background-color:green;color:#ffffff;_x000D_
}
_x000D_
<h1>The Last Will and Testament of Eric Jones</h1>
_x000D_
_x000D_
_x000D_

fiddle

http://jsfiddle.net/J7VBV/293/

more

display: table tells the element to behave as a normal HTML table would.

More about it at w3schools, CSS Tricks and here


Option 2

display: inline-flex;

  • requires text-align: center; on parent

_x000D_
_x000D_
.container {_x000D_
    text-align: center; /* center the child */_x000D_
}_x000D_
h1 {_x000D_
    display: inline-flex; /* keep the background color wrapped tight */_x000D_
    padding:5px;font-size:20px;background-color:green;color:#ffffff;_x000D_
}
_x000D_
<div class="container">_x000D_
  <h1>The Last Will and Testament of Eric Jones</h1>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Option 3

display: flex;

  • requires a flex parent container

_x000D_
_x000D_
.container {_x000D_
  display: flex;_x000D_
  justify-content: center; /* center the child */_x000D_
}_x000D_
h1 {_x000D_
    display: flex;_x000D_
    /* margin: 0 auto; or use auto left/right margin instead of justify-content center */_x000D_
    padding:5px;font-size:20px;background-color:green;color:#ffffff;_x000D_
}
_x000D_
    <div class="container">_x000D_
      <h1>The Last Will and Testament of Eric Jones</h1>_x000D_
    </div>
_x000D_
_x000D_
_x000D_

about

Probably the most popular guide to Flexbox and one I reference constantly is at CSS Tricks


Option 4

display: block;

  • requires a flex parent container

_x000D_
_x000D_
.container {_x000D_
  display: flex;_x000D_
  justify-content: center; /* centers child */_x000D_
}_x000D_
h1 {_x000D_
    display: block;_x000D_
    padding:5px;font-size:20px;background-color:green;color:#ffffff;_x000D_
}
_x000D_
<div class="container">_x000D_
  <h1>The Last Will and Testament of Eric Jones</h1>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Option 5

::before

  • requires entering words in css file (not very practical)

_x000D_
_x000D_
h1 {_x000D_
    display: flex; /* set a flex box */_x000D_
    justify-content: center; /* so you can center the content like this */_x000D_
}_x000D_
_x000D_
h1::before {_x000D_
    content:'The Last Will and Testament of Eric Jones'; /* the content */_x000D_
    padding: 5px;font-size: 20px;background-color: green;color: #ffffff;_x000D_
}
_x000D_
<h1></h1>
_x000D_
_x000D_
_x000D_

fiddle

http://jsfiddle.net/J7VBV/457/

about

More about css pseudo elements ::before and ::after at CSS Tricks and pseudo elements in general at w3schools


Option 6

display: inline-block;

  • centering with position: absolute and translateX

  • requires a position: relative parent

_x000D_
_x000D_
.container {_x000D_
  position: relative; /* required for absolute positioned child */_x000D_
}_x000D_
h1 {_x000D_
  display: inline-block; /* keeps container wrapped tight to content */_x000D_
  position: absolute; /* to absolutely position element */_x000D_
  top: 0;_x000D_
  left: 50%; /* part1 of centering with translateX/Y */_x000D_
  transform: translateX(-50%); /* part2 of centering with translateX/Y */_x000D_
  white-space: nowrap; /* text lines will collapse without this */_x000D_
  padding:5px;font-size:20px;background-color:green;color:#ffffff;_x000D_
}
_x000D_
  <h1>The Last Will and Testament of Eric Jones</h1>
_x000D_
_x000D_
_x000D_

about

More on centering with transform: translate(); (and centering in general) in this CSS tricks article


Option 7

text-shadow: and box-shadow:

  • not what the OP was looking for but maybe helpful to others finding their way here.

_x000D_
_x000D_
h1, h2, h3, h4, h5 {display: table;margin: 10px auto;padding: 5px;font-size: 20px;color: #ffffff;overflow:hidden;}_x000D_
h1 {_x000D_
    text-shadow: 0 0 5px green,0 0 5px green,_x000D_
                 0 0 5px green,0 0 5px green,_x000D_
                 0 0 5px green,0 0 5px green,_x000D_
                 0 0 5px green,0 0 5px green;_x000D_
}_x000D_
h2 {_x000D_
    text-shadow: -5px -5px 5px green,-5px 5px 5px green,_x000D_
                  5px -5px 5px green,5px 5px 5px green;_x000D_
}_x000D_
h3 {_x000D_
    color: hsla(0, 0%, 100%, 0.8);_x000D_
    text-shadow: 0 0 10px hsla(120, 100%, 25%, 0.5),_x000D_
                 0 0 10px hsla(120, 100%, 25%, 0.5),_x000D_
                 0 0 10px hsla(120, 100%, 25%, 0.5),_x000D_
                 0 0 5px hsla(120, 100%, 25%, 1),_x000D_
                 0 0 5px hsla(120, 100%, 25%, 1),_x000D_
                 0 0 5px hsla(120, 100%, 25%, 1);_x000D_
}_x000D_
h4 { /* overflow:hidden is the key to this one */_x000D_
    text-shadow: 0px 0px 35px green,0px 0px 35px green,_x000D_
                 0px 0px 35px green,0px 0px 35px green;_x000D_
}_x000D_
h5 { /* set the spread value to something larger than you'll need to use as I don't believe percentage values are accepted */_x000D_
  box-shadow: inset 0px 0px 0px 1000px green;_x000D_
}
_x000D_
<h1>The First Will and Testament of Eric Jones</h1>_x000D_
<h2>The 2nd Will and Testament of Eric Jones</h2>_x000D_
<h3>The 3rd Will and Testament of Eric Jones</h3>_x000D_
<h4>The Last Will and Testament of Eric Jones</h4>_x000D_
<h5>The Last Box and Shadow of Eric Jones</h5>
_x000D_
_x000D_
_x000D_

fiddle

https://jsfiddle.net/Hastig/t8L9Ly8o/

More Options

There are a few other ways to go about this by combining the different display options and centering methods above.