css - 动态设置div宽度和高度的方法

标签 css

场景 1:
顶部标题宽度为 100%,高度为 50 像素。
左侧导航栏宽度为 200 像素,高度为动态填充屏幕。

body{
    padding: 0;
    margin: 0;
}

.header{
    width: 100%;
    height: 50px;
}

.navBar{
    position: absolute;
    top: 50px;
    bottom: 0;
}

场景 2:
顶部标题宽度为 100%,高度为 50 像素。
左侧导航栏宽度为 200px,高度为动态填充屏幕。
导航栏右侧和标题下方的容器,具有动态宽度和高度以填充屏幕。

.container{
    position: absolute;
    top: 50px;
    bottom: 0;
    left: 200px;
    right: 0;
}

这会起作用,但我知道这不是实现它的最佳方法。一些建议?非常感谢!

最佳答案

您必须将 height:100% 添加到 htmlbody 标签。 然后对 heightwidth 属性使用 calc() css 函数来获得结果。

请参阅我的片段以了解这两个场景。它的工作。

html, body{ height:100%;}
header{ width:100%; height:50px;background:#777;}
.nav{ height: calc( 100% - 50px ); width:200px; float:left; background:#888;}
.container{ color:#fff; float:left; width:calc( 100% - 200px ); height:calc( 100% - 50px ); background:#222;}
<header>HEADER</header>  
<div class="nav">NAV</div>
<div class="container">Container</div>              

关于css - 动态设置div宽度和高度的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27014687/

相关文章:

jquery - 用 css 或 jquery 重写 !important

jquery - IE7 : Absolutely positioned Child Div with a negative CSS value for 'right'

css - 连续第 n 个 child (多次出现)

javascript - 如何将文本对齐到绝对定位的列表元素的中心?

html - 避免为 html 设置全局类

html - 下拉菜单隐藏在 div 后面

javascript - 使用变换 : scale is not appropriately hiding a div

jquery - 如何在不使用大图像或小图像/单图像的情况下使用 Elevatezoom 悬停缩放图像

html - 创建具有固定标题和可变列宽的表格的最简单方法是什么?

javascript - 日程应用程序上的无限时间轴