【问题标题】:how to increase the div height dynamically based on the image present inside it?如何根据其中存在的图像动态增加 div 高度?
【发布时间】:2020-08-09 03:13:37
【问题描述】:
<div id="my-modal" class="modal">

      <span class="closed" onclick="closeModal()">
        <img
          data-src="img/close-left-product-ui-ux-design-cover.webp"
          style="width: 24px; height: 24px;"
          alt="close button"
          class="img-fluid lazyload"
        />
      </span>

      <div class="modal-content">

        <div class="my-slides">

          <img
            src="img/image1@2x.webp"
            class="img-fluid lazyload"
            alt="ecolight product"
          />
        </div>
      </div>
    </div>

CSS

.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 50px;
  padding-bottom: 50px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.8);
}

.modal-content {
  position: relative;
  border-radius: 4px;
  padding: 24px;
  margin-left: auto;
  margin-right: auto;
  width: 70%;
  max-width: 1000px;
  border: none;
  background-color: #fff;
}

.my-slides {
  width: 100%;
}

.my-slides img {
  width: 100%;
}

我希望我的类 modal-content 和类 my-slides 根据其中存在的高度动态增加。使用此代码,容器高度不会增加。我有不同高度的图像。有没有办法解决这个问题?

【问题讨论】:

标签: javascript html jquery css css-selectors


【解决方案1】:

您可以通过将值 max-content 用于 height .modal-content 元素的 CSS 样式来解决此问题,例如:

.modal-content {
  position: relative;
  border-radius: 4px;
  padding: 24px;
  margin-left: auto;
  margin-right: auto;
  width: 70%;
  max-width: 1000px;
  border: none;
  background-color: #fff;
  height: max-content;   
}

Working Fiddle

【讨论】:

  • 对不起,我来晚了。当我设置这个高度时,它工作得很好,但是当我在 chrome android(仅限移动设备)中向下滚动模式时,底部会出现空白。
  • 很抱歉,我无法测试它,因为我主要在 Web 应用程序上工作。请在您的问题、所有信息和演示中发布一个新问题,以便其他移动开发人员可以对其进行调查并提供更多见解。谢谢!
  • 我的意思是在移动设备中以 chrome 浏览网页时,会出现此问题。我已经发布了这个问题。如果你能帮助我,请。链接:stackoverflow.com/questions/61780016/…
猜你喜欢
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
相关资源
最近更新 更多