【问题标题】:Tiling the page with CSS使用 CSS 平铺页面
【发布时间】:2017-04-05 01:45:42
【问题描述】:

我也想以递归方式水平和垂直地平铺我的页面。例如。该页面分为三列,其中第三列分为两行。

<div id="left/>
<div id="right">
    <div id="top">...</div>
    <div id="bottom">...</div>
</div>

这个小提琴解释了我在说什么: https://jsfiddle.net/zuyu1ed7/

此设计有效,直到您开始添加内容。为其中一个容器设置边框、边距或填充会导致混乱。整个布局看起来很合情合理。

此外,尝试在这些容器中添加 &lt;div style="width:100%; padding: 10px;"/&gt; 具有相同的布局破坏效果。这种情况超出了我对 css 的了解。

起初我想用表格,如果 css 一直在嘲笑你,你最好的朋友。但是后来我看到html5不再支持大多数colgroup-attributes。所以这个想法是死路一条。

我的问题是:如何像小提琴中所示水平和垂直拆分 div 并能够向容器添加内容?
我很感谢任何解决方案,但我当然更喜欢适用于所有浏览器的解决方案。

【问题讨论】:

标签: html css layout


【解决方案1】:

你考虑过flexbox吗? 基本支持by all modern browsers

Fiddle

html, body {
  width: 100%;
  height: 100%;
}

* {
  margin: 0px;
  padding: 0px;
}

body {
  display: flex;
}

#left {
  height: 100%;
  width: 20%;
  background-color: Blue;
}

#right {
  height: 100%;
  width: 80%;
  background-color: Green;
  display: flex;
  flex-direction: column;
}

#top {
  width: 100%;
  height: 33.33%;
  background-color: lightblue;
}

#center {
  width: 100%;
  height: 33.33%;
  background-color: lightgreen;
}

#bottom {
  width: 100%;
  height: 33.34%;
  background-color: black;
  display: flex;
}

#bottom-left {
  height: 100%;
  width: 35%;
  background-color: Yellow;
}

#bottom-right {
  height: 100%;
  width: 65%;
  background-color: orange;
}
<div id="left">
  </div>
  <div id="right">
      <div id="top"></div>
      <div id="center"></div>
      <div id="bottom">
          <div id="bottom-left"></div>
          <div id="bottom-right"></div>
      </div>
  </div>

【讨论】:

  • 感谢您的回答。我认为 flexbox 可能是实现我想要的最佳方式。
  • PS:小提琴链接与您答案中的代码不匹配,我创建了一个编辑。
  • 不客气 :) 代码很奇怪,我直接从小提琴中复制了它。嗯。
猜你喜欢
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多