CSS Image Captions with Gradients and Transparent Backgrounds

C
Published in: CSS

CSS Image Captions

You can add a semi-transparent white on black caption to your images using simple CSS. The opacity of the caption background can be changed by modifying the alpha parameter in background-color’s rgba attribute.

<style>
.image {
  width:600px;
}
.image .black {
  position: relative;
  left: 0;
  bottom: 60px;
  width: 100%;
  padding: 10px 20px;
  height: 40px;
  background-color: rgba(0, 0, 0, 0.6);
  filter: progid:DXImageTransform.Microsoft.gradient
          (startcolorstr=#7F000000,endcolorstr=#7F000000) 9;
 }

.image .black a {
  color: white;
  font-size: 18px;
  text-decoration: none;
  outline: none;
  font-family:georgia;
}

.image .black .follow {
  margin-top: 5px;
  position: absolute;
  right: 10px;
  top:30px;
  font-size: 12px;
  font-family:verdana;
}

</style>

<div class="image">
  <img src="img.png">
  <div class="black">
    <a href="#">
      Image Caption
    </a>
    <a href="#" class="follow">
      Image Credit &rarr;
    </a>
  </div>
</div>

Instead of transparent backgrounds, you can even have fading gradients that transition from black to white much like the lower thirds in videos. Add this snippet to the CSS.

background: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(1, rgba(0,0,0,0.0)), /* Top */
    color-stop(0, rgba(0,0,0,1.0)) /* Bottom */
);

/* Gecko */
background: -moz-linear-gradient(
    center bottom,
    rgba(0,0,0,1.0) 0%, /* Bottom */
    rgba(0,0,0,0.0) 100% /* Top */
);
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: CSS

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube