html - 占据所有剩余空间的 div 中的绝对定位 div

标签 html css layout flexbox

我正在尝试制作一个看似简单的布局。尽管看了很多例子,我还是无法破解它。

SideBar| .......MapContainer......
SideBar| ..........Map............
SideBar| .......MapContainer......

SideBar 和 MapContainer 都应该是 100% 高度。

棘手的一点: map 必须有定义的高度和宽度,因为 mapbox-gl-js 库使用它的尺寸来填充它。 (而不是添加内容然后调整它的大小)。

MapContainer 的存在是因为其中会有其他绝对定位的叠加元素。

我试图避免将边栏宽度编码到 MapContainer 的定义中,这样我就可以在 JS 中隐藏/显示边栏,并让 MapContainer 自动填充空间。

这变得非常非常接近:

* {
  box-sizing: border-box;
}

.sidebar, .mapcontainer, .container {   
  height: 200px; 
}

.container {
  width:100%;
  border:1px solid;
  display: flex
}
.sidebar {
    width:200px;
    background:lightblue;
}
.mapcontainer {
  width:auto;
  background:lightgreen;
  position:relative;
  flex-grow: 1;
}

.map {
  width: 100%;
  height: 100%;
  position:absolute;  
  border: 20px dashed grey;
  
}
<div class="container">
    <div class="sidebar"></div>
    <div class="mapcontainer">
      <div class="map">

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

但是一旦我将“高度:200px”更改为“高度:100%”,它就会崩溃。我需要做什么?

最佳答案

.sidebar, .mapcontainer, .container 规则中使用视口(viewport)单位 vh

* {
  box-sizing: border-box;
}
html, body {
  margin: 0;
}
.sidebar, .mapcontainer, .container {   
  height: 100vh; 
}
.container {
  border: 1px solid;
  display: flex
}
.sidebar {
    width: 200px;
    background:lightblue;
}
.mapcontainer {
  background:lightgreen;
  position:relative;
  flex-grow: 1;
}
.map {
  width: 100%;
  height: 100%;
  position:absolute;  
  border: 20px dashed grey;  
}
<div class="container">
    <div class="sidebar"></div>
    <div class="mapcontainer">
      <div class="map">

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

关于html - 占据所有剩余空间的 div 中的绝对定位 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42238513/

相关文章:

javascript - 动态图像显示 - javascript 错误的 url 读取

javascript - 无法在 Angular 上删除

html - 为什么弹出窗口的某些部分被切断

c# - Windows 窗体应用程序中的 Dock、Anchor 和 Fluid 布局

php - xpath表达式的结果是 : [object Text]. 应该是一个元素

javascript - HTML 选择框有关显示/隐藏 div 的 JQuery 问题

html - 将导航栏无序列表与 flexbox 对齐

html - 文本使我的 div 变形

layout - 在 Express/EJS 中,如何更改默认布局?

ruby-on-rails - 未定义的方法 `layout'