如何使.col2
具有灵活性,以便填补.col1
和.col3
之间的空间。我尝试了百分比,但它不起作用。实现这一目标的最佳方法是什么?感谢。
body{
margin:0;
}
.header{
width:100%;
background:gray;
height:120px;
margin:0 auto;
}
.MainController{
width:100%;
clear:both;
}
.col1{
height:679px;
background:yellow;
width:150px;
float:left;
}
.col2{
height:679px;
background:black;
float:left;
width:20%;
}
.col3{
height:679px;
background:red;
width:300px;
float:right;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="header"></div>
<div class="MainController">
<div class="col1"></div>
<div class="col2"></div>
<div class="col3"></div>
</div>
</body>
</html>
答案 0 :(得分:3)
您可以使用Flexbox
布局。将display: flex
应用于父级并将flex: 1
添加到.col2
以使其动态扩展。
body {
margin: 0;
}
.header {
width: 100%;
background: gray;
height: 120px;
margin: 0 auto;
}
.MainController {
width: 100%;
clear: both;
display: flex;
}
.col1 {
height: 679px;
background: yellow;
width: 150px;
}
.col2 {
flex: 1;
background: black;
width: 20%;
}
.col3 {
background: red;
width: 300px;
}
&#13;
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="header"></div>
<div class="MainController">
<div class="col1"></div>
<div class="col2"></div>
<div class="col3"></div>
</div>
</body>
</html>
&#13;
答案 1 :(得分:3)
calc
:您可以使用calc
计算宽度。 .col2
将是宽度的100%减去两列的宽度。所以在这种情况下:
calc: (100% - 450px); /*150px + 300px = 450px*/
您的代码变为:
body{
margin:0;
}
.header{
width:100%;
background:gray;
height:120px;
margin:0 auto;
}
.MainController{
width:100%;
clear:both;
}
.col1{
height:679px;
background:yellow;
width:150px;
float:left;
}
.col2{
height:679px;
background:black;
float:left;
width:calc(100% - 450px);
}
.col3{
height:679px;
background:red;
width:300px;
float:right;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="header"></div>
<div class="MainController">
<div class="col1"></div>
<div class="col2"></div>
<div class="col3"></div>
</div>
</body>
</html>
flexbox
:您也可以像Aziz建议的那样使用flexbox
。
只需将.col2
设为flex:1
,将.MainController
设为display:flex
即可。像这样:
body{
margin:0;
}
.header{
width:100%;
background:gray;
height:120px;
margin:0 auto;
}
.MainController{
width:100%;
clear:both;
display: flex;
}
.col1{
height:679px;
background:yellow;
width:150px;
float:left;
}
.col2{
height:679px;
background:black;
float:left;
flex: 1;
}
.col3{
height:679px;
background:red;
width:300px;
float:right;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="header"></div>
<div class="MainController">
<div class="col1"></div>
<div class="col2"></div>
<div class="col3"></div>
</div>
</body>
</html>
这两种方法都受到广泛支持,但您可能仍希望在此处检查浏览器支持。