Issue with image position shifting due to Bootstrap 4 Modal opening and closing

In the code snippet below, a bs4 modal is opened through a button when hovered over.

The problem arises when the modal is closed after being opened, causing the image to move unexpectedly. This issue can be resolved by hovering over the container again. The reason behind this behavior and how to fix it are areas of interest.

Link to the code snippet.

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

.p-img {
  opacity: 1;
  width: 100%;
  transition: 0.5s ease;
}

... (CSS styling continues)

TOPTOPTOP

TOPTOPTOPTOPTOP

Update:

If the modal is triggered through javascript, the strange movement of the image will not occur.

<button type="button" class="btn btn-primary w-100">launch modal</button>

$(function(){
    $("button").click(function() {
    $("#myModal").modal('toggle');
  });
});

Answer №1

There is a peculiar relationship between three distinct conditions:

  • .container with overflow: hidden;
  • .container containing overflowing content
  • .container not being hovered over unless the mouse is moved, causing the modal to close.

The solution involves removing the overflow:hidden from .container and achieving the button clipping effect by utilizing the button's own container (.p-cont):

.container {
  position: relative;
}

.p-img {
  opacity: 1;
  width: 100%;
  transition: 0.5s ease;
  height: auto;
}

.p-capt {
  background: white;
  width: 100%;
  transition: 0.5s ease;
  opacity: 0;
  position: absolute;
  text-align: center;
  padding-left: 15px;
  padding-right: 15px;
  overflow: hidden;
  bottom: 0;
  height: 0;
}

.p-capt .btn-primary {
  position: relative;
  left: -15px;
}

.container:hover .p-img {
  opacity: 0.3;
}

.container:hover .p-capt {
  opacity: 1;
  height: 38px;
}


/* this removes the button's outline when active, as IMHO it didn't look right (it was cut off by the overflow:hidden). Totally optional, of course */

