为了账号安全,请及时绑定邮箱和手机立即绑定

如何删除此 Bootstrap 4 模式右侧的多余空间?

如何删除此 Bootstrap 4 模式右侧的多余空间?

Go
青春有我 2023-08-21 15:04:58
这是小提琴: https: //jsfiddle.net/apo5u0mt/这是代码:超文本标记语言<div class="modal" id="galleryModal">  <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-xl">    <div class="modal-content">      <div class="gallery">        <div class="gallery-item">          <img src="https://preview.ibb.co/kPE1D6/clouds.jpg">        </div>        <div class="gallery-item">          <img src="https://preview.ibb.co/mwsA6R/img7.jpg">        </div>        <div class="gallery-item">          <img src="https://preview.ibb.co/kZGsLm/img8.jpg">        </div>      </div>    </div>  </div></div><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#galleryModal">Modal</button>CSS.gallery {  overflow-y: auto !important;}.gallery-item {  margin: 5px;  float: left;}.gallery-item img {  height: 150px;  width: 200px;  object-fit: cover;}.btn {  margin: 5px;}模态框的右侧有一些额外的空间。我不想要那个空间。我希望模态适合内容,即图像。我已经盯着这个太久了,非常感谢任何帮助。
查看完整描述

1 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

Bootstrap 正在这样做:


.modal-dialog {

  max-width: 500px;

}

该.modal-dialog元素是块元素,因此默认情况下希望宽度为 100%,但 Bootstrap 的样式将其宽度保持为 500px。

您明确将每张图像的宽度设置为 200 像素,这(暂时忽略边距)仅加起来为 400 像素。



两个可能的修复方法是:

1. 在这里重写 Bootstrap 的模态样式,将模态限制为更窄的宽度:

/*

  The value below is empirically tested,

  allows for your given margins & floating. I originally expected it to be

  420px = (200px width + 5px left margin + 5px right margin) * 2

  but 422px was necessary to avoid wrapping when tested in jsfiddle

*/


.modal-dialog {

  max-width: 422px;

}

https://jsfiddle.net/nftj9s7y/1/


如果图像的宽度恰好为 200 像素很重要,那么这就是您的解决方案。然而,这对我来说似乎是一个脆弱的解决方案 - 如果您决定更改图像宽度,它就会崩溃,并且可能无法在较小的屏幕尺寸下工作。




更灵活的解决方案是:


2.使用flexbox让图像扩展以填充模态框

.gallery {

  display: flex;

  flex-wrap: wrap;

  padding: 5px;

}


/* now that the parent element is display: flex, we don't need floats anymore */

.gallery-item {

  padding: 5px;

  width: 50%;

}


.gallery-item img {

  height: 150px;

  object-fit: cover;

  width: 100%; /* allow image to fill width of containing element */

}

https://jsfiddle.net/vzs98n6b/2/




最后一点,除非您在该元素上指定或,.gallery { overflow-y: auto }否则不会有任何效果。heightmax-height


查看完整回答
反对 回复 2023-08-21
  • 1 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信