Adjust the size of an image without a background to completely fill a container div

I'm attempting to create a rounded effect on an image within a container div by having the image stretch and fill the entire space. However, if the container div's height is larger than the image, it displays the background color instead of stretching the image. Is there a way to make the image stretch to fill the div completely without cutting off and showing the background color?

<style>
                .hero-interior-banner-image {
                    height: auto;
                    position: relative;
                }
                .interior-hero-overlay {
                    position: absolute;
                    top: 0px;
                    left: 0px;
                    z-index: 99;
                    width: 60px;
                    bottom: 0px;
                    height: 100%;
                    object-fit: cover;
                }
                .hero-tertiary img {
                    display: block;
                    /*object-fit: 100% 100% !important;*/
                    object-fit: cover !important;
                }
            </style>
<div class="col-12 col-xl-6 px-0">

                <div class="d-none d-xl-block hero-interior-banner-image float-end">

                    <img class="interior-hero-overlay" src="<?php echo get_template_directory_uri(); ?>/images/hero/hero-overlay-<?php the_field( 'color' ); ?>.svg" alt="Hero overlay" />

                    <div class="hero-tertiary">
                        <?php $image = get_field( 'image' ); ?>
                        <?php if ( $image ) : ?>
                            <img class="img-fluid hero-tertiary" src="<?php echo esc_url( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" />
                        <?php endif; ?>
                    </div>
                    
                </div>

            </div>

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

Answer №1

If you're searching for a way to make images fit inside specified elements, you can use object-fit: cover;. This property works similarly to background-size: cover, but specifically for <img> tags. It sounds like this might be the solution you're seeking.

Answer №2

To achieve the desired effect, remember to include display: block;

In cases where object-fit: cover; is not enough, consider using object-fit: 100% 100%;

Answer №3

