ahmed
ahmed

Reputation: 23

Div didn't open as a link when I prefer to "modal"

I want to develop image gallery. I want to present each image and its description under it and when I click on an image or text or white space got "modal"

I wrote this for an element in gallery

<div class="m-4 img99">
    <a href="#ml">
        <img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" class="simg " alt="..." data-toggle="modal" data-target="#ml">
        <div class="">
            <h5 class="">test</h5>
            <p>test</p>
        </div>
    </a>
</div>

The modal part is

<div class="modal fade" id="ml">
    <div class="modal-dialog modal-dialog-centered  modal-xl ">
        <div class="modal-content">

            <!-- Modal Header -->


            <!-- Modal body -->
            <div class="modal-body ">
                <div class="row">
                    <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 ">
                        <p>
                            test
                        </p>
                        <br>

                    </div>
                </div>
            </div>



        </div>
    </div>
</div>

and CSS part

 .simg {
        width: auto;
        height: 60px;
    }
 .img99 {
        margin-right: auto !important;
        margin-left: auto !important;
        width: max-content;
        height: max-content;
        border: 1px solid rgba(0, 0, 0, 0.15);
    }

My problem is when I click on a text or white space the model didn't work. "when I click on image :the model work normally"

https://codepen.io/ahof920/pen/MWYGRYp?editors=1100

What is the problem?

Upvotes: 0

Views: 32

Answers (1)

sanoj lawrence
sanoj lawrence

Reputation: 993

Replace your HTML with this:

Reason why its not working :

Modal are triggered by following tag's in bootstrap data-toggle="modal" data-target="#ml" and you added those inside <img> tag that's the reason modal is opened when clicked on image. So add data-toggle="modal" data-target="#ml" to <a>.

.simg {
  width: auto;
  height: 60px;
}

.img99 {
  margin-right: auto !important;
  margin-left: auto !important;
  width: max-content;
  height: max-content;
  border: 1px solid rgba(0, 0, 0, 0.15);
}
<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/[email protected]/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>

<div class="shadow2 m-4 img99">
  <a href="#ml" data-toggle="modal" data-target="#ml">
    <img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" class="simg " alt="...">
    <div class="">
      <h5 class="">test</h5>
      <p>test</p>
    </div>
  </a>
</div>
<div class="modal fade" id="ml">
  <div class="modal-dialog modal-dialog-centered  modal-xl ">
    <div class="modal-content">
      <!-- Modal Header -->
      <!-- Modal body -->
      <div class="modal-body ">
        <div class="row">
          <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 ">
            <p>
              test
            </p>
            <br>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions