Creating a responsive CSS image overlay using bootstrap

I am struggling with making the height of an image overlay responsive while maintaining the aspect ratio. I have tried defining it in pixels, but it doesn't work as intended. Here is the snippet of my HTML code:

<ul class="img-list">
  <li>
    <a href="http://nataliemac.com"><br>
       <img class="imgrd" src="http://127.0.0.1:8080/wordpress/wp-content/uploads/2016/03/PIC_6436-300x199.jpg" width="900" height="597" class="alignnone size-medium wp-image-189"><br>
       <span class="text-content"><span>Place Name</span></span><br>
    </a>
  </li>
<p>
  ...
</p>

Here is a snippet of my CSS:

ul.img-list {
  list-style-type: none;
  margin: 0;
  padding: 0;
  text-align: center;
}

ul.img-list li {
  display: inline-block;
  margin: 0 1em 1em 0;
  position: relative;
  width: 100%;
  height: 100%;

}

span.text-content span {
  display: inline-block;
  text-align: center;
  vertical-align: middle;
}

ul.img-list li:hover span.text-content {
  opacity: 1;
}
span.text-content {
  display: inline-block;
  background: rgba(0,0,0,0.5);
  color: white;
  cursor: pointer;
  display: table;
  left: 0;
  position: absolute;
  top: 25px;
  width: 100%; 
  height: 100%;
  border: 5px solid #fff;
  border-radius: 10px 125px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}

img.imgrd {
  border-radius: 10px 125px;
}

Any help on making the height responsive would be greatly appreciated!

Answer №1

Here is a suggestion:

ul.image-list li {
  display: inline-block;
  margin: 0 1em 1em 0;
  position: relative;
  width: 100%;
  height: auto;
}

Answer №2

Another approach is to dive in and extract the image's height using jquery, for example:

let imageHeight = $('.imgrd').height();
$('.text-content').height(imageHeight);

However, it may be preferable to utilize CSS methods if they better suit your needs :)

Answer №3

To ensure the text is properly positioned in the center of the overlay, you can adjust the styling of the li span to act like a table cell:

span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

Next, make the IMG responsive by setting its width to 100% and ensuring it responds appropriately when the container size changes:

img.imgrd {
  border-radius: 10px 125px;
  width: 100%;
  max-width: 100%;
  height: auto;
}

Remove any <br> tags that disrupt the layout. Also, adjust the span.text-content positioning properties to start at the top of the image and set box-sizing for the border:

span.text-content {
  display: inline-block;
  background: rgba(0,0,0,0.5);
  color: white;
  cursor: pointer;
  display: table;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%; 
  height: 100%;
  border: 5px solid #fff;
  border-radius: 10px 125px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
  box-sizing: border-box;
}

For more details, refer to this JSFiddle link:

https://jsfiddle.net/xeqhepn4/2/

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

Ways to activate the built-in HTML5 form validation without actually submitting the form

I have a form in my application that I plan to submit using AJAX. However, I would like to implement HTML5 for client-side validation. Is it possible to trigger form validation without actually submitting the form? Here is an example of the HTML code: &l ...

Strip away the HTML tags and remove any text formatting

