我正在尝试通过以下方式实现响应式布局:- 固定标题宽度宽度为100%,高度为例如50px- 右侧有 3 个相等的方块,占据了从页面顶部到底部的整个空间。- 主要内容正在占用页面上的剩余空间
目前我的代码看起来像这样(jsfiddle),但我无法根据当前高度自动设置右侧框的宽度以显示为正方形......有没有人知道纯 CSS 中的解决方案?
.HTML:
<div id="mainView">
<div id="content">
</div><!-- content -->
<div id="squaressWrapper">
<div id="square1"></div><!-- dummy -->
<div id="square2"></div><!-- dummy -->
<div id="square3"></div><!-- dummy -->
<div id="square4"></div><!-- dummy -->
</div><!-- squaressWrapper -->
</div><!-- mainView -->
</div><!-- wrapper -->
.CSS: HTML { 高度: 100%; 边距: 0; 填充: 0; }
body {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px;
}
#wrapper {
width: 100%;
height: 100%;
display: flex;
display: -webkit-flex;
flex-direction: column;
-webkit-flex-direction: column;
-webkit-align-content: stretch;
align-content: stretch;
}
#header {
height: 50px;
width: 100%;
background: #ffb8c4;
}
#mainView {
flex: 1;
-webkit-flex: 1;
position: relative;
background: #666;
}
#squaressWrapper {
position: absolute;
height: 100%;
background: green;
right: 0;
display: flex;
display: -webkit-flex;
flex-direction: column;
-webkit-flex-direction: column;
-webkit-align-content: stretch;
align-content: stretch;
}
#square1 {
position: relative;
flex: 1;
-webkit-flex: 1;
width: 100px;
border: 2px solid white;
background: green;
}
#square2 {
position: relative;
flex: 1;
-webkit-flex: 1;
width: 100px;
border: 2px solid white;
background: green;
}
#square3 {
position: relative;
flex: 1;
-webkit-flex: 1;
width: 100px;
border: 2px solid white;
background: green;
}
我得到了一个解决方案,按照Nicho的建议使用vh单位。
CSS的
#squaressWrapper {
position: absolute;
height: 100%;
width: 33vh;
right: 0;
display: flex;
display: -webkit-flex;
flex-direction: column;
-webkit-flex-direction: column;
-webkit-align-content: stretch;
align-content: stretch;
}
.squares {
position: relative;
flex: 1;
-webkit-flex: 1;
width: calc(100% - 15px);
border: 2px solid white;
background: green;
right: -11px;
}
演示
尺寸有点奇怪,因为将容器的宽度设置为 calc(33vh - 15px) 不起作用。
可能在不久的将来会更容易。
我不知道浏览器对此的支持是什么,我只在 Chrome 中对其进行了测试。
注意:15px是标题的尺寸(45px)除以正方形数。
好吧,我试了一下...我在 css 上更改了您的大小
这是小提琴的链接。
http://jsfiddle.net/D7RSP/2/
#header {
height: 20%;
width: 100%;
background: #ffb8c4;
}
#mainView {
flex: 1;
width: 69%;
height: 100%;
float: left;
background: green;
}
.square {
flex: 1;
-webkit-flex: 1;
width: 30%;
border: 1px solid white;
background: green;
height: 25%;
float:right;
}