I have three div elements: left, middle and right. Left and right are fixed and floating. What I want is the middle div to fill the gap in between them.
我有三个div元素:左、中、右。左右是固定和浮动的。我想要的是中间的div来填补它们之间的空白。
This is my code:
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<style>
* {border: dotted 1px red;}
#left {
width: 200px;
float: left;
}
#middle {
float: left;
}
#right {
width: 200px;
float: right;
}
</style>
</head>
<body>
<div id="left" > left </div>
<div id="middle"> middle </div>
<div id="right" > right </div>
</body>
</html>
Any ideas on how to do this? I tried different solutions but didn't manage to do what I want.
有什么办法吗?我尝试了不同的解决方案,但没有做到我想要的。
2 个解决方案
#1
71
The key is to restructure your html to have middle
last, remove the float
from the middle
and replace it with overflow: hidden
.
关键是将html结构调整为中继,从中间删除浮动,并用overflow: hidden替换它。
视图小提琴的例子。
HTML
HTML
<div id="left" > left </div>
<div id="right" > right </div>
<div id="middle"> middle </div>
CSS
CSS
#left {
width: 200px;
float: left;
}
#middle {
overflow: hidden;
}
#right {
width: 200px;
float: right;
}
#2
1
I was able to replicate the issue and fix it using percentages instead of absolute values. Since you are looking for something flexible between the two elements this should work.
我能够复制这个问题并使用百分比而不是绝对值来修复它。由于您正在寻找两个元素之间的灵活性,所以这应该是可行的。
#left {
width: 20%;
float: left;
background: #ccc;
}
#middle {
width: 60%;
float: left;
display: block;
background: #ddd;
}
#right {
width: 20%;
float: right;
background: #bbb;
}
Here's a demo
这是一个演示
#1
71
The key is to restructure your html to have middle
last, remove the float
from the middle
and replace it with overflow: hidden
.
关键是将html结构调整为中继,从中间删除浮动,并用overflow: hidden替换它。
视图小提琴的例子。
HTML
HTML
<div id="left" > left </div>
<div id="right" > right </div>
<div id="middle"> middle </div>
CSS
CSS
#left {
width: 200px;
float: left;
}
#middle {
overflow: hidden;
}
#right {
width: 200px;
float: right;
}
#2
1
I was able to replicate the issue and fix it using percentages instead of absolute values. Since you are looking for something flexible between the two elements this should work.
我能够复制这个问题并使用百分比而不是绝对值来修复它。由于您正在寻找两个元素之间的灵活性,所以这应该是可行的。
#left {
width: 20%;
float: left;
background: #ccc;
}
#middle {
width: 60%;
float: left;
display: block;
background: #ddd;
}
#right {
width: 20%;
float: right;
background: #bbb;
}
Here's a demo
这是一个演示