Tips for showing text on top of a responsive image using CSS

I am working on a layout with 4 columns, each containing images of different sizes followed by a hover effect with an absolutely positioned mask.

The width of the mask is currently exceeding the width of the images.

Is there a way to ensure that the mask always matches the width of the image?

Link to Example

HTML

<div class="wrap"><div class="row">
  <div class="col-1-4">
     <div class="show show-first">
        <img src="http://placehold.it/100X200" />
        <div class="mask">
           Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
        </div>
     </div>
  </div>
  <div class="col-1-4">
     <div class="show show-first">
        <img src="http://placehold.it/120X200" />
        <div class="mask">
           Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
        </div>
     </div>
  </div>
  <div class="col-1-4">
     <div class="show show-first">
        <img src="http://placehold.it/150X200" />
        <div class="mask">
           Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
        </div>
     </div>
  </div>
  <div class="col-1-4">
     <div class="show show-first">
        <img src="http://placehold.it/180X200" />
        <div class="mask">
           Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
        </div>
     </div>
  </div>

CSS

* {  box-sizing: border-box; }
.wrap{
  width: 87%;
  margin: auto;
  padding: 0 10px;
  background-color: rgba(255,255,255,0.9);
  overflow: hidden;
  box-shadow: 0 0 5px #eee;
}
img{
  max-width: 100%;
  height: auto; 
}
[class*='col-']{
  float:left;   
}
.col-1-3{
  width: 33.33%;
  padding: 20px;
} 
.col-2-3{
  width: 66.66%; 
  padding: 20px;
}
.col-1-4{
  width: 25%; 
  padding: 10px;
}
.show{
  width:100%;
  height: 100%;
  border: 1px solid rgba(0,0,0,0.04);
  overflow: hidden;
  position: relative;
  text-align: center;
  cursor: default;
  background: #fff;
  display: block;
  border-radius: 4px;
}
.show .mask{
  width: 100%;
  height: 100%;
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0
}
.show-first .mask {
  opacity: 0;
  background-color: rgba(0,0,0, 0.4);
  transition: all 0.4s ease-in-out;
}
.show-first:hover .mask {
  opacity: 1;
}
@media only screen and (max-width: 767px){
  .col-1-4{
    width: 50%; 
    padding: 10px;
    overflow: hidden;
    clear: right;
  }
  .wrap{
    width: 100%;
    margin: auto;
    overflow: hidden;
  }
  .mobile-clear{
    clear:both;
  }
}

Answer №1

Wrap the content with a div:

<div class="show show-first">
  <div class="mask-wrapper">
    <img src="http://placehold.it/100X200" />

    <div class="mask">
   Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
    </div>
  </div>
</div>

Next, adjust the CSS rules as follows:

.mask-wrapper: {
  position: relative;
  display: inline-block;
}

.show {
  text-align: center;
}

.mask {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 2;
}

img {
    max-width: 100%; // Consider converting this into a separate class for easier styling
}

Answer №2

Resolved Answer

To improve the layout, you can make the following changes:

width: 100%;

can be removed and replaced with

display: block;

which should be updated to

display: inline-block

for <div class="show">. Additionally, for center alignment of the <div class="show">s, include

text-align: center;

within <div class="col-1-4">.

Implementation Example:

* {
  box-sizing: border-box;
}
.wrap{
  width: 87%;
  margin: auto;
  padding: 0 10px;
  background-color: rgba(255,255,255,0.9);
  overflow: hidden;
  box-shadow: 0 0 5px #eee;
}
img{
  max-width: 100%;
  height: auto;
}
[class*='col-']{
  float:left;   
}
.col-1-3{
  width: 33.33%;
  padding: 20px;
}
.col-2-3{
  width: 66.66%; 
  padding: 20px;
}
.col-1-4{
  width: 25%; 
  padding: 10px;
  text-align: center;
}
.show{
  height: 100%;
  border: 1px solid rgba(0,0,0,0.04);
  overflow: hidden;
  position: relative;
  text-align: center;
  cursor: default;
  background: #fff;
  display: inline-block;
  border-radius: 4px;
}
.show .mask{
  width: 100%;
  height: 100%;
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0
}
.show-first .mask {
  opacity: 0;
  background-color: rgba(0,0,0, 0.4);
  transition: all 0.4s ease-in-out;
}
.show-first:hover .mask {
  opacity: 1;
}
@media only screen and (max-width: 767px) { 
  .col-1-4{
    width: 50%; 
    padding: 10px;
    overflow: hidden;
    clear: right;
  }
  .wrap{
    width: 100%;
    margin: auto;
    overflow: hidden;
  }
  .mobile-clear{
    clear:both;
  }
}
<div class="wrap">
<div class="row">
  <div class="col-1-4">
    <div class="show show-first">
      <img src="http://placehold.it/100X200" />
      <div class="mask">
       Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
      </div>
    </div>
  </div>
   <div class="col-1-4">
    <div class="show show-first">
      <img src="http://placehold.it/120X200" />
      <div class="mask">
       Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
      </div>
    </div>
  </div><div class="col-1-4">
    <div class="show show-first">
      <img src="http://placehold.it/150X200" />
      <div class="mask">
       Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
      </div>
    </div>
  </div><div class="col-1-4">
    <div class="show show-first">
      <img src="http://placehold.it/180X200" />

      <div class="mask">
       Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi sequi laboriosam delectus ipsa, vel nulla ipsum quod esse molestias nam quos eius. Reprehenderit repellat atque voluptas autem velit, distinctio esse.
      </div>
    </div>
  </div>
