html - 使 flex 元素的内容滚动而不是使容器更高

标签 html css scroll flexbox semantic-ui

<分区>

我有一个对话框:

$(document).ready(function() {
  $('.ui.modal').modal('show');
});
        .content {
            display: flex !important;
            flex-direction: column;
        }
                    
        .ui.modal > .content > .scroll {
            flex: 1;
        }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/fomantic-ui@2.7.6/dist/semantic.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/fomantic-ui@2.7.6/dist/semantic.min.js"></script>


<div class="ui overlay fullscreen modal">
  <div class="header">
    Dialog box
  </div>
  <div class="content">
    <div class="ui warning message">
      <div class="header">
        Be careful
      </div>
      This is a warning message
    </div>
    <div class="scroll ui segment">This box should scroll when the contents are too long.</div>
  <div class="ui segment">Part of the dialog box</div>
  </div>
  <div class="actions">
    <div class="ui approve button">Save</div>
    <div class="ui cancel button">Cancel</div>
  </div>
</div>

目前,如果大框(表示应该滚动的那个)的内容太长,那么它会使整个对话框滚动。我不想要这个,我希望框的内容滚动而不使整个窗口滚动,如下所示:

scroll dialog box

我该怎么做?

最佳答案

您可以将 max-heightoverflow-y: auto; 应用于该元素,以防止其高于特定高度并使其仅滚动如果根据其内容有必要:

$(document).ready(function() {
  $('.ui.modal').modal('show');
});
.content {
  display: flex !important;
  flex-direction: column;
}

.ui.modal>.content>.scroll {
  flex: 1;
}
.scroll.ui.segment {
  max-height: 120px;
  overflow-y: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/fomantic-ui@2.7.6/dist/semantic.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/fomantic-ui@2.7.6/dist/semantic.min.js"></script>


<div class="ui overlay fullscreen modal">
  <div class="header">
    Dialog box
  </div>
  <div class="content">
    <div class="ui warning message">
      <div class="header">
        Be careful
      </div>
      This is a warning message
    </div>
    <div class="scroll ui segment">This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when
      the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long.
      This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when
      the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long. This box should scroll when the contents are too long.
      This box should scroll when the contents are too long. This box should scroll when the contents are too long.</div>
    <div class="ui segment">Part of the dialog box</div>
  </div>
  <div class="actions">
    <div class="ui approve button">Save</div>
    <div class="ui cancel button">Cancel</div>
  </div>
</div>

关于html - 使 flex 元素的内容滚动而不是使容器更高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57421513/

相关文章:

html - 在未知高度 DIV 内垂直居中 DIV

javascript - CSS(滚动): why is it not possible to base the command on class name instead of id?

javascript - 您是否可以在 javascript 中使用 <a name=#page> 检查滚动到的位置?

jquery - 使用 jQuery 获取 "img"id

javascript - body onload 执行时是否有可能留下未读代码?

javascript - 将id和class属性添加到ajaxDisplayappendChild

html - 将 padding-left 和 padding-right 设置为元素宽度的 10%

html - 选择 "Top and Bottom"时,如何像 MS Word 那样在 HTML/CSS 中将文本环绕图像

javascript - 如何阻止 Kendo KO Grid 自动滚动

javascript - 滚动方向(向上和向下)