CSS Background Color Only Behind Text

CSS Background Color Only Behind Text
Code Snippet:Inline Background color with multi-line text
Author: Daniel Sturm
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 522
License: MIT
Edit Code online: View on CodePen
Read More

This code creates text with backgrounds in HTML using CSS. It styles headings with colorful backgrounds behind the text. The CSS defines specific background styles for h1 and h2 elements within a “content” class. The h1 heading has a solid background color with a shadow effect, while the h2 heading uses a gradient background that appears on hover.

This code is helpful for enhancing visual appeal by highlighting or emphasizing text content on a webpage using different background styles.

How to Create CSS Background Color Only Behind Text

1. Begin by structuring your HTML content. Place the text within <h1> and <h2> tags inside a <div> element with a class named “content”. You can also use any element for text like paragraphs or tables.

<div class="content">
<h1>A big blog title with a long heading with inline background</h1>

<br><br>

<h2>A title with background color on hover<h2>
</div>

2. Next, delve into the CSS styling to achieve the desired background effects. The code distinguishes between h1 and h2 headings within the “content” class.

.content h1 {
  font-family: Georgia;
  font-weight: bold;
  font-size: 60px;
  max-width: 1400px;
  display: inline;
  background-color: #fcf113;
  box-shadow: 10px 0 0 #fcf113, -10px 0 0 #fcf113;
  -webkit-box-decoration-break: clone;
          box-decoration-break: clone;
}

.content h2 {
  font-family: Georgia;
  font-weight: bold;
  font-size: 60px;
  max-width: 1400px;
  display: inline;
  background-repeat: no-repeat;
  background-image: linear-gradient(180deg, transparent 65%, #fcf113 0);
  background-size: 0% 100%;
  /*transition*/
  transition: background-size .4s ease;
  -webkit-box-decoration-break: clone;
          box-decoration-break: clone;  
}

.content h2:hover {
  background-size: 100% 100%;
}

Adjust the HTML as needed to fit your content. Customize the font, size, and other styling attributes to match your webpage’s theme.

That’s all! hopefully, you have successfully created CSS Background Color Only Behind Text. Experiment with variations to achieve the perfect visual impact for your webpage. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X