Put the text in an inline element, such as a <span>.
<h1><span>The Last Will and Testament of Eric Jones</span></h1>
And then apply the background color on the inline element.
h1 {
text-align: center;
}
h1 span {
background-color: green;
}
An inline element is as big as its contents is, so that should do it for you.
Answer from BalusC on Stack OverflowPut the text in an inline element, such as a <span>.
<h1><span>The Last Will and Testament of Eric Jones</span></h1>
And then apply the background color on the inline element.
h1 {
text-align: center;
}
h1 span {
background-color: green;
}
An inline element is as big as its contents is, so that should do it for you.
Option 1
display: table;
- no parent required
h1 {
display: table; /* keep the background color wrapped tight */
margin: 0px auto 0px auto; /* keep the table centered */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<h1>The Last Will and Testament of Eric Jones</h1>
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
.container {
text-align: center; /* center the child */
}
h1 {
display: inline-flex; /* keep the background color wrapped tight */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
Option 3
display: flex;
- requires a flex parent container
.container {
display: flex;
justify-content: center; /* center the child */
}
h1 {
display: flex;
/* margin: 0 auto; or use auto left/right margin instead of justify-content center */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
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
.container {
display: flex;
justify-content: center; /* centers child */
}
h1 {
display: block;
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
Option 5
::before
- requires entering words in css file (not very practical)
h1 {
display: flex; /* set a flex box */
justify-content: center; /* so you can center the content like this */
}
h1::before {
content:'The Last Will and Testament of Eric Jones'; /* the content */
padding: 5px;font-size: 20px;background-color: green;color: #ffffff;
}
<h1></h1>
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: absoluteandtranslateXrequires a
position: relativeparent
.container {
position: relative; /* required for absolute positioned child */
}
h1 {
display: inline-block; /* keeps container wrapped tight to content */
position: absolute; /* to absolutely position element */
top: 0;
left: 50%; /* part1 of centering with translateX/Y */
transform: translateX(-50%); /* part2 of centering with translateX/Y */
white-space: nowrap; /* text lines will collapse without this */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<h1>The Last Will and Testament of Eric Jones</h1>
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.
h1, h2, h3, h4, h5 {display: table;margin: 10px auto;padding: 5px;font-size: 20px;color: #ffffff;overflow:hidden;}
h1 {
text-shadow: 0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green;
}
h2 {
text-shadow: -5px -5px 5px green,-5px 5px 5px green,
5px -5px 5px green,5px 5px 5px green;
}
h3 {
color: hsla(0, 0%, 100%, 0.8);
text-shadow: 0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 5px hsla(120, 100%, 25%, 1),
0 0 5px hsla(120, 100%, 25%, 1),
0 0 5px hsla(120, 100%, 25%, 1);
}
h4 { /* overflow:hidden is the key to this one */
text-shadow: 0px 0px 35px green,0px 0px 35px green,
0px 0px 35px green,0px 0px 35px green;
}
h5 { /* set the spread value to something larger than you'll need to use as I don't believe percentage values are accepted */
box-shadow: inset 0px 0px 0px 1000px green;
}
<h1>The First Will and Testament of Eric Jones</h1>
<h2>The 2nd Will and Testament of Eric Jones</h2>
<h3>The 3rd Will and Testament of Eric Jones</h3>
<h4>The Last Will and Testament of Eric Jones</h4>
<h5>The Last Box and Shadow of Eric Jones</h5>
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.
Changing background color of text in LaTeX - TeX - LaTeX Stack Exchange
How can I add a background color to a text like in this example?
Give text its own colored background?
How to apply background color with css to text with line break
Videos
I prefer using tcolorbox thinking that in future you may want the background to be fashionable. I have given many options (which are not needed for this particular case) in the tcbset so that you can play with them to suit your needs.
\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbset{
frame code={},
center title,
left=0pt,
right=0pt,
top=0pt,
bottom=0pt,
colback=gray!70,
colframe=white,
width=\dimexpr\textwidth\relax,
enlarge left by=0mm,
boxsep=5pt,
arc=0pt,outer arc=0pt,
}
\begin{document}
\begin{tcolorbox}
\textsc{Extra Curricular Achievements}
\end{tcolorbox}
\end{document}

Here is another option using framed package.
\documentclass{article}
\usepackage{xcolor}
\usepackage{framed}
\definecolor{shadecolor}{RGB}{180,180,180}
\begin{document}
\begin{snugshade*}
\noindent\textsc{Extra Curricular Achievements}
\end{snugshade*}
\end{document}

Without extra packages:
\documentclass{article}
\usepackage{xcolor}
\definecolor{shadecolor}{RGB}{150,150,150}
\begin{document}
\noindent\colorbox{shadecolor}
{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\textsc{Extra Curricular Achievements}}}
\end{document}

And convert it in to a macro:
\documentclass{article}
\usepackage{xcolor}
\definecolor{shadecolor}{RGB}{150,150,150}
\newcommand{\mybox}[1]{\par\noindent\colorbox{shadecolor}
{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1}}}
\begin{document}
\mybox{\textsc{Extra Curricular Achievements}}
\end{document}
I know this question has been answered very extensively. But not what I wanted or thought was the question based on the title. Therefore if others get in here looking for a possibility for colouring behind a word than this snippet is much easier:
\colorbox{blue!30}{blue}
or
\textcolor{blue!30}{blue}
resulting in:

This is possible by only adding \usepackage{xcolor}. Just some extra info :)
Colour Several Lines It is correct that the above methods does not work for several lines, if you to be more than one line you can do:
{\color{green} the text you want to write}
This can however also be wrapped in a function so it is easier to use several places during edits, e.g., for colouring new text or whatever:
\newcommand{\added}[1]{{\color{green}[added]:#1}}
I couldn't really find a guide for what I am trying to do, so I thought to ask here. I am wanting to put the text in its own colored background. However, it is in a container with a picture and whatnot too, which I want the text to be beside.
I don't want to affect the color of the overall container, just the bit where the block of text is. Is this possible?