html - 具有响应式中央列的 3 列布局

标签 html css responsive-design

我正在尝试构建响应式三列布局。但是,我希望外列的宽度固定,并且我希望中央列占据所有剩余空间。

因此,如果屏幕宽度为 900 像素,而我的外部列分别为 200 像素和 300 像素,则中间列将占用剩余的 400 像素。

到目前为止我有

.column-left,
.column-main,
.column-right {
    display: inline-block;
    vertical-align: top;    
    height: 100%;
}
.column-left {
    width: 200px;
}
.column-main {
    width: 100%;
}
.column-right {
    width: 300px;
    float: right;
}

但这会将中间列向下推到下一行,第三列也将向下推到另一行。

最佳答案

选项 1:表格布局

为什么不使用 CSS 表格布局?注意。这在这里作为布局是有效的,而不是用于数据内容的语义 table 元素。

html,
body,
.table {
  height: 100%;
  width: 100%;
  margin: 0;
}
.table {
  display: table;
  table-layout: fixed;
}
.cell {
  display: table-cell;
  background:lightgrey;
}
.cell:first-child,
.cell:last-child {
  width: 200px;
  background: grey;
}
<div class="table">
  <div class="cell"></div>

  <div class="cell"></div>

  <div class="cell"></div>

</div>

选项 2:定位布局

或者,您可以定位您的元素...

html,
body,
div {
  height: 100%;
  width: 100%;
  margin: 0;
}
div {
  position: absolute;
  top: 0;
  left: 200px;
  bottom: 0;
  right: 200px;
  width: calc(100% - 400px);
  background:lightgrey;
}
div:first-of-type,
div:last-of-type {
  background: grey;
  width: 200px;
  position: absolute;
}
div:first-of-type {
  left: 0;
  right: auto;
}
div:last-of-type {
  left: auto;
  right: 0;
}
<div></div>
<div></div>
<div></div>

关于html - 具有响应式中央列的 3 列布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815647/

相关文章:

javascript - 将单词中的第一个字母大写表示正常状态

html - CSS 响应式等高列,每个 div 下都有按钮正确包装

javascript - 如何为所有实例设置相同的子组件动态宽度?网络组件

javascript - 从 firebase 向我的网站显示图像的问题如何解决?

html - 甚至独立于它们的类型选择一个类的元素

css - 为什么我的响应式网页上有水平滚动条

css - 流体网格 - 如何在响应式网页设计中使用它们?

php - 如何使用codeigniter从 Controller 为 View 的标签赋值

css - 修复响应式网页中的 DIV 高度

javascript - Coldfusion 中数据表的无限滚动