How can you style the modal image to center it? Which <div> should you target for styling?

I'm facing an issue with my modal image within a Bootstrap 4 carousel that uses JQuery. When I open the modal, it doesn't appear centered on the screen. Could you advise me on which parent div to style and how? Additionally, how can I ensure the image stays proportional and responsive on smaller screens?

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">      
      <img class="pop d-block" src="images/getonboard.png" alt="First slide">
    </div>
  <div>
  <a class="carousel-control-prev" href="javascript:void(0) role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="javascript:void(0) role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>


<!-- Modal Images Link -->


<!-- Geton Modal -->
<div class="modal fade" id="imagemodal" tabindex="-1" role="dialog" aria- 
   labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div style="width: 800px; height: 500px;" class="modal-content">              
      <div class="modal-body">
        <button type="button" class="close" data-dismiss="modal"><span aria- 
          hidden="true">&times;</span><span class="sr-only">Close</span> 
         </button>
        <img src="images/getonboard.png" class="imagepreview" style="width: 
         100%;" >
      </div>
    </div>
  </div>
</div>



$(function() {
    $('.pop').on('click', function() {
      $('.imagepreview').attr('src', $(this).find('img').attr('src'));
      $('#imagemodal').modal('show');   
    });   
});

Answer №1

The reason the modal-content is not centered is because inline styles have been added to it.

<div style="width: 800px; height: 500px;" class="modal-content">

To fix this, remove the inline styles and use media queries instead.

Using the "img-fluid" class allows images to take up the full width of their parent element.

<img class="img-fluid" src="https://cdn.pixabay.com/photo/2017/03/26/16/06/architecture-2175925_960_720.jpg" class="imagepreview" >

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">      
      <img class="pop d-block" src="https://cdn.pixabay.com/photo/2017/03/26/16/06/architecture-2175925_960_720.jpg" alt="First slide">
    </div>
  <div>
  <a class="carousel-control-prev" href="javascript:void(0)" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="javascript:void(0)" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>


<!-- Modal Images Link -->


<!-- Geton Modal -->
<div class="modal fade show" style="display: block" id="imagemodal" tabindex="-1" role="dialog" aria- 
   labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">              
      <div class="modal-body">
        <button type="button" class="close" data-dismiss="modal"><span aria- 
          hidden="true">&times;</span><span class="sr-only">Close</span> 
         </button>
        <img class="img-fluid" src="https://cdn.pixabay.com/photo/2017/03/26/16/06/architecture-2175925_960_720.jpg" class="imagepreview" >
      </div>
    </div>
  </div>
</div>

Answer №2

Your CSS is hardcoded onto the modal-content class, which may be causing issues. Consider removing the style and see if it fixes the problem.

<div style="width: 800px; height: 500px;" class="modal-content">
.

Visit this Fiddle for a demonstration: https://jsfiddle.net/2ofkxa8r/

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

Changing the position of a Bootstrap popover dynamically from top to bottom

Struggling with the bootstrap popover here. I can't seem to change the popover position from top to bottom when it reaches the top of the viewport. I attempted to use placement: 'auto bottom' but unfortunately, it didn't do the trick f ...

Is it necessary to include the Fonts file (which accompanies Bootstrap files) in the HTML document?

As a beginner in HTML, I decided to incorporate Bootstrap into my project. After downloading the necessary files, including css, js, and fonts, I found myself able to easily add the css and js files using the appropriate tags. However, I encountered some c ...

Modifying the font style of text within an HTML string using SQL Server

I need to standardize font sizes in my database where strings are stored as html and users can modify the font size. However, for generating reports, I require all font sizes to be consistent. If I have the following html code, how can I change the font si ...

Continuously querying the database in JSP to automatically fetch data

I've been exploring Spring recently and I'm curious if there is a way to set up automatic database polling using Spring 4. Ideally, I'd like the code to run without requiring the user to manually refresh any pages. Is it possible for my jsp ...

Changing the CSS background in React when updates occur