</div> 
</div>

Answer №3

Give this code a try:

 $('.display').hover(function(){
   var imageWidth = $(this).find('img').width();
    //alert(getimg);
     $('.overlay').css({'width':imageWidth})

  });

For a demonstration, check out this Example

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

What is the method of adding a child to the outerHTML of the parent element instead of within it?

Is there a way to use the outerHTML method on a parent element to append a child element so that the icons appear outside of the targeted element rather than inside it? The current code snippet shows the icons inside the box, but I want them to be placed o ...

Tips for stopping the background color from affecting the text color

I'm dealing with a situation where I have an h6 nested inside a div. <div class="cylinder" data-v-5147e140=""> <div data-v-5147e140=""> <h6 class="quantite" data-v-5147e140="&qu ...

Is there a way to display x squared as a superscript inside a button element?

In my reactjs project, I am developing a basic calculator. One of the buttons is supposed to calculate x squared, and I tried using the <sup> HTML element for this purpose but it did not work as expected. <Button name = "x<sup>2</sup> ...

What steps should be taken to retrieve the contents of a file that has been chosen using the browse

You have successfully implemented a browse button that allows the user to navigate the directory and choose a file. The path and file name are then displayed in the text element of the Browse button complex. Now, the question arises - how can I extract dat ...

Striking a line through the hyperlink tag

I am attempting to create a crossed-out line above the content of an a tag for decorative purposes. The line should stretch across the entire width without intersecting with the actual text within the tag. This is my desired outcome: This is my current p ...

The iPython terminal is not displaying properly due to a constrained screen width

When my iPython displays a long list, it appears as one tall column instead of utilizing the full width of the terminal screen. I have attempted to adjust the CSS in '.ipython/profile_default/static/custom/custom.css' by setting '.container ...

Using javascript, what is the best way to clear a text field with a specific condition?

When I clear the text field #t1, I expect text field #d1 to also clear. However, the last two lines of code do not achieve this result. function utl() { var tField = document.getElementById('t1'); var dField = document.getElementById(&a ...

Center an absolutely positioned div using CSS

What is the best way to position an absolute div at the center? <div class="photoFrame">minimum width of 600px, positioned absolutely</div> jQuery var screenWidth = $(window).width(); $('.photoFrame').css({'margin-left': ...

Refresh a div element automatically with information from a different webpage using jQuery

I've been attempting to automatically reload a page and fetch new data every 10 seconds, or even less. However, the codes I've tried so far have not been successful. Here is my current approach... // script // <script> $(document).rea ...

Learn how to utilize Javascript to easily drag and drop an image within the same block

I am encountering an issue with dragging and dropping images or swapping them using JavaScript. I have tried to implement this in the code below, where clicking on icons moves them onto a colored div. However, I am now trying to drag and drop the images wi ...

What is the best way to define the relative path and folder paths in HTML and CSS when working in Visual Studio 2012?

Working on a basic HTML project that includes css and javascript. Within the project folders named css, scripts, and images are used for organization. The project structure can be seen in the provided image. The issue that arises is how to correctly set p ...

Tips for centrally zooming responsive images without altering their dimensions

I have created a custom jQuery and CSS function that allows an image to zoom in and out on mouseover while maintaining a constant box size. I modified an example code to suit my needs. Check out the demo here: https://jsfiddle.net/2fken8Lg/1/ Here is the ...

Center a single div vertically within a parent div by utilizing a fluid layout

Attempting to align text elements within a responsive layout presents challenges due to the fluid nature of <div> sizes. I recently discovered a fantastic method for horizontally and vertically centering a child <div> inside its parent <div ...

Is it possible to create a fluid-width layout with two columns that also spans the full

I have searched tirelessly through Google and stackoverflow, but I can't seem to find a solution to my specific problem. I need help creating a container div that can hold either one or two columns. The content is generated by a CMS and may or may not ...

What are the steps to prevent a redirect?

I have two forms with different functionalities - one for submitting/adding and the other for selecting/redirecting. Currently, both forms are redirecting. How can I prevent the top form from redirecting after submission? <?php if($_POST){ / ...

Creating an animated background slide presentation

After creating a button group, I wanted the background of the previous or next button to move/slide to the selected one when the user clicks on it. I achieved this effect using pure CSS and simply adding or removing the 'active' class with jQuery ...

I need help picking out specific elements from this messy HTML code using XPath and lxml - any ideas on how to do

I am looking to extract specific strings from this HTML document using only lxml and clever XPath. The content of the strings may vary, but the structure of the surrounding HTML will remain constant. The strings I need are: 19/11/2010 AAAAAA/01 Normal U ...

Struggling to align my overlay accurately by combining absolute and relative positioning

I'm currently tackling a frontend mentor project, and I've hit a roadblock trying to get my image overlay to position itself over the image rather than below it. I've set the parent position to relative and the overlay to absolute, but so fa ...

Why are the items I have inserted inside my <div> tag not appearing on the screen?

What could be simpler than this: {this.state.dash ? <div className='dashWrapper slide'> HELLO </div> : null} I've tried everything, but nothing seems to show up inside the div. ...

Various text sizes within a nested HTML list structure

I've developed a nested CSS class for an ordered list on my website, but I'm encountering a problem where each list item is appearing in different font sizes even though I have specified the font size. .number_list ol { font:normal 1.2em ...