To achieve the desired result, I suggest adding width and height to the element (even if you don't include display: block).

<style>
    .hero-interior-banner-image {
        height: auto;
        position: relative;
    }
    .interior-hero-overlay {
        position: absolute;
        top: 0px;
        left: 0px;
        z-index: 99;
        width: 60px;
        bottom: 0px;
        height: 100%;
        object-fit: cover;
    }
    .hero-tertiary img {
        display: block;
        object-fit: cover;
        width: 100%;
        height: 100%;
    }
</style>
<div class="col-12 col-xl-6 px-0">

<div class="d-none d-xl-block hero-interior-banner-image float-end">

    <img class="interior-hero-overlay" src="<?php echo get_template_directory_uri(); ?>/images/hero/hero-overlay-<?php the_field( 'color' ); ?>.svg" alt="Hero overlay" />

    <div class="hero-tertiary">
        <?php $image = get_field( 'image' ); ?>
        <?php if ( $image ) : ?>
            <img class="img-fluid hero-tertiary" src="<?php echo esc_url( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" />
        <?php endif; ?>
    </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

How can you use CSS animations to animate two images in a way that hides one while showing the other?

click here for the image link visit this webpage for the link I need I am looking to add an animated section to my website. The inspiration comes from the webpage linked above, where images slide down one after another in a seamless manner. I attempted t ...

What is the best way to insert an additional div within a div that already exists?

let latestRules = []; I need to include: <div className='cancel-fee-icon'/> As a nested element within the following parent element: latestRules.push(<div className='cancel-fee' dangerouslySetInnerHTML={createMarkup(hotelPoli ...

Learn how to display every ASCII or EBCDIC character once radio buttons have been selected

I'm currently working on a simple website that requires the user to input a minimum of 256 characters, choose between ASCII or EBCDIC for conversion, and then click the submit button (Run) to display the final converted result on the page. However, I& ...

Utilize dynamic inline styling to pass asset image paths as background images in Nuxt.js

I am in the process of developing a Nuxt.js application and I have a requirement to dynamically set the background image of an element using images from the assets folder. Here is what I have implemented: <template> <div> <div v ...

"Utilizing AngularJS to streamline the process of inserting and updating content on a

Today's challenge is going to be a lengthy one :) Here's the scenario: I want to use the same HTML page for two controllers. The issue arises when the page in the insert state fails to load due to ng-repeat="employee in employee" which is nonexi ...

Ways to incorporate a CSS template into JSF

I'm currently developing a web application using Java NetBeans, and I am looking to incorporate a CSS template for the visual interface. However, I am unsure of how to go about implementing it. ...

Steps for adding a div after a specified number

To display the following div after getting a value greater than or equal to the specified value and retrieving it through ajax: 1. How can I show the below div with the given value? .mainbox{ width:auto; height:auto; padding:20px; background:#f00; } .i ...

Unlocking the hidden div puzzle in Bootstrap: A step-by-step guide

Currently, I am working on a website project that utilizes Bootstrap. On the right side of the page, there is a column containing a search module and various other elements. However, this column is hidden on smaller screens. I am looking to bring back only ...

What is the best way to remove a scrolling event handler from a particular element or class in JavaScript?

My goal is to set up a Table of Contents (TOC) in Obsidian that remains visible as you scroll through the page. Everything functions properly until you reach about halfway down the page, at which point the TOC mysteriously vanishes. #TOC, .TOC { posit ...

Setting an action when clicking on a slice of a Doughnut chart using Chart.js

I have been working on integrating chart.js into my Django Project, and so far it has been going smoothly. I successfully created a doughnut chart with two slices. Now, I am trying to implement separate actions for each slice when clicked, such as redirect ...

The currency exchange script is malfunctioning and not functioning properly

Is there a solution for accessing the JSON value that is currently eluding me? If anyone has any suggestions, I would greatly appreciate it. function forex() { var to = document.getElementById("to").value; alert(to); var from = document.getE ...

Display as selected when the option is chosen

Two APIs are available to retrieve permissions data: one returns a list of all permissions, while the other returns a list of selected permissions. The goal is to display a list of permissions in checkboxes, with a checkmark next to any permissions that ha ...

Individualize participants and thwart repetition

I need help separating 3 radio players using HTML code and ensuring they remain distinct entities. What code should I use between them to achieve this? For example, all 3 radio players end up looking like the last one at the bottom if not separated correc ...

The icons at the bottom of the page are not properly aligned

I am having trouble aligning my footer icons, specifically the first 2 images. While the bootstrap icons are centered correctly, the first 2 images are not aligning as expected. I have tried various methods like align-items-center, justify-content-center, ...

Can CSS be used to eliminate shadows from a particular image?

Is it possible to eliminate the shadow from a specific image that is displayed multiple times on various pages of a website? This particular image should not have any shadow. Link to Image The image can be found on pages like this one: Page Link Below ...

Guide to efficiently centering the background image in your existing Bootstrap template

I'm currently working on ensuring that the background image stays within the user's window, regardless of the device being used. While I have successfully added the background image, I am facing challenges in centering it properly. The current ou ...

Having difficulty parsing HTML with JSoup

I am currently trying to fetch HTML data from a website in order to download a 480p video. However, I am facing issues with the process when using an Async Class. Below is my doInBackground Method: @Override protected Void doInBackground(Void... v ...

Attempt to create a truncated text that spans two lines, with the truncation occurring at the beginning of the text

How can I truncate text on two lines with truncation at the beginning of the text? I want it to appear like this: ... to long for this div I haven't been able to find a solution. Does anyone have any suggestions? Thanks in advance! ...

"Utilize openlayers' map.updateSize function to automatically adjust the map size in response to

When trying to display a side panel, I want to ensure that the map's proportions stay within the new div width or height. Currently, the map is simply resized, causing the countries to appear distorted. I attempted to use map.updateSize without succes ...

Is it possible to utilize a WordPress page as a CSS stylesheet?

Is there a way to customize my WordPress theme's CSS through a Page in the backend rather than editing the default style.css file? I've seen it as a page template before but I'm having trouble figuring it out on my own. I'm currently r ...