What is the best way to reduce the size of a Bootstrap card image back to its original dimensions when

I am currently experimenting with a bootstrap card using HTML, CSS, and Bootstrap.

My main focus right now is on how to shrink the image when hovering over it. The .card-header-img has a fixed height of 150px that I do not want to modify. What I want to achieve is to scale down the image on hover while ensuring that the entire image remains visible (both in width and height)!

I would prefer a solution that does not involve JavaScript. I have already attempted the following approach, but it doesn't seem to work:

transform: scale(1, calc(150px / var(--initial-height)));

.acf-blocksy-child-container {
  width: 70%;
}

.card-header-img {
  position: relative;
  height: 150px;
  /* Fixed height */
  overflow: hidden;
}

.card-header-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  /* Ensure the image covers the entire container */
  transition: transform 0.3s ease-in-out;
}

.card-header-img::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  /* Dark overlay */
  opacity: 1;
  transition: opacity 0.3s ease-in-out;
}

.card-header-img:hover img {
  /* Scale down the image on hover */
  transform: scale(0.7);
}

.card-header-img:hover::before {
  opacity: 0;
  /* Dark overlay disappears on hover */
}

.card-header-img:hover .card-title,
.card-header-img:hover .card-text {
  display: none;
  /* Hide title and text on hover */
}
<link href='https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css' rel='stylesheet'>
<div class='container-fluid acf-blocksy-child-container'>
  <div class='row'>
    <div class='col-md-12 mt-5'>
      <div class='card'>
        <!-- CARD HEADER -->
        <div class='card-header card-header-img' style='--initial-height: 250px'>
          <img src='https://cdn4.vectorstock.com/i/1000x1000/07/08/long-neck-giraffe-height-measure-vector-12290708.jpg' class='card-img img-fluid' alt='Card image'>
          <div class='card-img-overlay'>
            <h5 class='card-title text-white'>Card title</h5>
            <p class='card-text text-white'>This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
            <p class='card-text text-white'>Last updated 3 mins ago</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Answer №1

If you want an image to fit automatically within a container without declaring a CSS variable, simply set the width to 100% and the height to 150px or any desired value. Then, when hovering over the image, adjust the width to auto for automatic fitting.

To centrally align the image, use:
margin-left: auto;
margin-right: auto;
display: block;

Below is the complete code snippet:

.card-header-img {
  position: relative;
  overflow: hidden;
}

.card-header-img img {
  width: 100%;
  height: 150px;
  object-fit: cover;
  /* Ensure the image covers the entire container */
  transition: transform 0.5s ease-in-out;
}

.card-header-img::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  /* Dark overlay */
  opacity: 1;
  transition: opacity 0.5s ease-in-out;
}

.card-header-img:hover img {
  width: auto;
  margin-left: auto;
  margin-right: auto;
  display: block;
}

.card-header-img:hover::before {
  opacity: 1;
  /* Dark overlay disappears on hover */
}

.card-header-img:hover .card-title,
.card-header-img:hover .card-text {
  display: none;
}
<link href='https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css' rel='stylesheet'>


<div class='card-header card-header-img'>
  <img src='https://cdn4.vectorstock.com/i/1000x1000/07/08/long-neck-giraffe-height-measure-vector-12290708.jpg' class='card-img img-fluid' alt='Card image'>
  <div class='card-img-overlay'>
    <h5 class='card-title text-white'>Card title</h5>
    <p class='card-text text-white'>This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
    <p class='card-text text-white'>Last updated 3 mins ago</p>
  </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

Position the ion-radio element in the middle of the layout

As I work on creating a privacy page in HTML using Ionic 3, my desired output should resemble the following: https://i.sstatic.net/lxzc9.png However, with my current code, the result I'm seeing is different from what I expected: https://i.sstatic.net ...

What is the best way to deactivate buttons with AngularJS?

I have a situation where I need to disable the save button in one function and permanently disable both the save as draft and save buttons in another function using AngularJS. How can I accomplish this task with the disable functionality in AngularJS? Her ...

Combining arrays that run parallel in Jquery

