Using the <img> tag instead of applying background-image with CSS

Currently, I have successfully implemented a background using the CSS background-image property. However, I am interested in utilizing the HTML <img> tag instead. The existing code with the background-image property looks like this:

<section className="banner">
    <div className="container">
        <div className="wrapper">
            <p>bla bla content, buttons, etc.</p>
        </div>
    </div>
</section>

.banner {
    display: flex;
    align-items: center;
    background-size: cover;
    height: 428px;
    margin-bottom: 11px;
    background-color: #000;
    background-image: url("/image.jpg");
}

In an attempt to switch to using the <img> tag, I followed a guide from this post and came up with the following structure:

<section className="banner">
    <img
        alt=""
        src={"/image.jpg"}
    />
    <div className="container">
        <div className="wrapper">
            <p>bla bla content, buttons, etc.</p>
        </div>
    </div>
</section>

.banner {
    display: flex;
    align-items: center;
    overflow: hidden;
    position: relative;
    
    height: 528px;
    width: 100%;
    margin-bottom: 11px;
    background-color: #000;

    .img {
        position: absolute;
        object-fit: cover;
        min-width: 100%;
        max-width: 100%;
        z-index: -1;
    }
}

Unfortunately, this new implementation does not properly display the content within the wrapper, and cuts off part of the image on the right side. How can I resolve this issue?

Answer №1

If you assign a z-index to the img element and set all other elements to have higher z-index values, you can control their stacking order.

img { z-index: -1; }
.banner-options: { z-index: 0 }

With this setup, the img will be positioned more towards the front, while .banner-options will appear even further in front.

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

Sending a CSS class to an Angular library

In my development process, I am currently working on creating a library using Angular CDK specifically for custom modals. One feature I want to implement is the ability for applications using the library to pass a CSS class name along with other modal conf ...

Simple 3-column grid design in HTML and CSS

My task is to design a 3-grid layout that resembles the following structure: ------------------------------------- | Image | The name of logo | > | ------------------------------------- I attempted this using the code below: <div class=" ...

Unable to access the suggestion list within the modal

I incorporate the PrimeNG AutoComplete feature within a PrimeNG Dialog, displaying a list of elements below the AutoComplete in the dialog. My objectives are: To set the max-height of the modal dialog to 300, and have a visible scrollbar if the total ...

Customizing ExtJS Themes for Ext.panel.Panel

In my current project using ExtJS 4.1.1a, I am working on creating a new theme for a "tabbedPane" and a normal Panel with an "Accordion" layout. However, I am facing difficulty in changing the color of the headers for both elements. I wish to customize the ...

Is there a way to make this animation activate when the entire area in front of the link is hovered over or tapped?

I am working on an animation that needs to run behind a link. Here is an example: https://codepen.io/ehbehr/pen/KKNbOvN and below is the HTML and CSS: *, *:before, *:after { margin: 0; padding: 0; } .container { position: relative; width: 100%; ...

How to pass arguments to a function within a preg_replace in PHP

Greetings! I currently have a situation similar to this: $content = preg_replace('#(\s*)\<pre(.*?)\>(.*?)\</pre\>(\s*)#sie', 'dostuff(\'\\3\', \'\\2 ...

How to display a "data not available" notification in AngularJS ngTable plugin tables when the data response array is void of content

I need to implement a no data message in my ngTable table when the response data array is empty. Currently, I have the following code: <tr ng-repeat="row in $data"> <td data-title="'name'" filter="{name: 'text& ...

After reducing the size of the table, the data spills over

Hey there, amazing individuals of the internet! Hope you're all doing well today. I've encountered a rather perplexing issue that's got me stumped. So, here's the situation - I have this table full of data and I want it to initially be ...

Creating a Stylish CSS Table Utilizing DIV Elements: Preventing the Top Row from Scrolling

Utilizing a pure DIV table with the display: table, table-row, table-cell format for styling, I have managed to make the entire table scroll except for the header row. However, I am in search of methods that can prevent the header row (1st row) from scroll ...

Arrange the icons in multiple rows to ensure an equal distribution of icons on each row

There are 12 image icons displayed in a row within div elements using display: inline-block. However, when the browser window is resized, I would like the icons to be arranged on two or more rows so that each row contains an equal number of icons. ...

Adjusting the header and unordered list on resize

I need help with my portfolio website. I can't seem to figure out how to make my header and navigation menu stack on top of each other when the page is small, and then go back to their normal layout when the page is larger. Below is the HTML code I&a ...

Identify Horizontal Swipe Gestures on Page-level

I am currently focused on ensuring accessibility for users utilizing voiceover technology. While navigating their phone, these individuals rely on right and left swipes to interact with elements on a page. I am seeking to implement swipe detection at the ...

Jumbotron featuring videos in a Bootstrap Carousel

I am attempting to create a bootstrap carousel featuring videos within a jumbotron. Despite my best efforts, I am struggling to make it work correctly. The videos appear too small within the jumbotron and seem to be overlaid by other elements. I have exper ...

Move the box upwards and change it to grayscale when hovering over the image, then reverse the effect when the hover

As I hover over the image, my goal is to make a gray box appear at the bottom and smoothly slide up. While it slides up, everything it covers should turn grayscale. When I move my mouse away from the image, I want the gray box to slide back down and remove ...

Tips for keeping a reading item in place while scrolling

To enhance the user experience, I would like to improve readability without affecting the scroll position. Question: Is there a way to fix the scrolling item at a specific position (item with the .active class)? I am looking to maintain a consistent read ...

Looking to perform an Ajax call to update a Rails model using HTML5 contenteditable, but encountering an issue where it creates a new

I am in need of assistance to update the plots of a story using HTML5 contenteditable feature. Below is the code snippet: Refer to file# for editing multiple plots <div id="editable" contenteditable="true"> <%= simple_format p.description %&g ...

Padding located within a div tag

As a novice in the world of HTML and CSS, I have designed a birthday card with a desired 50/50 split down the center. Placing an image on the left side was successful, however when trying to add text to the right, it ended up too close to the center line. ...

Ways to stop grabber applications from identifying and saving mp3 files directly from the web browser

I am in the process of creating a music streaming platform that caters to both premium and free users. https://i.sstatic.net/J9gok.jpg As shown in the screenshot above, users with grabber apps installed on their browsers (such as IDM and other download a ...

What was the inspiration behind Spotify's section design?

Spotify has cleverly hidden the overflowing content without showing a scrollbar, allowing users to swipe through the list. How did they achieve this? I am currently using tailwindcss if that makes a difference. https://i.stack.imgur.com/z59yv.png ...

How can I stop a table from resizing in Tailwind CSS?

So, I have created this table <table class="table-fixed border-separate border border-green-800"> <thead class="bg-indigo-800 border-b-2 border-indigo-200 w-full"> <tr> <th ...