Skip to content

CSS Image Overlay: Overlaying Text and Images for Visual Effect

CSS image overlays are a common technique for transposing text or images over each other. For example, you can combine images and text on a website when captioning an image, or place a live text element over a button.

CSS image overlays can be a solid color, a gradient, a color block with transparency, or a blur.

This post explains how to overlay images with CSS and how to create seven cool overlay effects with only one codeline in Cloudinary, which is tough to do in CSS.

Here are the topics:

To combine image and interactive text elements, for example, menus and buttons, in your website, dynamically overlay text on images in CSS. Note an important caveat, however: the image delivered to the site does not include the overlaid text. Even though the text is visible in their browser, if users download and share the image, the text disappears. Therefore, do not, for example, add your brand to images by means of CSS overlays for watermarks. Instead, turn to Cloudinary for that task. See the details below.

The following example demonstrates the HTML and CSS code that produces and customizes a text overlay in color. Note that you can apply any text properties. Most important of all, align your elements. Generally, set your container element to be position: relative and your text elements to be position: absolute.

See the Pen qBOGygN by Nic-Sevic (@Nic_Sevic) on CodePen.

To ensure that the text in your overlays is readable, adopt one of these techniques:

  • Color overlays, which apply a single color or shade over an image, ideal for muting details and colors. That makes your text sharp since it competes with only one color family.
  • Gradient overlays, which apply a gradient of colors or shades over an image for more visual appeal. Depending on your value settings, these overlays can reduce the number of competing colors behind the text.
  • An image blur, which reduces the details by adding a background for the text without competing edges, making it easier to see where the letters begin and end regardless of color.
  • Background color, which applies a small block of pigment behind the text, leaving the rest of the image unchanged. Use this trick for captions or text on images that remain sharp if certain areas are obscure.

You can also add color filters or backgrounds behind text to improve readability (as described above) or to spruce up visuals. Such overlays offer the added benefit of access-to-hover events, enabling you to make images interactive or to change settings to grab attention.

To create transparent or translucent CSS image overlays, configure the opacity property with a value between 0 (translucent) and 1 (opaque). We recommend that you define RGBA values for opacity so that you can transform child elements separately.

See this example of a standard opacity application (with a 50-percent transparency) for the parent only (also with a 50-percent transparency):

.overlay {opacity: 0.5;} .overlay {background: rgba(76, 175, 80, 0.5);}
Code language: CSS (css)

To tweak the transparency level, add the hover selector to the element in question. The changes you apply are live on a cursor hover of that element. Here’s an example that changes the opacity and color of a sheer-color layer on top of an image:

.overlay { opacity: 0; } .overlay:hover { opacity: 0.25; background-color: yellow; }
Code language: CSS (css)

This example makes use of translucent overlays, hover effects, and text overlays:

<script async src="//jsfiddle.net/digamber89/x4mc2gaz/13/embed/html,css,result/dark/"></script>
Code language: HTML, XML (xml)

To learn more, check out these articles on CSS image effects and resizing.

A cloud-based service for managing images and videos, Cloudinary offers a generous free plan. While on that platform, you can intuitively upload images, apply complex overlays, and automatically position and organize them for images. Cloudinary does all that dynamically without modifying the original images.

Applying filters and effects to images on Cloudinary takes only one line of code, which would be a major challenge in CSS. You do that either with changes to the related URLs or through convenient SDKs, tailored for all popular programming languages.

The list below describes seven overlay effects you can apply to images with Cloudinary. Each of the items links to a resource that contains more details of the effect.

You can display text over an image, automatically positioning the text in different areas. For example, to position the text Hello World at the bottom of the image, code its URL this way:

Loading code examples Text Overlay

To make the positioning more accurate, add the xand y offsets. For example, to position the text northwest of the image with an offset, code the URL like this:

Loading code examples gravity

To position the same image on an automatically detected face there, rotate it and fit the text size to 50 percent of the detected face. Code the URL this way:

Loading code examples facial recognition

To overlay text on an image with a nice transparent background and ensure that the bar is relative to the image width, code the URL this way:

Loading code examples responsive overlay

To add an image overlay with opacity (e.g., one called sample_watermark) to another one (e.g., one called brown_sheep) with Cloudinary’s overlay command (1), code the URL this way:

Loading code examples Stacked image overlay

To shrink a watermark and position it arbitrarily with the ‘gravity’, ‘x’, and ‘y’ transformation commands, see the example URL below, which adds an 80-pixel-wide watermark five pixels from the bottom-right corner of an image that is resized to fill a 300×200-pixel rectangle based on face detection:

Loading code examples automated overlay

You can upload your own custom fonts to Cloudinary for use as overlay text on images (see the procedures in the documentation). Afterwards, this code overlays text over an image with the uploaded font:

Loading code examples custom fonts

You can stack multiple images, one after the other, on top of a base image. Cloudinary can apply eye-catching effects to each of the overlaid images.

The URL below sets the flag fl_layer_apply and performs three transformations:

  1. Display a base image of a coffee mug with the pictures of a handsome couple and a balloon on top of it.
  2. Smartly crop and resize the picture of the couple in addition to adding a blurred border.
  3. Resize the balloon to 30 pixels wide, rotate it, and change the hue level to pink.

Loading code examples Multiple overlays

Besides overlay capabilities for images and text, Cloudinary offers a multitude of robust tools for web developers, including the following:

  • Automated image uploads. You can upload images at scale anywhere from a browser, mobile app, or application back-end directly to the cloud.
  • Generous image storage. Cloudinary accords you up to 25 GB free managed, secure, and cloud-based storage space with multiregion backup, revision history, and disaster recovery.
  • Seamless asset management. You can efficiently manage your image library on Cloudinary by performing tasks like searching, organizing, and tagging files; controlling access; and monitoring usage and performance.
  • Effective image transformation. You can transform, enhance, transcode, crop, scale, and enhance images with a URL-based API or with SDKs that support all popular programming languages.
  • Automated image optimization. Cloudinary automatically selects the optimal quality and encoding settings for images, adapts the settings to any resolution or pixel density, and scales or crops images to focus on the important regions.
  • Responsive images. Cloudinary automatically scales images in an art-directed manner, cropping them to fit different resolutions and viewports.
  • Reliable and fast image delivery. Cloudinary delivers images through Content Delivery Networks (CDNs)—Akamai, Fastly, and CloudFront—with no integration or management on your part.

Do give Cloudinary a try. To start, sign up for a free account.

Want to learn more about CSS images? These articles are an excellent resource:

Back to top

Featured Post