Seeking a solution similar to looping through multiple arrays in parallel using jQuery, but this time to construct an HTML page. I have three sets of parallel objects with arrays, potentially forming a 2D array. {"images":["Bellevue\/images\/1. ...

Blog Writer: Picture quality compromised when adjusting size

Issue with blurry images after resizing. The problem is noticeable in the three main images located under the Header section. To preview the blog, click on this link: Click Here Your assistance in solving this issue would be greatly appreciated. Thank yo ...

Wave Filter in SVG

While attempting to create a fisheye-esque filter in my SVG, I came across this interesting codepen example: http://codepen.io/johanberonius/pen/RopjYW The effect works well, but I would like it to be a bit more pronounced. Unfortunately, I am unable to m ...

Implementing a feature to insert a horizontal rule button in React Draft Wysiwyg

I am currently working on implementing a custom button in React Draft Wysiwyg that will allow me to add an <hr> tag to my content. Although I have followed the demos and documentation, I have encountered an issue where the custom button successfully ...

The CSS for SVG circles breaks in Firefox, causing the browser to strip away the radius property

Currently, I am working on creating a progress ring using SVG and CSS. It is functioning well in Chrome; however, Firefox (61.0.1 (64-bit)) is giving me trouble as it does not display the circle. I attempted to apply the method from this question but did n ...

Animating a Bootstrap 4 card to the center of the screen

I am attempting to achieve the following effect: Display a grid of bootstrap 4 cards Upon clicking a button within a card, animate it by rotating 180 degrees, adjusting its height/width from 400px - 350px to the entire screen, and positioning it at the c ...

What is the best way to stylize a date using Bootstrap-datepicker?

While this topic is well-known, I have a slightly more complex question regarding it. I have gone through the documentation here. My goal is to display the date in French format (dd/mm/yyyy) and save the value in US format (yyyy-mm-dd). Additionally, I nee ...

Guide on transforming Div content to json format with the use of jquery

I am trying to figure out how to call the div id "con" when the export button is clicked in my HTML code. I want it to display JSON data in the console. If anyone has any suggestions or solutions, please help! <html> <div id ="con"> < ...

Avoiding special characters in URLs

Is there a way to properly escape the & (ampersand) in a URL using jQuery? I have attempted the following methods: .replace("/&/g", "&amp;") .replace("/&/g", "%26") .replace("/&/g", "\&") Unfortunately, none of these are y ...

The padding of dynamically generated bootstrap buttons diminishes

I am facing an issue with a bootstrap button bar that is using the well class. The problem occurs when I create it twice in my code: once using HTML and another using dynamic javascript. The bootstrap functions properly when created with HTML. However, t ...

Compile a list of URLs found within a particular HTML A tag and save them to a text file

In an attempt to extract specific URLs from a webpage, I aim to target only those within a particular HTML A tag. For instance, the targeted URLs are in HTML A tags that contain "Info science": a bunch of HTML before <a rel="external nofollow&quo ...

HTML Date Form: Setting the Start Date to Tomorrow with JavaScript

Is there a way to restrict the start date to tomorrow's local date? I'm having trouble with the code below: <form method="POST"> <div> <label for="s2">pickup_date</label> ...

Styling a rectangle in p5.js: Tips and tricks

In my p5.js code, I created a rectangle using rect(): rect(10, 10, 10, 10); Now, I want to add some style to it with a box-shadow effect: box-shadow: 20px 20px 50px #00d2c6, -30px -30px 60px #00ffff; However, when I tried to apply the style using the doc ...

Looking to extract text between specific match groups using regex from an HTML YouTube page? Here's how you can do

Utilizing the power of Screaming Frog to extract keyword data from YouTube videos has its limitations. The software only displays 160 characters of meta information, which means videos with extensive keywords may not fully show up in this tab. Experimenti ...

wrap <td> data in a link with vue depending on certain conditions

I'm trying to customize the display of a particular table cell td. I want to show the data in a link if a certain condition is met, and if not, just show the data as text. However, I'm encountering some difficulties in implementing this. I have ...

Discover the position of a dynamically added element

Is there a way to identify the specific dynamically added checkbox that was clicked, whether by index or name? I came across a similar question with a solution in this JSFiddle: JSFiddle Instead of just displaying "clicked", I would like it to show someth ...

Issue with Dynamic Image Path in Require Function: Unable to locate the relative module

I've been struggling with an error in VueJs require function for the past two days. I'm attempting to pass a prop to the Home component and then display the image. Home.vue <template> <BlogPost :post="welcomeScreen"/> <B ...

When using Websocket, an error message stating "Invalid frame header" will be triggered if a close message of 130 or more characters is sent

I am utilizing the ws node.js module along with html5's WebSocket. The Websocket connection is established when a user triggers an import action, and it terminates once the import is completed successfully or encounters an error. At times, the error ...