.p-capt .btn-primary:not(:disabled):not(.disabled).active:focus,
.p-capt .btn-primary:not(:disabled):not(.disabled):active:focus,
.p-capt .show>.btn-primary.dropdown-toggle:focus,
.p-capt .btn-primary.focus,
.p-capt .btn-primary:focus {
  box-shadow: none;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8b8a7b8b8adbae6a2bb88f9e6f9fee6f8">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

<h2>TOPTOPTOP</h2>
<p>TOPTOPTOPTOPTOP</p>

<div class="container">
  <div class="row">
    <div class="col">
      <img class="p-img d-block w-100 h-auto" src="https://www.wired.com/wp-content/uploads/2015/09/google-logo.jpg" />
      <div class="p-capt">
        <button type="button" class="btn btn-primary btn-block" data-toggle="modal" data-target="#myModal">launch modal</button>
      </div>
    </div>
  </div>
</div>
<!-- Modal -->
<div id="myModal" class="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Additionally, I chose to utilize Bootstrap version v4.4.1 instead of v4.0.0, but this change does not impact the example provided. The adjustment was made solely to rule out version discrepancies as a potential cause.

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

Is it feasible for a rollover to occur on a layered component?

Is there a way to achieve the following using CSS, jQuery, and PHP? I have a div element with the class button_background that contains a background image. Inside this div is an anchor tag (A tag) with a background that overlays the parent div's back ...

Unable to automatically calculate JavaScript when the number is not manually typed into the dropdown menu

Calculating A and B vertically from a dropdown combo box has been a challenge. Here is a sample data set: Data A B ---------------------------- 1 | 1 | 0 2 | 0 | 1 3 | 0 | 1 ---- ...

unusual problems with absolute positioning in CSS

I've run into a problem with absolute positioning in my CSS. Oddly, IE 10+ displays correctly, but Chrome gets distorted. See the images below: Any advice would be appreciated. (This distortion seems to have started after the Chrome 52 update) IE ...

The delete button in the Bootstrap modal is functional, however, there seem to be issues with its successful

Having an Issue with Bootstrap Modal in My View. <div id="myModal" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h4 c ...

What is the best way to display two radio buttons side by side in HTML?

Looking at my HTML form in this JSFiddle link, you'll see that when the PROCESS button is clicked, a form with two radio buttons appears. Currently, they are displayed vertically, with the female radio button appearing below the male radio button. I& ...

Having trouble resolving row border issues with CSS in DataTables?

I need to enhance the appearance of specific rows in my table by adding a darker border at the bottom. To achieve this, I have assigned a custom class (des-rec-row) to the rows and included the following CSS code: .des-rec-row { border-bottom: 1px soli ...

What could be causing sporadic disappearance of my background image?

Feeling overwhelmed by this sudden issue on my WordPress site. The logo isn't working properly, and I've saved this page as HTML for reference. Check out the link: Oddly enough, leaving a page open for a while or navigating away and returning t ...

Having Difficulty with Splicing Arrays in React?

Currently working on learning React and trying to develop my own mini-app. I'm basing it very closely on the project showcased in this video tutorial. I've run into an issue with the comment deletion functionality in my app. Despite searching va ...

The formatting of the list is not being correctly displayed in the Ajax Jquery list

Trying to dynamically generate an unordered list from XML onto a jQuery mobile page has been a bit of a challenge for me. While I can display the items on the page, the styling never seems to work properly, only showing up as plain text with blue links. Is ...

What is the process for altering the color of a table using JavaFX?

In my JavaFX application, I'm working with a table and I want users to be able to change the color of the selected element using a color picker. However, when I attempt the following code: menuBar.setStyle("-fx-background-color:" + settings.getRgbSt ...

"Exploring the world of CSS3: Arrow shapes and animated effects

Looking at the image provided, my task is to create and animate it. I was considering using CSS3 border-radius instead of an actual image. Below are the HTML and CSS code that I have so far. The animation I envision involves the arrow gradually appearing ...

Make the nav tabs in Bootstrap 4 stretch across the entire width of the screen with a bottom border

Looking for assistance with Bootstrap nav nav-tabs? How can I replicate the displayed behavior where the bottom border spans the full width of the screen and the tabs are centered on the page, as shown in this image? https://i.sstatic.net/WNRHB.png ...

``Stylishly Designed CSS Tabs inspired by Google Chrome's Modern

Utilizing Material UI Tabs in React to create curved tabs resembling those seen in Google Chrome. The design requires the parent element to have a bottom border that extends across the entire row, appearing on both the tabs and other content on the right. ...

Is it possible to customize the background color of the 'rows per page' selector box in Bootstrap 4 bootstrap-table?

The text is set in white on a dark grey background by default and appears just below the formatted table. Best regards, Phil Please refer to the attached image:Section of table showing rows per page selector box ...

Permanent Visibility of Bootstrap 4 Navbar Toggler Icon

I am facing an issue with my navbar-toggler as it remains visible even on a large screen where it is not needed. To view the file, click here. <div class="navbar-header"> <a href="#" class="navbar-brand">Logo</a> </di ...

Using Angular's [ngIf], [ngIfElse], and [ngIfElseIf] functionalities enables dynamic content rendering

Currently, I have the following code snippet: <ng-container *ngIf="someCondition"> <ng-template [ngIf]="cd.typedType === 'first'" [ngIfElse]="Second"> <div class="row"> fir ...

Creating a fixed column in an Angular Material Table for enhanced user experience

Has anyone found a method to implement a sticky first column in Angular Material using CSS? Check out the editable Stackblitz code here. I attempted to modify this approach from https://jsfiddle.net/zinoui/BmLpV/, but encountered issues where the first c ...

The height of the Bootstrap Sidebar is not displaying correctly

Currently, I am working on developing a traditional sidebar layout for the left portion of my template using Bootstrap 4. Here is the code snippet I have implemented: <nav class="team-left-column col-12 col-md-4 col-lg-2" style="background-color: ...

Transform Typescript into compiled css files without using any additional tools

Currently, I am working on a monorepo project where the main project relies on another project called components. When running the entire monorepo, the main project utilizes webpack.dev while the components project simply uses the TypeScript compiler. It l ...

Is there a way to align my text to the top of the divs in my layout?

I am struggling with positioning text within my divs. No matter what I try, the text does not appear at the top of the div where I want it to be. I attempted setting the position to fixed and adjusting padding and margin properties, but nothing seems to wo ...