Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to fit the image into modal popup using Bootstrap?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Modal plugin allows us to add a dialog popup window that is displayed on top of the current page. Bootstrap provides an easy, yet effective way to incorporate modal into our web pages. Modal can consist of any content along with a header and footer.

Images can be fitted in modal popup using Bootstrap by including <img> tag in the “modal-body” div. The “modal-body” div determines the main content of modal popup. By using the <img> tag, an image can be inserted into it.

This is illustrated in the following example:

Example 1:

HTML




<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href=
        integrity=
"sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
        crossorigin="anonymous">
</head>
<body>
    <button type="button" class="btn btn-primary"
        data-toggle="modal" data-target="#exampleModal">
        Click to view modal
    </button>
    <!--Bootstrap modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1"
        role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <!-- Modal heading -->
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">
                        GeeksforGeeks
                    </h5>
                    <button type="button" class="close"
                        data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">
                            ×
                        </span>
                    </button>
                </div>
                <!-- Modal body with image -->
                <div class="modal-body">
                    <img src="gfg.png" />
                </div>
            </div>
        </div>
    </div>
    <script src=
        integrity=
"sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous">
    </script>
    <script src=
        integrity=
"sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
        crossorigin="anonymous">
    </script>
    <script src=
        integrity=
"sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous">
    </script>
</body>
</html>

Output:

Example 2: Here an image has been used instead of the button, which triggers modal popup on click. The content of modal has been center aligned using “text-align: center”. Class “w-100” has been added to modal title so that it occupies 100% width of the parent div and becomes center aligned due to “text-align: center”.

HTML




<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href=
        integrity=
"sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
        crossorigin="anonymous">
    <style>
        /* Content of modal div is center aligned */
        .modal {
            text-align: center;
        }
    </style>
</head>
<body>
    <img src="gfg.png" data-toggle="modal"
        data-target="#exampleModal">
    <div class="modal fade" id="exampleModal" tabindex="-1"
        role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <!-- w-100 class so that header
                div covers 100% width of parent div -->
                    <h5 class="modal-title w-100"
                        id="exampleModalLabel">
                        GeeksForGeeks
                    </h5>
                    <button type="button" class="close"
                        data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">
                            ×
                        </span>
                    </button>
                </div>
                <!--Modal body with image-->
                <div class="modal-body">
                    <img src="gfg.png" />
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger"
                        data-dismiss="modal">
                        Close
                    </button>
                </div>
            </div>
        </div>
    </div>
    <script src=
        integrity=
"sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous">
    </script>
    <script src=
        integrity=
"sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
        crossorigin="anonymous">
    </script>
    <script src=
        integrity=
"sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous">
    </script>
</body>
</html>

Output:


My Personal Notes arrow_drop_up
Last Updated : 05 May, 2022
Like Article
Save Article
Similar Reads
Related Tutorials