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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:23:46  来源:igfitidea点击:

Fixed width div on left, fill remaining width div on right

csscss-floatoverflowfixed-widthvariable-width

提问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:leftand width:100%from .header-right— the right divthen behaves as requested.

尝试删除float:leftand 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 gridyou can achieve this more easily

使用css grid您可以更轻松地实现这一目标

  • you need to wrap those divs in a wrapper, lets say parent
  • give .parentdisplay: grid
  • give grid-template-areasin .parentand grid-areain both children
  • no need to use float: leftin both childrenadjust the width of left div using grid-template-columnsin .parent
  • 你需要将这些 div 包装在一个包装器中,让我们说 parent
  • .parentdisplay: grid
  • grid-template-areas.parentgrid-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 的帮助下进行回答,只要它对某人有用:)