Currently working on toggling the CSS background image within this React application. The images have been successfully imported and a variable called "image" is set to the imported image as the iteration increases/decreases with each click. The issue I&a ...

I am struggling to retrieve the data from the Giphy API after making the initial AJAX request

I'm currently in the process of building a basic website that fetches random gifs from the Giphy API. This project is purely for practice, so I'm keeping the site very minimalistic. However, I've hit a snag when it comes to extracting data u ...

The z-index of 999 in CSS is not functioning as expected on both Google Chrome and Safari browsers

To ensure the slider text is readable in white color, we have applied a black background with 0.65 opacity CSS on the slider image. This allows the white text to stand out effectively. The following CSS code was used for this effect: .zlslides .ms-slide- ...

In need of assistance with adding a divider within a box using HTML/CSS

As a beginner, I may have messy code but I'm working on creating a job search page with a bar displaying the results like this: I've almost got it, except I can't figure out how to add that divider between PREV, 1 to 100, and NEXT. My curre ...

What is the simplest way to send an AJAX request to run a PHP script?

Hey everyone, I'm facing a specific issue on my website that I haven't been able to solve by searching online. Therefore, I decided to create this question myself. What I'm trying to achieve: When I click a button on my site, it triggers a ...

Capture all input values within an array from a dynamically generated table using jQuery

I have utilized jQuery to create a dynamic table with the following code: $.ajax({ data : data, type : "get", url : url, dataType : "json", error : function(resp){ alert("Error !! ...

What happens when absolute positioning looks for "relative" inside nested divs?

I'm struggling to grasp the concept of nested divs and how they work together. I understand that when you have a "position absolute" element inside a "position relative" element, the former looks for the latter as its reference point. But what happens ...

Increasing the values of jQuery variables

I've written this code to assign different ids to variables, as demonstrated below: var numgrid = jQuery('.panel-grid').length; for (var i = 0; i < numgrid; i++) { var currentId = "#pg-"+id+"-"+i; if (jQuery(currentId)[0]) ...

Is it possible for me to create a menu using the .row and .col classes from Bootstrap?

I attempted to create a menu using Bootstrap's cols. Something similar to this: https://i.stack.imgur.com/55xst.png However, I faced numerous challenges when trying to implement it into Wordpress and realized that it may not be the best approach... ...

Issue with hashtag in regular expressions

Looking for a way to identify and link hashtag words with an anchor tag? Here's a snippet showcasing the concept: <p class="display"></p> var content = "I enjoy #blueSky. My favorite color is #green. #sky is amazing"; ...

The partnership between Selenium and WhitePages has created an innovative solution

I am currently attempting to register on . The registration process requires entering my first name, last name, email address, and password. For the first name field, I attempted to locate the element by CSS using the syntax "input#name_fname", however, I ...

Using jQuery and AJAX to submit a dynamic form

I am currently facing an issue with cloning a HTML drop down list using jQuery. The main problem I am encountering is that there seems to be no restriction on the number of cloned sections, and I need to store all these sections in a mySQL database. How c ...

What is the best way to extract text following a particular symbol in javascript or Jquery?

If I have a string that reads and the cow went @moo, and my goal is to exclusively choose "moo"... what steps should I take? ...

The Bootstrap navigation bar isn't displaying properly on mobile devices

Help! My Bootstrap navbar is not working properly on mobile view. The menu icon shows up but when clicked, no items are displayed. Here is the HTML code for my navbar: <header class="header_area"> <div class="main-menu"> ...

Pointer-enhanced Material UI Popup

Is there a way to implement a Popup that looks like the image provided using @Material-ui? https://material-ui.com/ I attempted to use Popover, however Popper does not include a pointer like in the image. https://i.stack.imgur.com/atz0G.png ...

using jquery to select multiple checkboxes with not() and contains()

I have a situation where I need to read multiple checkboxes and combine their values into a comma-separated string. Using this string, I aim to hide certain elements in a table that do not contain the specified values from the checkboxes. Each checkbox cor ...