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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.