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 Overflow
🌐
W3Schools
w3schools.com › css › css_text.asp
CSS Text Color
Background Color Background Image Background Repeat Background Attachment Background Shorthand Code Challenge CSS Borders · Border Style Border Width Border Color Border Sides Border Shorthand Rounded Borders Code Challenge CSS Margins · Margins Margin Collapse Code Challenge CSS Padding · Padding Padding box-sizing Code Challenge CSS Height / Width · Height / Width Min / Max Code Challenge CSS Box Model ... Text Color Text Alignment Text Decoration Text Decoration Styles Text Transformation Text Spacing Text Shadow Code Challenge CSS Fonts
Top answer
1 of 16
302

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.

2 of 16
124

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: absolute and translateX

  • requires a position: relative parent

.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.

Discussions

Changing background color of text in LaTeX - TeX - LaTeX Stack Exchange
How can I make change a text background in LaTeX? For example using Latex code \textsc{Extra Curricular Achievements}\ I get However, I want the background of this heading to be changed so that it More on tex.stackexchange.com
🌐 tex.stackexchange.com
October 7, 2013
How can I add a background color to a text like in this example?
I guess it's pretty simple; use the text tool to make whatever, then make a layer between it and the background so you can use the rectangle select and bucket fill it Bonus points if you set the text boxes to 60 - 40% opacity so you can still kinda see whatever's behind More on reddit.com
🌐 r/GIMP
15
5
September 16, 2018
Give text its own colored background?
Yes. Place the text you are trying to add the background in double equal signs. Example: Welcome to ==My Site== Now, if you go to the text settings, you can alter the highlight color. More on reddit.com
🌐 r/Carrd
19
5
June 5, 2024
How to apply background color with css to text with line break
Hi! I would like to create a text background that is just behind the text even when the line breaks, like this: I found a solution on stackoverlfow but I am not able to make it work in WeWeb: I copied the following code from stackoverflow to the custom CSS of my heading in Weweb: background: ... More on community.weweb.io
🌐 community.weweb.io
1
0
November 2, 2024
🌐
Webflow
discourse.webflow.com › design help › general
[Solved] How to add background text color? - General - Forum | Webflow
March 31, 2015 - Hey guys, would somebody teach me how to add a background color to the text, like in this images? My research pointed that I should use a span, but I don’t even know how to actually implement it. Regards, Diogo Fa…
🌐
Adobe
help.adobe.com › en_US › framemaker › using › using-framemaker › user-guide › frm_page_layout_pl-styles-background-color.html
Background color
Select the text in a paragraph. Open the Character Designer. In the Background color drop-down list, choose a color and click Apply.
🌐
Microsoft Support
support.microsoft.com › en-us › office › apply-shading-to-words-or-paragraphs-2020d0e0-f99e-4d53-a895-009077cbcfda
Apply shading to words or paragraphs - Microsoft Support
Shading words or paragraphs gives them a background color that updates when you switch to a different document theme. It's different from highlighting text, which has a very limited choice of colors, and doesn't update when you switch to another theme.
🌐
Coolors
coolors.co
Coolors - The super fast color palettes generator!
Image Picker Extract beautiful colors from your photos and turn them into palettes for your projects. Launch the image picker · Contrast Checker Calculate the contrast ratio of text and background colors to make your content more accessible.
Find elsewhere
🌐
HubSpot
blog.hubspot.com › home › website › how to change text and background color in css
How to Change Text and Background Color in CSS
June 15, 2023 - Its syntax is: element { background-color: [color code]}. Changing text color on a web page is easy with the CSS color property.
Top answer
1 of 4
59

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}
2 of 4
268

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}}

🌐
Lemon and the Sea
lemonandthesea.com › blog › adding-text-on-a-colored-background
Adding Text on a Colored Background
October 21, 2024 - What can you do when you want to add text on top of an image? There are a few solutions, but today I’m focusing on just one: adding a colored background to text. We’re going to do this using the Squarespace Code Block, so you can add it to any section of your website, including on Index ...
Address   P.O. Box 4451 Midlothian, VA 23112
🌐
Reddit
reddit.com › r/carrd › give text its own colored background?
r/Carrd on Reddit: Give text its own colored background?
June 5, 2024 -

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?

🌐
Microsoft Support
support.microsoft.com › en-us › office › add-change-or-delete-the-background-color-in-word-db481e61-7af6-4063-bbcd-b276054a5515
Add, change, or delete the background color in Word - Microsoft Support
You can also add a watermark. Go to Design > Page Color. Choose the color you want under Theme Colors or Standard Colors. If you don't see the color you want, select More Colors, and then choose a color from the Colors box. To add a gradient, texture, pattern, or picture, select Fill Effects, ...
🌐
Contrast-Finder
app.contrast-finder.org
Contrast Finder, find correct color contrasts for web accessibility (WCAG)
WCAG success criteria 1.4.3 requires text to have a minimum contrast ratio of 4.5 (and 3 for large text). Contrast-Finder is a tool which computes the contrast between two colors (background, foreground) and checks if the contrast is valid.
🌐
Natalie Ducey
natalieducey.com › 2022 › 05 › 10 › how-to-add-background-colour-color-to-text-in-canva
How to Add Background Colour (Color) to Text in Canva! – Natalie Ducey
May 19, 2022 - Under Style, you’ll see the option Background. ... Once you click on it, you’ll see the yellow colour added to your text.
🌐
Microsoft Support
support.microsoft.com › en-us › office › change-the-colors-in-a-text-box-or-shape-c3ab6bb7-89f9-4908-912e-e86ea5fd106d
Change the colors in a text box or shape - Microsoft Support
In the Colors box, either click the color you want on the Standard tab, or mix your own color on the Custom tab. Custom colors and colors on the Standard tab aren't updated if you later change the document theme. Select the shape or text box to change.
🌐
Google Support
support.google.com › docs › answer › 13267978
Change the color of text, objects, and backgrounds - Google Docs Editors Help
From Google Docs, Sheets, and Slides, you can: Change the color of text, objects, and backgrounds Create custom colors through HEX values, RGB values, or the eyedropper tool Change the color of
🌐
Adobe Support Community
community.adobe.com › home › app communities › adobe express › questions › using adobe express how do you change the background color of text?
Using adobe express how do you change the background color of text? | Community
January 30, 2024 - The option selected by you (shown in the shared screenshot) highlights the text background; if you choose the option adjacent to that option, it'll cover the text box. Hope this helps! ... If you add more text to that text box, does the red rectangle grow as well? If so, then check the "Shape" option at the bottom of the Text panel when that box is selected. If that rectangle doesn't grow, select the rectangle via the layers list on the right and then change the fill color.
🌐
Tailwind CSS
tailwindcss.com › docs › background-color
background-color - Backgrounds - Tailwind CSS
Use utilities like bg-white, bg-indigo-500 and bg-transparent to control the background color of an element:
🌐
remove.bg
remove.bg › f › add-background-color
Add background color – remove.bg
All you have to do is upload or drag and drop your image to the website and wait a few seconds for the AI to remove its background. Then click on background > color and select the color you prefer.