CSS 左侧固定宽度 div,右侧填充剩余宽度 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12724967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fixed width div on left, fill remaining width div on right
提问by Anamaria Miehs
I want a div with a fixed width image on the left and a variable width div with a background color, which should extend its width 100% on my device. I can't stop the second div from overflowing my fixed div.
我想要一个左侧有固定宽度图像的 div 和一个带有背景颜色的可变宽度 div,它应该在我的设备上将其宽度扩展 100%。我无法阻止第二个 div 溢出我的固定 div。
When I add overflow:hidden at the variable width div it just jumps under the photo, on the next row.
当我添加 overflow:hidden 在可变宽度 div 时,它只是跳到下一行的照片下方。
How can I fix this the right way (i.e. without hacks or margin-left, since I need to make the site responsive later with media queries and I have to change the image with other resolution images for each device)?
我怎样才能以正确的方式解决这个问题(即没有黑客攻击或左边距,因为我需要稍后通过媒体查询使网站响应,并且我必须为每个设备更改具有其他分辨率图像的图像)?
- beginner web designer trying to tackle the horror of responsive websites -
- 初学者网页设计师试图解决响应式网站的恐怖问题 -
HTML:
HTML:
<div class="header"></div>
<div class="header-right"></div>
CSS:
CSS:
.header{
float:left;
background-image: url('img/header.png');
background-repeat: no-repeat;
width: 240px;
height: 100px;
}
.header-right{
float:left;
overflow:hidden;
background-color:#000;
width: 100%;
height: 100px;
}
回答by caitriona
Try removing the float:left
and width:100%
from .header-right
— the right div
then behaves as requested.
尝试删除float:left
and width:100%
from .header-right
-div
然后右侧会按要求运行。
.header {
float: left;
background: #efefef;
background-repeat: no-repeat;
width: 240px;
height: 100px;
}
.header-right {
overflow: hidden;
background-color: #000;
height: 100px;
}
<div class="header"></div>
<div class="header-right"></div>
回答by Zuber
with the use of css grid
you can achieve this more easily
使用css grid
您可以更轻松地实现这一目标
- you need to wrap those divs in a wrapper, lets say
parent
- give
.parent
display: grid
- give
grid-template-areas
in.parent
andgrid-area
in bothchildren
- no need to use
float: left
in bothchildren
adjust the width of left div usinggrid-template-columns
in.parent
- 你需要将这些 div 包装在一个包装器中,让我们说
parent
- 给
.parent
display: grid
- 给
grid-template-areas
在.parent
和grid-area
两个children
- 无需使用
float: left
两个children
利用调整左div的宽度grid-template-columns
在.parent
.parent {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr;
grid-template-areas: "header-left header-right";
}
.header {
background-image: url(img/header.png);
background-color: #ebebeb;
background-repeat: no-repeat;
height: 100px;
grid-area: header-left;
}
.header-right {
overflow: hidden;
background-color: #000;
width: 100%;
height: 100px;
grid-area: header-right;
}
<div class="parent">
<div class="header"></div>
<div class="header-right"></div>
</div>
css has become much more advanced and answering with the help of that advanced css just if it can be useful to someone :)
css 已经变得更加先进,并且在该高级 css 的帮助下进行回答,只要它对某人有用:)