How come my overlay consistently extends beyond the boundaries of my image?

The overlay on my current image is extending beyond the image's current size. I initially resized the image because it was too large for my needs, and now the overlay is not functioning correctly

HTML:

<div class="col-md-5 order-md-2 text-center">
    <h2 class="title-dark">1957-1979</h2>
    <div class="image-container">
        <img src="../images/stanford_car.png" alt="Stanford Car" class="image-style">
        <div class="overlay">
            <div class="overlay-text">Stanford Car</div>
        </div>
    </div>
</div>

CSS:

.image-container {
    overflow: hidden;
    position: relative;
}

.image-style {
    width: 70%;
    height: auto;
    border-radius: 15px;
}


.overlay {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    display: flex;
    opacity: 0;
    transition: .5s ease-in-out;
    background-color: rgba(0, 0, 0, 0.7);
    box-sizing: border-box;
    border-radius: 15px;
}

.image-container:hover .overlay {
    opacity: 1;
}

.overlay-text {
    color: white;
    font-size: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
}

Answer №1

Consider these 2 alternatives:

Approach 1: Modify the overlay width

A straightforward fix is to match the width of the .overlay class to the .image-style class (e.g., width: 70%;).

.image-style {
    width: 70%;
    height: auto;
    border-radius: 15px;
}


.overlay {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 70%;
    /* ... */
}

Approach 2: Define a set width for the <img>

The alternative is to specify a fixed width for the image instead of a percentage. For example, width: 1000px; can be used. This might cause the container to still have a maximum width, resulting in an oversized overlay. To address this, adjust the width of the .image-container to fit-content.

.image-container {
    overflow: hidden;
    position: relative;
    width: fit-content;
}

.image-style {
    width: 1000px;
    height: auto;
    border-radius: 15px;
}

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

Certain Android devices may experience compatibility issues with the web app

I am facing an issue with my webapp on Raspberry Pi, as it is not being recognized properly by all my Android devices. The server hosting the website is a Raspberry Pi 3 running Apache and everything is up to date. To protect privacy and security, all IP ...

Receiving Null Value Upon Asynchronous API Call Before Data Retrieval

Struggling with Fetching and Displaying API Data in a Table I am facing a challenge where I need to fetch an API multiple times and populate the data into a table. The issue arises when the data for a specific year is not available, causing the table to b ...

Creating dynamic charts in Javascript using Firebase

My dad recently asked me to enhance our motor home by integrating an Arduino with Firebase to monitor the water and propane tanks. He's hoping to be able to check tank levels from his phone so he can see when they need refilling. I've successful ...

What is the best way to implement pushstate degradation – using backbone.history or history.js?

I am interested in implementing pushstate functionality, which involves making an ajax request, changing the URL, and adjusting the browser history stack. However, since pushstate is not universally supported by all browsers, I need to find a workaround f ...

What is the best way to adjust the position of a dropdown menu in Material UI?

I am currently working on a Gatsby application and I'm trying to achieve a dropdown menu effect when the Menu icon is clicked, with the menu appearing right under the header and expanding to 100% width. Below is the code snippet that I have been using ...

Working with PHP to transfer toggle switch information to JSON

I'm currently experimenting with a combination of HTML, JavaScript, and PHP on a single page. My goal is to update a JSON file with either 0 or 1 based on the toggle switch state change. I seem to be close to achieving this functionality, but upon rel ...

How can I utilize JQuery to dynamically refresh a dropdown menu?

My dropdown list is initially empty: <div> <label>Boarding Point </label> <select title="Select pickup city" id="boardingDropdown"> </select> </div> I am trying to popula ...

Issue with JQuery addClass functionality in Firefox

I've come across numerous posts on this topic, but none of them have provided a solution. I recently added drag and drop functionality to my website. When I drag an item over a valid container, I add a specific class to it. Here is the HTML for the ...

Stop Internet Explorer from automatically scrolling when it gains focus

Please access this fiddle using internet explorer (11): https://jsfiddle.net/kaljak/yw7Lc1aw/1/ When the page loads, the <p> tag is focused and Internet Explorer slightly scrolls the element so that the table border becomes invisible... document.qu ...

Tips for creating code that will allow users to update their status on a web application, similar to Twitter

I am interested in developing a web application that can receive status updates (text) from an Android device. My plan is to use Ajax and HTML for this project. I'm looking for guidance on how to dynamically update the status on the web page without r ...

ASP.NET Core MVC: Issue with Passing C# Razor Variable to HTML Tag

GitHub Repo: https://github.com/shadowwolf123987/Gun-Identification-App I'm currently facing an issue in passing the values of two C# variables from the code block defined in my view to the corresponding html tags within the same view. Unfortunately, ...

What is the best way to navigate between different areas of an image using html and javascript?

I am currently in the process of learning how to develop mobile applications, and I am still in the early stages. Although this question is not directly related to mobile development, it pertains more to html/css/js. My goal is to create a simple game wh ...

Guide on building a personalized button layout in Bootstrap 4

I have been working on a React component that I want to stick to the bottom of the screen no matter what content is above it. Even if there is minimal content, I want it to remain at the bottom. To achieve this, I added style={{position: 'fixed' ...

Is there a way to access other HTML pages in a separate folder from the index file when running npm start?

At the moment, I have a simple index.html file and would like to access the app.html file located in a subfolder from the index.html. You can see the folder structure here: https://i.sstatic.net/IGcOk.png My app.html file is inside the 'app' sub ...

transferring a JavaScript variable to PHP upon submitting a form

This question arises after my previous inquiry on the topic of counting the rows in an HTML table using PHP. Since I didn't find a solution that worked for me, I am exploring new methods but struggling with the implementation. My approach involves ass ...

Using Node.js to send instructions to an HTTP server in order to highlight or add color to specific elements within

I have set up a server that receives data from a gaze sensor, as well as an HTTP server that serves an HTML page. I am looking for a way to dynamically highlight elements in the HTML based on the incoming data. Any suggestions on what resources or techniqu ...

Transport parameter via routerLink to a different component

When I click on the link provided in the list-profile HTML document, I need to send a variable. However, the current code is not working and causing crashes. If more details are required, feel free to ask. List Profile Component HTML <div class="col c ...

How to change the padding of a parent element in CSS3?

I've created a circle-shaped div element using the following code: #circle{ width: 320px; height: 320px; background-image: url('image.jpg'); border-radius: 50%; background-position: 50% 50%; transition: all 1s; } This circle expands on hov ...

Using select tags in conjunction with JavaScript can add dynamic functionality to your website

I've been working on adding dropdown menus to my website and have been using code similar to this: <select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</opti ...

Webpack Encore: SCSS import order causing unexpected issues

When developing with Symfony + Webpack Encore, I aim to split styles into "layout" and "page-based" categories for convenience. However, despite this organization, I still prefer to compile all styles into a single CSS file. To achieve this, I structure my ...