Incorporate RGBA backgrounds into various images simultaneously

I'm having trouble adding an rgba background to multiple images using Bootstrap while ensuring responsiveness. Unfortunately, something seems to have gone wrong.

Here's an example of what I'm trying to achieve:

https://i.sstatic.net/ueBEZ.png

Below is the HTML code snippet I'm working with:

.text-caption {
  color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.caption:after {
  content: ' ';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, .5);
}
<div class="container">
  <div class="row">

    <div class="text-center">
      <h3>This is some text</h3>
      <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
    </div>

    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/400x300/ffb3d1/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>

    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/400x300/c2f0c2/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>

    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/400x300/cc99ff/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>

  </div>
</div>

Any help would be greatly appreciated!

Answer №1

To ensure your code functions correctly, remember to assign z-index values to the absolutely positioned elements and apply relative positioning to the caption:

.text-caption {
  color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index:2;
}

.caption {
  position:relative;
}

.caption:after {
  content: ' ';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, .5);
  z-index:1;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
  <div class="row">

    <div class="col-md-12 text-center">
      <h3>This is some text</h3>
      <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
    </div>

    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/400x300/ffb3d1/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>

    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/400x300/c2f0c2/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>

    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/400x300/cc99ff/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>

  </div>
</div>

Check out this Example on Bootply

Answer №2

Several key points to take note of:

  • The selected images are significantly large and to give the appearance of fitting within their containers, the overflow: hidden property has been included for the caption.
  • I have set a maximum height of 500px for the caption, allowing for automatic scaling of the image.

.text-caption {
  color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.caption {
  max-height: 500px;
  overflow: hidden;
}

.caption:after {
  content: ' ';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, .5);
}

.caption>img {
  width: auto;
  height: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="text-center">
      <h3>This is some text</h3>
      <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
    </div>
    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/4000x3000/ffb3d1/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>
    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/4000x3000/c2f0c2/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>
    <div class="col-md-4">
      <div class="caption">
        <img class="img-responsive" src="https://dummyimage.com/4000x3000/cc99ff/000">
        <div class="text-caption">
          <h3>This is some text</h3>
          <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
          <button class="btn btn-primary center-block">Button</button>
        </div>
      </div>
    </div>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Repair the image within the HTML image tag

Currently, I am working on an HTML page using Knockout.js and Bootstrap. I am facing an issue where the image I am adding in the image tag does not completely cover the image tag. Instead, there is a white background appearing which is not desired. I wan ...

Creating a centered transparent rectangle of a specific width using HTML

I am working on a page layout similar to this FIDDLE body { margin:0 auto; max-width: 800px; padding:0 0 0 0; text-align:left; background-color:#FFFFFF; background-image: url('http://freeseamlesstextures.com/images/40-dirty-pa ...

Troubleshooting a problem with the Bootstrap dropdown menu

Can anyone assist with an issue regarding Bootstrap 4.4.1? I am facing a problem where two consecutive dropdown links+menus are not displaying correctly. The second dropdown menu is showing the content of the previous link instead of its own, even though t ...

Is it possible to dynamically adjust the width of table columns using CSS/LESS on a per-column basis?

In this scenario, the table's column width is determined by the number of columns present. For example, if there are 4 columns in the table, each column's width would be set to a maximum of 25% of the table's overall width. With 3 columns, t ...

Tips for creating a responsive div that contains both an image and description, including automatically resizing the image to fit the

#content { background-color: #ACAE4C; float: right; display: inline-block; padding: 0; } <div id="content"> <div class="pic"></div> <div class="desc"></div> </div> I need assistan ...

Switch back and forth between different information

My Bootstrap version 4.1.1 page needs a tweak. I want to make the background color of the active toggle white. .nav-tabs { margin-top: 3%; border: none; background: #0062cc; border-radius: 1.5rem; width: 28%; float: right; } .nav-tabs .nav- ...

Is there a way to achieve a transparent background while animating the second text?

I am seeking to create a unique typography animation that involves animating the second text, which is colored and consists of multiple text elements to animate. The animation should showcase each text element appearing and disappearing one after the other ...

Personalize Drop-Down Selection

Currently implementing bootstrap on a site and seeking to enhance the dropdown menu customization. The default navbar appearance is present, with dropdown elements appearing upon hover. My goal is to include an icon next to each item in the navbar and ha ...

Having trouble implementing absolute css positioning on my custom composite button

I encountered a peculiar issue that I have managed to work around, but I am still curious about why it is happening. Below is the code for my Composite class: import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.clie ...

displaying date picker on button click with angular bootstrap ui

I am trying to display an AngularJS Bootstrap calendar only upon clicking a button, and I want to restrict it from appearing when clicking on the input field. Below is my code: <input type="text" onkeydown="return false;" ...

Other options to consider besides using flex wrapping

Is it possible to use other methods to wrap these image items with the display: flex; property besides using the flex-wrap property? Are there alternative approaches available? body { margin: 0px; } img {} h1 {} p {} a { background-color: #ddd; ...

Creating a design that resembles boxes for the div elements while ensuring the grid layout remains undisturbed

Hey there! I'm new to using Bootstrap and I'm trying to create boxes that look like the ones on the right side of this page: Unfortunately, I haven't had much luck so far. I tried using padding, but it messed up my grid system. I assigned a ...

Creating a custom fav icon within a button with CSS styling

https://example.com/code-example Hey there, I'm attempting to replicate the button showcased in the code example above. However, I'm facing a challenge when trying to incorporate the stars without altering the existing script. Another issue is w ...

Chrome fails to load webfont upon page initialization

Encountering a problem with loading a WebFont specifically on Chrome version "48.0.2564.82 m (64-bit)". The issue arises from the webfont not being applied upon page load or navigation, unless I manually refresh the page. Below is the snippet of code fro ...

Modify the color of the panel header when the button is clicked

I am trying to modify the background color of my panel header using a button that changes the color scheme of my site. The button currently functions on AFO. Below is the stylesheet for the button: .AFO { background-color: #fff;} .AFO h1 { color: #00159d; ...

Is there a way to dynamically alter the content depending on the index of the current slide using swiper.js?

Hi, I am new to utilizing the Swiper framework and so far, it has been one of the best sliders I have ever experienced. Currently, I am trying to establish a connection between 2 div tags - one tag holds the content of each slide while the other tag contro ...

CSS Flexibility

Greetings everyone, it's been a while since I dabbled in web development. I recently worked on my site with the intention of making it responsive using flexbox. This is my first time posting here, so please guide me on how to seek help more effective ...

I am facing difficulties displaying Arabic language characters on my HTML code

I created a website and included the following meta tag in the head section: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> However, all Arabic text appears distorted as shown in the image. Interestingly, when I manually cha ...

Display words on screen and then alter hue using HTML and CSS

Is there a way to dynamically change the color of text inside <a> tags from black to red after a specific time interval and keep it permanently red? do { document.getElementById("code").innerHTML +="<a>Hello World</a><br>"; awa ...

The issue occurs when using z-index: -1 in Google Chrome causes links to malfunction

Recently, I encountered an issue with Chrome. When I applied a z-index of -1 to a position: relative; unordered list, the links within it became unclickable. You can see an example at http://jsfiddle.net/raLnx/ using Chrome 20.0.1132.47m. There is no pro ...