Can't close a Modal using JS
  • 17-May-2023
Lightrun Team
Author Lightrun Team
Can't close a Modal using JS

Can’t close a Modal using JS

Lightrun Team
Lightrun Team

Explanation of the problem


When using Bootstrap 5 modals in a web application on Linux with the latest version of Chrome, a problem arises where the modal backdrop fails to hide after closing the modal. This issue can be observed in the following test case:


Troubleshooting with the Lightrun Developer Observability Platform


Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for: Can’t close a Modal using JS

To resolve this issue, a common workaround is to manually remove the modal backdrop element using JavaScript. While this solution may work, it is considered a temporary and somewhat hacky approach, as the responsibility of handling the backdrop should ideally be part of the Bootstrap API.

Here is an example of how to remove the modal backdrop element programmatically using JavaScript:




By executing the above code snippet, you can remove the backdrop element and address the problem of the lingering modal backdrop after closing the modal. However, it is recommended to keep an eye on updates and releases from Bootstrap, as the issue may be resolved in future versions with a more integrated solution within the Bootstrap API.


Problems with bootstrap


Problem 1: One common problem encountered with Bootstrap is the inconsistent spacing between columns in a grid layout. When using the grid system to create responsive layouts, developers may notice that the columns do not align properly or have uneven spacing. This issue can occur due to various factors such as incorrect column classes or conflicting custom CSS styles.

To resolve this problem, it is important to ensure that the correct column classes are applied to each element within the grid. The sum of column classes in a row should add up to 12 to maintain proper alignment. Additionally, developers should check for any conflicting CSS styles that might affect the spacing. Here’s an example of how to properly structure a row with three columns using Bootstrap’s grid system:


<div class="row">
  <div class="col-md-4">Column 1</div>
  <div class="col-md-4">Column 2</div>
  <div class="col-md-4">Column 3</div>


Problem 2: Another common problem with Bootstrap is the lack of customization options for certain components. While Bootstrap provides a wide range of pre-designed components, there may be instances where developers need to customize the appearance or behavior of a particular component beyond the available options.

In such cases, developers can use custom CSS styles or extend Bootstrap’s existing CSS classes to achieve the desired customization. By adding custom CSS classes or modifying the existing ones, developers can override default styles or add additional styles to tailor the component to their specific requirements. Here’s an example of how to customize the color of a Bootstrap button using custom CSS:


<button class="btn btn-primary custom-btn">Custom Button</button>


.custom-btn {
  background-color: #ff0000;
  color: #ffffff;


Problem 3: A common issue with Bootstrap is the lack of support for older versions of Internet Explorer (IE). Bootstrap’s CSS and JavaScript components may not render or function correctly in older versions of IE, causing layout inconsistencies or JavaScript errors.

To address this problem, developers can use polyfills or fallbacks to provide support for older versions of IE. Polyfills are JavaScript code that emulate newer browser features in older browsers, while fallbacks are alternative solutions that work specifically for older browsers. By including polyfills or fallbacks, developers can ensure that Bootstrap components work as intended in older versions of IE. Additionally, it is recommended to inform users about the limitations of Bootstrap in older browsers and encourage them to upgrade to a modern browser for optimal experience.


A brief introduction to bootstrap


Bootstrap is a popular front-end framework that provides a collection of pre-designed HTML, CSS, and JavaScript components for building responsive and mobile-first web applications. It offers a wide range of ready-to-use components such as navigation bars, buttons, forms, modals, and grids, which can be easily incorporated into web projects to enhance their visual appeal and usability.

One of the key features of Bootstrap is its responsive grid system. The grid system allows developers to create flexible and responsive layouts by dividing the web page into 12 equal columns. This enables the content to adapt and reflow based on the screen size, ensuring optimal display on various devices and screen resolutions. By using the appropriate grid classes, developers can easily arrange and align content within the grid, creating visually appealing and consistent designs.

In addition to the grid system, Bootstrap provides a comprehensive set of CSS classes and utility components that help developers streamline the development process. These classes and components offer various styling options, such as typography, colors, spacing, and responsive breakpoints, allowing developers to customize the appearance of their web applications with ease. Bootstrap also includes JavaScript plugins that add interactive functionality to the components, such as dropdowns, carousels, modals, and tooltips. With its extensive set of features and robust documentation, Bootstrap is a powerful tool for building modern and responsive web applications efficiently.


Most popular use cases for bootstrap


  1. Responsive Web Design: Bootstrap provides a responsive grid system and a wide range of responsive CSS classes that enable developers to create websites that adapt and adjust to different screen sizes and devices. By using Bootstrap’s grid classes, developers can easily organize and structure their content into responsive layouts. For example, the following code snippet demonstrates how to create a responsive two-column layout using Bootstrap’s grid system:


<div class="container">
  <div class="row">
    <div class="col-sm-6">
      <!-- Content for the left column -->
    <div class="col-sm-6">
      <!-- Content for the right column -->
  1. Pre-Designed Components: Bootstrap offers a rich collection of pre-designed components that can be easily integrated into web projects. These components include navigation bars, buttons, forms, cards, modals, and much more. By leveraging these components, developers can save time and effort in designing and implementing common user interface elements. For example, the following code snippet demonstrates how to create a navigation bar using Bootstrap:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Logo</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home</a>
      <li class="nav-item">
        <a class="nav-link" href="#">About</a>
      <li class="nav-item">
        <a class="nav-link" href="#">Contact</a>
  1. Customization and Theming: Bootstrap provides extensive customization options, allowing developers to easily modify the default styles and appearance to match their project requirements. By overriding Bootstrap’s default CSS classes or using its Sass variables, developers can create unique and personalized designs. Bootstrap also offers a theming system that enables developers to quickly switch between pre-defined themes or create their own. This flexibility empowers developers to create visually appealing and cohesive designs with minimal effort.

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.