DEV Community

Cover image for CSS text background
Cornea Florin
Cornea Florin

Posted on

3

CSS text background

Hello fellows developers,

As a CSS enthusiast I'm always trying to replicate designs I see using CSS.

Today I wanted to create a text background for a paragraph.
I used codepen as always(God Bless Codepen).

At first I added the necessary HTML I knew I wanted on the page:

<section>
  <span class="text-background">2021</span>
  <p>
    “Do the hard jobs first. The easy jobs will take care of themselves.”
Dale Carnegie
  </p>
</section>
Enter fullscreen mode Exit fullscreen mode

Now I want my section to be centered on the screen, a dark background and some nice visuals for the body and for that I've added some CSS:

body {
  margin: 0;
  height: 100vh;
  width: 100%;
  background: #454545;

  display: grid;
  place-items: center;
  text-align: center;
}
Enter fullscreen mode Exit fullscreen mode

For my particular design I want to have all the elements centered so I need to center everything inside the section also:

section {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
}
Enter fullscreen mode Exit fullscreen mode

The relative positioning will help me center and arrange the elements inside without having to mess with z-index(do to the fact of html elements order).

Display grid and Place-items center will center the items inside the section.

And the last and most important thing to do is to add position absolute to our text elements and some styling to look nice:

.text-background {
  position: absolute;
  font-size: 142px;
  color: #3d3d3d;
}

p {
  position: absolute;
  font-size: 20px;
  color: #eee;
}
Enter fullscreen mode Exit fullscreen mode

The final result:

Thank you for reading this article and I'm curios if you have another method to achieve this result.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️