【问题标题】:Auto-growing modal Dialog in CSS that should not grow out of screenCSS中的自动增长模式对话框不应超出屏幕
【发布时间】:2021-08-30 01:17:24
【问题描述】:

我目前正在开发一个针对平板电脑的网络应用。我想显示一个模态对话框(标题栏+正文),其增长(高度)不大于其内容要求,但如果内容要求的高度大于视口,则对话框应该有一个滚动条body 元素和视口/父级的最大高度。

我可以通过 CSS/JS 以某种方式实现这一点吗?

【问题讨论】:

  • 如果您提供代码将非常有帮助
  • 我在这里添加了我糟糕的尝试:jsfiddle.net/6jndgf34/40
  • 我添加了一个答案看看它是否解决了你的问题@user2316484

标签: javascript html css web progressive-web-apps


【解决方案1】:

你离得很近 这里看看 我在 .dialog 中添加了 overflow-y: hidden;,它可以工作

.screen {
    width: 800px;
    height: 200px;

    font-family: sans-serif;
    display: grid;
    place-items: center;
    background-color: gray;
  }

  div.titlebar {
    background-color: green;
    height: 32px;
    display: grid;
    place-items: center;
  }

  div.body {
    overflow-y: scroll;
    background-color: purple;

    /* setting height to 100% (of the parent) */
    /* minus the 32px of the header */
    height: calc(100% - 32px);
  }

  div.dialog {
    width: 400px;
    height: auto; /* auto size ... */
    max-height: 100%; /* ... but dont grow bigger than screen */
    display: flex;
    flex-direction: column;
    background-color: yellow;
    overflow-y: hidden;
  }
<div class="screen">
      <div class="dialog">
        <div class="titlebar">Title</div>
        <div class="body">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error
          praesentium aut laboriosam modi eius ex cum, ullam placeat, beatae,
          nobis est quasi voluptate alias necessitatibus excepturi? Voluptas,
          ut! Maxime consequuntur, quod? Ipsa ullam eveniet, dolore voluptas
          eius obcaecati vero sint aut at sapiente! Vel iure distinctio pariatur
          maxime, illo ab voluptate veritatis, porro delectus, earum molestiae
          at ipsam ducimus dicta. Laboriosam perspiciatis molestias voluptatibus
          modi dolorem ea asperiores assumenda alias minus, saepe facilis nam
          consequuntur nulla ipsum delectus totam itaque consequatur molestiae.
          Quibusdam nam, vero fugit mollitia minima dolor, eveniet obcaecati
          sint iste inventore explicabo eligendi ratione harum, tempore quasi.
        </div>
      </div>
    </div>

【讨论】:

  • 请将我的答案标记为已回答,以便对其他人有所帮助
【解决方案2】:

您可能只使用max-height: 100vh;overflow-y: auto;

如果您需要从模态框的高度移除一些边距,请随意减小max-height 的值或使用calc(100vh - 30px) 作为值。

【讨论】:

  • 不,遗憾的是这不起作用。似乎 max-height 被忽略了。我在这个小提琴中添加了代码:jsfiddle.net/6jndgf34/40 也许这可以解决我的问题。
  • @user2316484 在您的 jsfiddle 中,您缺少 overflow 部分。高度受到尊重,但由于您的 div 确实允许溢出,所以它是 .. 溢出的。 :) 只需将 overflow-y: auto; 添加到 div.dialog 即可。
  • 天哪!我怎么能错过这个..非常感谢。现在这确实花了我 2 个小时...:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 2020-09-09
相关资源
最近更新 更多