【问题标题】:CSS block layout full screenCSS块布局全屏
【发布时间】:2015-05-19 22:50:32
【问题描述】:

想制作一个大致如下的屏幕布局:

整个浏览器窗口应该填充“已知”高度的唯一两个元素,即顶部、左侧和底部、左侧的白色块(对于两个标签,因此已知将与字体相关)。其他所有内容都应随浏览器窗口缩放,即左侧栏为 15% 宽,右侧为 85%,等等。

作为一名 C++ 开发人员,我的直觉是处理 Javascript 中的事件和针对 DOM 的代码,但我感觉这对于 CSS 来说是相对微不足道的。

谁能帮帮我?

【问题讨论】:

  • 你尝试过什么样的代码?

标签: css layout


【解决方案1】:

我试图快速复制:

* {
  box-sizing: border-box;
}
html,
body,
.wrapper {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
.left,
.right {
  float: left;
}
.left {
  position: relative;
  width: 15%;
  height: 100%;
}
.left .label-top,
.left .label-bottom {
  position: absolute;
  width: 100%;
  background-color: #fff;
}
.left .label-top {
  top: 0;
  left: 0;
}
.left .label-bottom {
  bottom: 0;
  left: 0;
}
.left .content,
.left .top,
.left .bottom {
  border: 1px solid white;
}
.left .top,
.left .bottom {
  height: 5%;
  background-color: gray;
}
.left .content {
  height: 30%;
  background-color: #a09898;
}
.right {
  width: 85%;
  height: 100%;
  background-color: gray;
}
.right::after {
  content: '';
  display: table;
  clear: both;
}
<div class="wrapper">
  <div class="left">
    <div class="label-top">Label</div>
    <div class="top"></div>
    <div class="content"></div>
    <div class="content"></div>
    <div class="content"></div>
    <div class="bottom"></div>
    <div class="label-bottom">Label</div>
  </div>
  <div class="right"></div>
</div>

【讨论】:

  • 这很好用,但在我的 iPad 上它不够高。即您必须向下滑动屏幕才能看到“底部”和“标签底部”。在我的 PC 上的 Chrome 中,这很好。编辑:不,似乎又可以工作了。 Safari 是多么神秘。
【解决方案2】:

因此,您可以在此基础上开始工作。

作为固定高度,我使用了vh,这实际上取决于您要支持的浏览器:vh support

否则,您可以使用父级或正文的height: 100%

.left-bar {
    width: 15%;
    background-color: red;
    float: left;
    height: 100vh;
    border-right: 5px solid black;
}

.right-window {
    width: 85%;
    float: left;
    height: 100vh;
    background-color: pink;
    
}
* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
<div class="left-bar">
</div>
<div class="right-window">
</div>

我想这就是你想要的。不过不确定。

这样就实现了完整的浏览器填充。

如果您注意到宽度有 calc() 从边框移除 5px,如果您愿意,可以将其移除并仅放置 15%

我认为你只想要一个基本结构,这是一个非常简单的结构,你一定会喜欢我的颜色挑选技巧。

编辑:通过添加box-sizing: border-box 替换calc(),感谢@Paulie_D 评论。

【讨论】:

  • vh 与 % (我一直在尝试但失败了)不同,大概意思是“垂直高度”。是可见窗口的垂直高度?
  • 这是viewport height,非常自我解释:)
  • 作为初学者,在 CSS 中“自我解释”并不多。
  • 如果您使用box-sizing:border-box 在宽度/高度值中包含边框和填充,您可能会丢失calc。 - jsfiddle.net/krugpmjL/1
  • @Paulie_D 是的,但我试图“简化”它。可能应该添加box-sizing。在编辑中替换了calc
【解决方案3】:

对于布局,请考虑使用定位和显示属性。有许多方法可以创建动态结构,以确保响应能力。

有关更多详细信息,请参阅this question and answer,了解您在创建网站时可能会考虑的一些“一般”规则。

.left {
  position: absolute;
  lefT: 0;
  top: 0;
  height: 100%;
  width: 15%;
  background: lightgray;
}
.right {
  position: absolute;
  left: 15%;
  top: 0;
  height: 100%;
  width: 85%;
  background: dimgray;
}
.left .fixedBlock {
  margin: 0;
  padding: 0;
  height: 50px;
  background: blue;
}
.left .filledDiv {
  height: calc(100% - 100px);
  background: tomato;
}
.left .filledDiv .third {
  height: 33.33%;
  background: rgba(0, 0, 0, 0.2);
}
.left .filledDiv .third:nth-child(2) {
  background: rgba(0, 0, 0, 0.4);
}
<div class="left">
  <div class="fixedBlock">fixed height</div>
  <div class="filledDiv">
    <div class="third">dynamic height</div>
    <div class="third">with children</div>
    <div class="third">a third of its heigth</div>
  </div>
  <div class="fixedBlock">also fixed height</div>

</div>
<div class="right">right side - open me in full screen!</div>

【讨论】:

  • 我希望你能@Robinson。几乎所有你“风格”的东西都可以/应该在 css 中完成(高度/显示/定位/形状)。对于文本/输入处理的操作 - 这就是 JS 和背后的代码发挥作用的地方:)
猜你喜欢
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多