html - 如何将具有最大高度的元素与垂直中心对齐?

标签 html css

我的网站上有一个叠加弹出元素,我希望它在水平和垂直方向上都居中。我希望高度随着其中的内容而增长,但最多为窗口高度的 90%,然后转到滚动。

但问题是现在我想添加一个固定在弹出框底部的关闭按钮。我需要关闭按钮位于 overflow:auto 元素之外,但是将内容元素置于框内而不是框会使框不再垂直居中。如果您设置固定高度,它会起作用。但我希望高度是动态的并随内容一起增长,现在是内容框。

我已经尝试了所有我能想出并在网上找到的组合。我已经阅读了有关不同样式行为的文档。但是想不出解决办法。

这是一个没有关闭按钮的工作示例:

* {
  margin:0;
  padding:0;
}
body {
  font-family:Roboto;
  color:#fff;
  font-size:12px;
}
body, html {
  width: 100%;
  height: 100%;
}
h1 {
  padding-bottom:10px;
}
h2 {
  padding-bottom:7px;
}
p {
  line-height:1.5em;
  padding-bottom:10px;
}
.table {
    display: table;
    width: 100%;
}
.row {
    display: table-row;
}
.col {
    display: table-cell;
}

.popup-box.active {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  text-align: center;
  vertical-align: top;
  display: inherit;
}

.popup-box>.table,
.popup-box>.table>.row,
.popup-box>.table>.row>.col {
  height: 100%;
  width: 100%;
}

.popup-box>.table>.row>.col {
  vertical-align: middle;
}

.popup-box.active .popup-box-content {
  display: block;
  background-color: #272a2e;
  max-height: 90%;
  width: 90%;
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
  vertical-align: middle;
  border-radius: 7px;
  text-align: left;
  position: relative;
  min-height: 200px;
  padding:20px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
<div class="popup-box active">
  <div class="table">
    <div class="row">
      <div class="col">

          <div class="popup-box-content">

              <h1>Popup Title</h1>

              <p class="ingress">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen
                book.
              </p>

              <h2>A random second title</h2>

              <p class="text">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
                'Content here, content here', making it look like readable English.</p>

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

      </div>
    </div>

这是带有关闭按钮的那个,请看第 72 行:

* {
  margin:0;
  padding:0;
}
body {
  font-family:Roboto;
  color:#fff;
  font-size:12px;
}
body, html {
  width: 100%;
  height: 100%;
}
h1 {
  padding-bottom:10px;
}
h2 {
  padding-bottom:7px;
}
p {
  line-height:1.5em;
  padding-bottom:10px;
}
.table {
    display: table;
    width: 100%;
}
.row {
    display: table-row;
}
.col {
    display: table-cell;
}

.popup-box.active {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  text-align: center;
  vertical-align: top;
  display: inherit;
}

.popup-box>.table,
.popup-box>.table>.row,
.popup-box>.table>.row>.col {
  height: 100%;
  width: 100%;
}

.popup-box>.table>.row>.col {
  vertical-align: middle;
}

.popup-box.active .popup-box-content-wrap {
  max-height: 90%;
  width: 90%;
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
  vertical-align: middle;
  background-color: #272a2e;
  border-radius: 7px;
  position:relative;
}

.popup-box.active .popup-box-content {
  width:calc(100% - 40px);
  min-height: 100px;
  max-height: calc(100px - 40px); /* <--- Want this to be 100% instead of 100px */
  text-align: left;
  padding:20px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.popup-box.active .popup-box-close {
  text-align: center;
  width: 100%;
  height: 40px;
  font-size: 20px;
  line-height:160%;
}
<div class="popup-box active">
  <div class="table">
    <div class="row">
      <div class="col">

        <div class="popup-box-content-wrap">
          <div class="popup-box-content">

              <h1>Popup Title</h1>

              <p class="ingress">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>


            </div>
            
            <div class="popup-box-close">Close</div>
            
        </div>

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

最佳答案

试试这个:

.popup-box.active .popup-box-content-wrap {
  max-height: 90%;
  width: 90%;
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
  vertical-align: middle;
  background-color: #272a2e;
  border-top-left-radius: 7px;
  border-top-right-radius: 7px;
  position:relative;
}

.popup-box.active .popup-box-close {
    background-color: #272a2e;
    position: absolute;
    bottom: -40px;
    border-bottom-right-radius: 7px;
    border-bottom-left-radius: 7px;
  text-align: center;
  width: 100%;
  height: 40px;
  font-size: 20px;
  line-height:160%;
}

完整代码:

<div id="popup" class="popup-box active">
  <div class="table">
    <div class="row">
      <div class="col">

        <div class="popup-box-content-wrap">
          <div class="popup-box-content">

              <h1>Popup Title</h1>

              <p class="ingress">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>


            </div>

            <div class="popup-box-close">Close</div>

        </div>

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

<style>
    * {
  margin:0;
  padding:0;
}
body {
  font-family:Roboto;
  color:#fff;
  font-size:12px;
}
body, html {
  width: 100%;
  height: 100%;
}
h1 {
  padding-bottom:10px;
}
h2 {
  padding-bottom:7px;
}
p {
  line-height:1.5em;
  padding-bottom:10px;
}
.table {
    display: table;
    width: 100%;
}
.row {
    display: table-row;
}
.col {
    display: table-cell;
}

.popup-box.active {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  text-align: center;
  vertical-align: top;
  display: inherit;
}

.popup-box>.table,
.popup-box>.table>.row,
.popup-box>.table>.row>.col {
  height: 100%;
  width: 100%;
}

.popup-box>.table>.row>.col {
  vertical-align: middle;
}

.popup-box.active .popup-box-content-wrap {
  max-height: 90%;
  width: 90%;
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
  vertical-align: middle;
  background-color: #272a2e;
  border-top-left-radius: 7px;
  border-top-right-radius: 7px;
  position:relative;
}

.popup-box.active .popup-box-content {
  width:calc(100% - 40px);
  min-height: 100px;
  max-height: calc(100px - 40px); /* <--- Want this to be 100% instead of 200px */
  text-align: left;
  padding:20px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.popup-box.active .popup-box-close {
    background-color: #272a2e;
    position: absolute;
    bottom: -40px;
    border-bottom-right-radius: 7px;
    border-bottom-left-radius: 7px;
  text-align: center;
  width: 100%;
  height: 40px;
  font-size: 20px;
  line-height:160%;
}
</style>

关于html - 如何将具有最大高度的元素与垂直中心对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53861557/

相关文章:

javascript - 删除导航链接上的事件类

javascript - 如何以编程方式捕获强制更新的网页

html - 只滚动页面的一部分,同时让它们覆盖整个屏幕

css - 框阴影悬停过渡闪烁

html - 如何从右侧缩进文本?

html - 当 parent 使用背景大小 :cover 时如何定位页脚

html - 分词后,下一行向左对齐而不是居中

javascript - 使用javascript取消设置刷新按钮?

javascript - 更改元素的 HTML 后,CSS 悬停状态保留在 Safari 中

html - 在 div 中自动调整图像大小