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:
- Overlaying text on images in CSS
- Rendering text on images readable
- Adding transparent overlays to images
- Creating image-overlay effects in Cloudinary
- Cloudinary: Much More than Image Overlays
- Want to learn more about CSS images?
Overlaying Text on Images in CSS
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.
Rendering Text on Images Readable
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.
Adding Transparent Overlays to Images
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):
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; }
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>
To learn more, check out these articles on CSS image effects and resizing.
Creating Image-Overlay Effects in Cloudinary
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.
Text overlay with gravity
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:
To make the positioning more accurate, add the x
and y
offsets. For example, to position the text northwest of the image with an offset, code the URL like this:
Text overlay with face recognition
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:
(new ImageTag('face_center.jpg')) ->overlay( Overlay::source( Source::text('Hello World', (new TextStyle('Arial', 45)) ->fontWeight(FontWeight::bold())) ->transformation((new ImageTransformation()) ->rotate(Rotate::byAngle(19)) ->resize(Resize::scale()->width(0.5)->regionRelative()))) ->position((new Position()) ->gravity(Gravity::focusOn(FocusOn::face())) ));
Text overlay with responsiveness
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:
cl_image_tag("lupine.jpg", array("transformation"=>array( array("overlay"=>"black_bar", "width"=>"1.0", "height"=>"0.1", "flags"=>"relative", "opacity"=>60), array("overlay"=>array("font_family"=>"Arial", "font_size"=>150, "font_weight"=>"bold", "text"=>"Cloudinary"), "color"=>"white") )))
(new ImageTag('lupine.jpg')) ->overlay( Overlay::source(Source::image('black_bar') ->transformation((new ImageTransformation()) ->resize(Resize::scale()->width(1.0)->height(0.1)->relative()) ->adjust(Adjust::opacity(60))))) ->overlay( Overlay::source( Source::text('Cloudinary', (new TextStyle('Arial', 150)) ->fontWeight(FontWeight::bold())) ->textColor(Color::WHITE) ));
cloudinary.url().transformation(new Transformation() .overlay(new Layer().publicId("black_bar")).width(1.0).height(0.1).flags("relative").opacity(60).chain() .overlay(new TextLayer().fontFamily("Arial").fontSize(150).fontWeight("bold").text("Cloudinary")).color("white")).imageTag("lupine.jpg");
cloudinary.imageTag('lupine.jpg', {transformation: [ {overlay: new cloudinary.Layer().publicId("black_bar"), width: "1.0", height: "0.1", flags: "relative", opacity: 60}, {overlay: new cloudinary.TextLayer().fontFamily("Arial").fontSize(150).fontWeight("bold").text("Cloudinary"), color: "white"} ]}).toHtml();
$.cloudinary.image("lupine.jpg", {transformation: [ {overlay: new cloudinary.Layer().publicId("black_bar"), width: "1.0", height: "0.1", flags: "relative", opacity: 60}, {overlay: new cloudinary.TextLayer().fontFamily("Arial").fontSize(150).fontWeight("bold").text("Cloudinary"), color: "white"} ]})
cloudinary.Api.UrlImgUp.Transform(new Transformation() .Overlay(new Layer().PublicId("black_bar")).Width(1.0).Height(0.1).Flags("relative").Opacity(60).Chain() .Overlay(new TextLayer().FontFamily("Arial").FontSize(150).FontWeight("bold").Text("Cloudinary")).Color("white")).BuildImageTag("lupine.jpg")
MediaManager.get().url().transformation(new Transformation() .overlay(new Layer().publicId("black_bar")).width(1.0).height(0.1).flags("relative").opacity(60).chain() .overlay(new TextLayer().fontFamily("Arial").fontSize(150).fontWeight("bold").text("Cloudinary")).color("white")).generate("lupine.jpg");
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation() .setOverlay("black_bar").setWidth(1.0).setHeight(0.1).setFlags("relative").setOpacity(60).chain() .setOverlay("text:Arial_150_bold:Cloudinary").setColor("white")).generate("lupine.jpg")!, cloudinary: cloudinary)
Stacked image overlays
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:
Image overlay with automated positioning and face detection
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 300x200-pixel rectangle based on face detection:
(new ImageTag('face_center.jpg')) ->resize(Resize::fill()->width(300)->height(200)->gravity(Gravity::focusOn(FocusOn::face()))) ->overlay( Overlay::source(Source::image('cloudinary_icon') ->transformation((new ImageTransformation()) ->resize(Resize::scale()->width(80)) ->adjust(Adjust::opacity(50)) ->adjust(Adjust::brightness()->level(200)))) ->position((new Position()) ->gravity(Gravity::compass(Compass::southEast())) ->offsetX(5)->offsetY(5) ));
<cl-image public-id="face_center.jpg" > <cl-transformation width="300" height="200" gravity="face" crop="fill"> </cl-transformation> <cl-transformation width="80" gravity="south_east" x="5" y="5" overlay="cloudinary_icon" opacity="50" effect="brightness:200"> </cl-transformation> </cl-image>
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation() .setWidth(300).setHeight(200).setGravity("face").setCrop("fill").chain() .setWidth(80).setGravity("south_east").setX(5).setY(5).setOverlay("cloudinary_icon").setOpacity(50).setEffect("brightness:200")).generate("face_center.jpg")!, cloudinary: cloudinary)
Custom fonts
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:
Overlays with multiple images and effects
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:
- Display a base image of a coffee mug with the pictures of a handsome couple and a balloon on top of it.
- Smartly crop and resize the picture of the couple in addition to adding a blurred border.
- Resize the balloon to 30 pixels wide, rotate it, and change the hue level to pink.
cl_image_tag("coffee_cup.jpg", :transformation=>[ {:width=>400, :height=>250, :gravity=>"south", :crop=>"fill"}, {:overlay=>"nice_couple", :width=>1.3, :height=>1.3, :gravity=>"faces", :flags=>"region_relative", :crop=>"crop"}, {:effect=>"saturation:50"}, {:effect=>"vignette"}, {:flags=>"layer_apply", :width=>100, :radius=>"max", :gravity=>"center", :y=>20, :x=>-20, :crop=>"scale"}, {:overlay=>"balloon", :width=>30}, {:effect=>"hue:-20", :angle=>5}, {:flags=>"layer_apply", :x=>30, :y=>5} ])
cl_image_tag("coffee_cup.jpg", array("transformation"=>array( array("width"=>400, "height"=>250, "gravity"=>"south", "crop"=>"fill"), array("overlay"=>"nice_couple", "width"=>"1.3", "height"=>"1.3", "gravity"=>"faces", "flags"=>"region_relative", "crop"=>"crop"), array("effect"=>"saturation:50"), array("effect"=>"vignette"), array("flags"=>"layer_apply", "width"=>100, "radius"=>"max", "gravity"=>"center", "y"=>20, "x"=>-20, "crop"=>"scale"), array("overlay"=>"balloon", "width"=>30), array("effect"=>"hue:-20", "angle"=>5), array("flags"=>"layer_apply", "x"=>30, "y"=>5) )))
(new ImageTag('coffee_cup.jpg')) ->resize(Resize::fill()->width(400)->height(250)->gravity(Gravity::compass(Compass::south()))) ->overlay( Overlay::source(Source::image('nice_couple') ->transformation((new ImageTransformation()) ->resize(Resize::crop()->width(1.3)->height(1.3) ->gravity(Gravity::focusOn(FocusOn::faces())) ->regionRelative()) ->adjust(Adjust::saturation()->level(50)) ->effect(Effect::vignette()) ->resize(Resize::scale()->width(100)) ->roundCorners(RoundCorners::max()))) ->position((new Position()) ->gravity(Gravity::compass(Compass::center())) ->offsetX(-20)->offsetY(20))) ->overlay( Overlay::source(Source::image('balloon') ->transformation((new ImageTransformation()) ->resize(Resize::scale()->width(30)) ->rotate(Rotate::byAngle(5)) ->adjust(Adjust::hue()->level(-20)))) ->position((new Position()) ->offsetX(30)->offsetY(5) ));
CloudinaryImage("coffee_cup.jpg").image(transformation=[ {'width': 400, 'height': 250, 'gravity': "south", 'crop': "fill"}, {'overlay': "nice_couple", 'width': "1.3", 'height': "1.3", 'gravity': "faces", 'flags': "region_relative", 'crop': "crop"}, {'effect': "saturation:50"}, {'effect': "vignette"}, {'flags': "layer_apply", 'width': 100, 'radius': "max", 'gravity': "center", 'y': 20, 'x': -20, 'crop': "scale"}, {'overlay': "balloon", 'width': 30}, {'effect': "hue:-20", 'angle': 5}, {'flags': "layer_apply", 'x': 30, 'y': 5} ])
cloudinary.image("coffee_cup.jpg", {transformation: [ {width: 400, height: 250, gravity: "south", crop: "fill"}, {overlay: "nice_couple", width: "1.3", height: "1.3", gravity: "faces", flags: "region_relative", crop: "crop"}, {effect: "saturation:50"}, {effect: "vignette"}, {flags: "layer_apply", width: 100, radius: "max", gravity: "center", y: 20, x: -20, crop: "scale"}, {overlay: "balloon", width: 30}, {effect: "hue:-20", angle: 5}, {flags: "layer_apply", x: 30, y: 5} ]})
cloudinary.url().transformation(new Transformation() .width(400).height(250).gravity("south").crop("fill").chain() .overlay(new Layer().publicId("nice_couple")).width(1.3).height(1.3).gravity("faces").flags("region_relative").crop("crop").chain() .effect("saturation:50").chain() .effect("vignette").chain() .flags("layer_apply").width(100).radius("max").gravity("center").y(20).x(-20).crop("scale").chain() .overlay(new Layer().publicId("balloon")).width(30).chain() .effect("hue:-20").angle(5).chain() .flags("layer_apply").x(30).y(5)).imageTag("coffee_cup.jpg");
cloudinary.imageTag('coffee_cup.jpg', {transformation: [ {width: 400, height: 250, gravity: "south", crop: "fill"}, {overlay: new cloudinary.Layer().publicId("nice_couple"), width: "1.3", height: "1.3", gravity: "faces", flags: "region_relative", crop: "crop"}, {effect: "saturation:50"}, {effect: "vignette"}, {flags: "layer_apply", width: 100, radius: "max", gravity: "center", y: 20, x: -20, crop: "scale"}, {overlay: new cloudinary.Layer().publicId("balloon"), width: 30}, {effect: "hue:-20", angle: 5}, {flags: "layer_apply", x: 30, y: 5} ]}).toHtml();
$.cloudinary.image("coffee_cup.jpg", {transformation: [ {width: 400, height: 250, gravity: "south", crop: "fill"}, {overlay: new cloudinary.Layer().publicId("nice_couple"), width: "1.3", height: "1.3", gravity: "faces", flags: "region_relative", crop: "crop"}, {effect: "saturation:50"}, {effect: "vignette"}, {flags: "layer_apply", width: 100, radius: "max", gravity: "center", y: 20, x: -20, crop: "scale"}, {overlay: new cloudinary.Layer().publicId("balloon"), width: 30}, {effect: "hue:-20", angle: 5}, {flags: "layer_apply", x: 30, y: 5} ]})
<Image publicId="coffee_cup.jpg" > <Transformation width="400" height="250" gravity="south" crop="fill" /> <Transformation overlay="nice_couple" width="1.3" height="1.3" gravity="faces" flags="region_relative" crop="crop" /> <Transformation effect="saturation:50" /> <Transformation effect="vignette" /> <Transformation flags="layer_apply" width="100" radius="max" gravity="center" y="20" x="-20" crop="scale" /> <Transformation overlay="balloon" width="30" /> <Transformation effect="hue:-20" angle="5" /> <Transformation flags="layer_apply" x="30" y="5" /> </Image>
<cld-image publicId="coffee_cup.jpg" > <cld-transformation width="400" height="250" gravity="south" crop="fill" /> <cld-transformation :overlay="nice_couple" width="1.3" height="1.3" gravity="faces" flags="region_relative" crop="crop" /> <cld-transformation effect="saturation:50" /> <cld-transformation effect="vignette" /> <cld-transformation flags="layer_apply" width="100" radius="max" gravity="center" y="20" x="-20" crop="scale" /> <cld-transformation :overlay="balloon" width="30" /> <cld-transformation effect="hue:-20" angle="5" /> <cld-transformation flags="layer_apply" x="30" y="5" /> </cld-image>
<cl-image public-id="coffee_cup.jpg" > <cl-transformation width="400" height="250" gravity="south" crop="fill"> </cl-transformation> <cl-transformation overlay="nice_couple" width="1.3" height="1.3" gravity="faces" flags="region_relative" crop="crop"> </cl-transformation> <cl-transformation effect="saturation:50"> </cl-transformation> <cl-transformation effect="vignette"> </cl-transformation> <cl-transformation flags="layer_apply" width="100" radius="max" gravity="center" y="20" x="-20" crop="scale"> </cl-transformation> <cl-transformation overlay="balloon" width="30"> </cl-transformation> <cl-transformation effect="hue:-20" angle="5"> </cl-transformation> <cl-transformation flags="layer_apply" x="30" y="5"> </cl-transformation> </cl-image>
cloudinary.Api.UrlImgUp.Transform(new Transformation() .Width(400).Height(250).Gravity("south").Crop("fill").Chain() .Overlay(new Layer().PublicId("nice_couple")).Width(1.3).Height(1.3).Gravity("faces").Flags("region_relative").Crop("crop").Chain() .Effect("saturation:50").Chain() .Effect("vignette").Chain() .Flags("layer_apply").Width(100).Radius("max").Gravity("center").Y(20).X(-20).Crop("scale").Chain() .Overlay(new Layer().PublicId("balloon")).Width(30).Chain() .Effect("hue:-20").Angle(5).Chain() .Flags("layer_apply").X(30).Y(5)).BuildImageTag("coffee_cup.jpg")
MediaManager.get().url().transformation(new Transformation() .width(400).height(250).gravity("south").crop("fill").chain() .overlay(new Layer().publicId("nice_couple")).width(1.3).height(1.3).gravity("faces").flags("region_relative").crop("crop").chain() .effect("saturation:50").chain() .effect("vignette").chain() .flags("layer_apply").width(100).radius("max").gravity("center").y(20).x(-20).crop("scale").chain() .overlay(new Layer().publicId("balloon")).width(30).chain() .effect("hue:-20").angle(5).chain() .flags("layer_apply").x(30).y(5)).generate("coffee_cup.jpg");
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation() .setWidth(400).setHeight(250).setGravity("south").setCrop("fill").chain() .setOverlay("nice_couple").setWidth(1.3).setHeight(1.3).setGravity("faces").setFlags("region_relative").setCrop("crop").chain() .setEffect("saturation:50").chain() .setEffect("vignette").chain() .setFlags("layer_apply").setWidth(100).setRadius("max").setGravity("center").setY(20).setX(-20).setCrop("scale").chain() .setOverlay("balloon").setWidth(30).chain() .setEffect("hue:-20").setAngle(5).chain() .setFlags("layer_apply").setX(30).setY(5)).generate("coffee_cup.jpg")!, cloudinary: cloudinary)
Cloudinary: Much More than Image 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.
Check Out the Details of CSS Images
Want to learn more about CSS images? These articles are an excellent resource:
- Working With CSS Images
- CSS Image Overlay: Overlaying Text and Images for Visual Effect
- Image Resizing: Manually With CSS and Automatically With Cloudinary
- CSS Image Effects: Five Simple Examples and a Quick Animation Guide
- Creating Image-Filter Effects With CSS and Riveting Transformations
- Rotating Images in JavaScript: Three Quick Tutorials
- Cool Tricks for Resizing Images in JavaScript