How can I effectively remove HTML tags and replace newlines with spaces within text? The current pattern I am using is not ideal as it adds extra space between words. Any suggestions on how to improve this pattern? replace(/(&nbsp;|<([^>]+)> ...

The issue with mCustomScrollbar's "scrollTo" function not functioning properly has been detected within a single-page application

Although there are many similar questions out there, I have been unable to find a solution to my particular issue. In my single page application with metro style design, I am facing an issue where the scroll position does not reset back to its original pl ...

Tips for creating zoomed-in background images on small screens

I am currently implementing this code for a website project .homepage{ background-image: url(/wp-content/themes/ep/img/_.png); -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; background ...

Editing the object retrieved from JSON is not possible once it has been fetched

Project. Input text in the field and it appears on the shirt. When you click "see back," there is an issue where new text overlaps old text. Clicking on "see front" allows you to enter new text, with the previous text saved underneath. Question: How can ...

Adjustable textarea with automatic height based on content and line breaks

Imagine you have a textarea with text that includes line breaks. If you style it like this: textarea { height: auto; overflow: hidden; resize: none; } Upon loading the page, the textarea will only adjust its height based on the content until it enc ...

What sets Fetch apart from ajax and XMLHttpRequest that makes it impressively faster?

Over the past few days, I have been working on optimizing a client table for a project. The table contains over 10k clients, and as a result, it was taking a long time to load. The front-end team had implemented pagination, filters, and reordering, which ...

Position multiple div elements at the top with CSS alignment

Hey there! I have a question about the HTML and CSS code snippet below. I'm trying to align the text in the <h2> tags at the top for all three <div class="article-col-3"> containers, but it's currently aligned at the bottom. ...

Share and sign with the href link to the PHP webpage for assistance

Here is a problem I am facing: browse_cat.php?cat_gr='Mopeds &amp; Traktors'"> In the browse_cat.php file, I have used this code to retrieve the above "category": $cat=$_GET['cat_gr']; echo $cat; The output of thi ...

Retrieve the content of a text field with jQuery

Check out this block of HTML: <div class="sub-middle-column"> <div class="div-header">Grandsire <a "#", class="table-control-focus sub-header-table-focus" id="table-control-focus-t" >abc</a> <ul class="table-controls h ...

The element "center" is not a recognized HTML tag in Angular 4

The center tag is not functioning and it says that center is an unknown element. <center> <a><img class="placeholder_img" src="img/placeholder.png"></a> </center> When I used the above HTML tags, it stated that "center i ...

Convert a fixed Jquery div to absolute positioning when it reaches the end of the page

I've implemented a Jquery code that adds a fixed class to a div when scrolling down. However, I now want to remove this fixed class when it reaches the bottom of the page and replace it with a new one that includes position:absolute; and margin-bottom ...

Loading dynamic fonts in React MaterialUI

In our web application, each user experiences a unique style with colors, fonts, border widths, and more based on the Material UI JSON theme retrieved from the database upon loading the app. The challenge lies in handling dynamic fonts. What approaches ha ...

What is the best way to focus the video on its center while simultaneously cropping the edges to keep it in its original position and size?

I'm trying to create a special design element: a muted video that zooms in when the mouse hovers over it, but remains the same size as it is clipped at the edges. It would be even more impressive if the video could zoom in towards the point where the ...

Showing a cropped section of an image inside a div container

Is it possible to display a specific portion of an image within a div that is 300*300px in size while maintaining responsiveness, using only HTML and CSS? The div always occupies 33% of the page width. Here is an example of my code: HTML <div class= ...

Cross-Domain Image Uploading

I've been attempting to enable image uploads from one domain to another (CORS). Everything runs smoothly on Localhost, but when I try it on an actual domain, I consistently encounter this message in the Developer Console: Invalid request In my uplo ...

Strategies for avoiding overflow-x issues in Safari and mobile devices

I am currently facing an issue where I have a div containing a table with overflow enabled, allowing the whole table to be viewed by scrolling. However, on Safari and mobile Safari browsers, there is a horizontal scroll on the html and body elements. Whil ...

CSS rule for targeting an element that does not have any child elements

I have a variety of different elements that are structured similarly to the following.. <div id="hi"> <div class="head"> </div> <div class="footer"> </div> </div> ..however, some of these elements do no ...

Cross-browser compatibility issues preventing CSS button effects from functioning properly

Experiencing some challenges with CSS Button effects not functioning consistently across different browsers. The display is satisfactory in Chrome, but not in Firefox. Struggling to pinpoint the source of the issue. Here is my current setup: Fiddle a ...

The height of the content in the polymer core-scaffold cannot be set to 100%

I am working with a polymer element that I have defined. The main objective is to make the conversation_container element have a height of 100% so that the message_box_container can be positioned at the bottom of the entire panel using position: absolute; ...