【问题标题】:HTML -> Fixed top, Flexible bottom with min-height and dynamic squared contentHTML -> 固定顶部,灵活底部,最小高度和动态平方内容
【发布时间】:2021-05-19 17:19:22
【问题描述】:

我真的很难像这样创建 css 布局:

顶行:固定大小:例如:50px;

内容:当前宽度可以容纳的最大正方形。所以这个宽度=高度。它应该尊重底行最小高度。

底行:占用所有剩余空间,并具有最小高度。例如:50 像素。

没有滚动条。我们的想法是尽可能以最佳方式使用当前屏幕以适应任何分辨率。除非只有 js 才有可能,否则没有 javascript。

有什么想法吗?

这是我目前为止最好的:

   <div class="shell">
      <div class="header"></div>
      <div class="square"></div>
      <div class="footer"></div>
    </div>

css

body {
  margin: 0px;
}
.shell {
  background-color: #000000;
  height: 100vh;
  max-width: calc(100vh - 100px);
  overflow: hidden;
  margin-left: auto;
  margin-right: auto;
}

.header {
  background-color: #0000ff;
  height: 50px;
}

.square {
  width: 100%;
  background-color: #dc143c;
}

.footer {
  background-color: #00008b;
  height: 100vh;
}

【问题讨论】:

  • 我已经尝试了数百万种组合。我应该全部发布吗?曾经有一段时间,S.O.是关于帮助人们:(

标签: html css


【解决方案1】:

您可以使用填充来获取纵横比:

body {
  margin: 0px;
}
.shell {
  background-color: #000000;
  height: 100vh;
  max-width: calc(100vh - 100px);
  overflow: hidden;
  margin-left: auto;
  margin-right: auto;
}

.header {
  background-color: blue;
  height: 50px;
}

.square {
  width: 100%;
  padding-top: 100%;
  background-color: green;
}

.footer {
  background-color: red;
  height: 100%;
}
<div class="shell">
  <div class="header"></div>
  <div class="square"></div>
  <div class="footer"></div>
</div>

参考here

【讨论】:

  • 页脚大小不是剩余空间的 100%,而是整个容器的 100%,但滚动不可见,因为您将其隐藏了 overflow: hidden
【解决方案2】:

我认为您的问题已经在这里解决了: Make a div fill the height of the remaining screen space

结合你的尝试:

body {
  margin: 0;
}

.box {
  display: flex;
  flex-flow: column;
  background-color: #000000;
  height: 100vh;
  max-width: calc(100vh - 100px);
  overflow: hidden;
  margin-left: auto;
  margin-right: auto;
}


.box .row.header {
  flex: 0 1 50px;
  background-color: #0000ff;
}

.box .row.content {
  flex: 1 1 auto;
  width: 100%;
  background-color: #dc143c;
}

.box .row.footer {
  flex: 0 1 40px;
  background-color: #00008b;
}
<div class="box">
  <div class="row header">
  </div>
  <div class="row content">
  </div>
  <div class="row footer">
  </div>
</div>

小提琴:https://jsfiddle.net/901s2kdL/

【讨论】:

  • 宽度太小不起作用。如果发生这种情况,它就不再是正方形了。
【解决方案3】:

内容:当前宽度可以容纳的最大正方形。所以宽度= 这个高度。它应该尊重底行最小高度。

如果你想要最大的squarefooterheight 将被固定,它总是等于min-height(应该是),所以你是否设置它并不重要height100%50pxmax-width 的正方形确定实际尺寸。如果你看这个max-width: calc(100vh - 100px)- 100px 部分是真正的剩余空间,包括headerfooter,所以如果header height 设置为50pxfooter @987654337 @ 也是50px

body {
  margin: 0px;
}

.shell {
  background-color: black;
}

.header {
  height: 50px;
  background-color: red;
}

.square {
  max-width: calc(100vh - 100px);
  margin: 0 auto;
  background-color: green;
}

.square:after {
  content: "";
  display: block;
  padding-bottom: 100%;
}

.footer {
  height: 50px;
  background-color: blue;
}
<div class="shell">
    <div class="header"></div>
    <div class="square"></div>
    <div class="footer"></div>
</div>

【讨论】:

  • 它仍然是一个正方形,但底部有一个固定的高度,而不是占用所有剩余的可用空间。
  • 我已经更新了帖子,解释为什么100%50px无关紧要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
相关资源
最近更新 更多