Transparent Text Background Image In HTML CSS – Simple Examples

Welcome to a quick tutorial and examples on how to create transparent text, background, and images in HTML CSS. Once upon a time in the Dark Ages of the Internet, we have to manually create GIF or PNG images to do transparency. Today, things are very easy to modern CSS.

  1. To create transparent text in HTML and CSS, simply define the color using rgba. For example, p.ghost { color: rgba(0, 0, 0, 0.5); }
  2. To create a transparent background, we can also define the background using rgba. For example, div.ghost { background: rgba(0, 0, 0, 0.5); }
  3. Lastly, we can create transparent images and just about any transparent element using opacity. For example, img.ghost { opacity: 0.5; }

That covers the quick basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Transparent HTML Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

TRANSPARENT HTML ELEMENTS

All right, let us now get into the examples of how to create transparent elements in HTML and CSS.

 

1) TRANSPARENT TEXT

1-text.html
<!-- (A) THE HTML -->
<div id="demoA">
  <img src="image.jpg"/>
  <div id="demoAT">Hello World!</div>
</div>
 
<!-- (B) THE CSS -->
<style>
/* (B1) CONTAINER */
#demoA {
  position: relative;
  width: 300px;
}
 
/* (B2) TEXT BLOCK */
#demoAT {
  /* POSITION + DIMENSIONS */
  position: absolute;
  top: 30px; left: 0;
  width: 100%;
 
  /* TEXT STYLES */
  font-size: 32px;
  font-weight: 700;
  text-align: center;
 
  /* TRANSPARENT TEXT */
  color: rgba(0, 8, 255, 0.5);
}
</style>

Hello World!

Creating transparent text is that simple – color: rgba(0, 8, 255, 0.5). For those who are new to the color system in the cyber world:

  • RGB stands for the basic colors – red, green, blue.
  • A stands for alpha, or “transparency” in layman terms.
  • So how this works is very simple, we mix and match RGB (a number from 0 to 255) to get different colors. Then, the alpha is a number from 0 (invisible) to 1 (opaque).

 

 

2) TRANSPARENT BACKGROUND

2-background.html
<!-- (A) THE HTML -->
<div id="demoB">
  <img src="image.jpg"/>
  <div id="demoBT">Hello World!</div>
</div>

<!-- (B) THE CSS -->
<style>
/* (B1) CONTAINER */
#demoB {
  position: relative;
  width: 300px;
}

/* (B2) TEXT BLOCK */
#demoBT {
  /* POSITION + DIMENSIONS */
  position: absolute;
  bottom: 0; left: 0;
  width: 100%;
  padding: 10px;
  box-sizing: border-box;

  /* TEXT STYLES */
  font-size: 32px;
  font-weight: 700;
  text-align: center;
  color: #fff;

  /* TRANSPARENT BACKGROUND */
  background: rgba(0, 20, 255, 0.4);
}
</style>

Hello World!

Yep, it’s the same rgba here. Except that we define it to the background instead.

 

 

3) TRANSPARENT IMAGE

3-back-img.html
<!-- (A) THE HTML -->
<div id="demoC">
  <img id="demoCI" src="image.jpg"/>
  <div id="demoCT">Hello World!</div>
</div>
 
<!-- (B) THE CSS -->
<style>
/* (B1) CONTAINER */
#demoC {
  position: relative;
  width: 300px;
}
#demoCI, #demoCT { position: absolute; }

/* (B2) BACKGROUND IMAGE */
#demoCI {
  z-index: 1;
  opacity: 0.5;
}

/* (B3) TEXT BLOCK */
#demoCT {
  /* POSITION + DIMENSIONS */
  z-index: 2;
  top: 30px; left: 0;
  width: 100%;

  /* TEXT STYLES */
  font-size: 32px;
  font-weight: 700;
  text-align: center;
}
</style>

Hello World!

Lastly, we can use opacity to change the transparency of almost any HTML element. Some of you smart code ninjas may be thinking that background-image makes more sense here – But no. As at the time of writing, there is no such thing as background-opacity. So it’s back to the old-school way of layering elements to create the transparent background image.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Transparent Text Image Background (click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *