首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >固定的Div和宽度灵活的div相邻。

固定的Div和宽度灵活的div相邻。
EN

Stack Overflow用户
提问于 2015-11-02 14:24:35
回答 2查看 10.6K关注 0票数 2

在一个有2%左右填充的容器中,我有两个div盒子。左div框的固定宽度为200 box,固定的边距为60 box。我希望右div调整其宽度越小/越大的浏览器窗口得到。如何实现红色框的宽度总是(独立于浏览器宽度)填满,直到容器的严格填充开始,而蓝色div保持其200 box?

JSFIDDLE:http://jsfiddle.net/3vhrst19/3/

HTML:

代码语言:javascript
复制
<div id="container">
    <div id="fixed-width"></div>

    <div id="flexible-width"></div>
</div>

CSS:

代码语言:javascript
复制
#container {
    float: left;
    width: 100%;
    padding: 50px;
    background: lightgrey;
}

#fixed-width {
    float: left;
    width: 200px;
    height: 500px;
    margin-right: 60px;
    background: blue;
}

#flexible-width {
    float: left;
    width: 500px; /* my goal is that the width always fills up independent of browser width */
    height: 500px;
    background: red;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-02 14:43:19

这在flexbox中是容易实现的

代码语言:javascript
复制
#container {
  display: flex;
  width: 100%;
  padding: 50px;
  background: lightgrey;
  box-sizing: border-box; /* used so the padding will be inline and not extend the 100% width */
}

其中,响应元素用flex-grow填充剩余的空间。

代码语言:javascript
复制
#flexible-width {
  flex: 1; /* my goal is that the width always fills up independent of browser width */
  height: 500px;
  background: red;
}

注意,我删除了所有的floats,因为在这个示例中它不是必需的。

JSFiddle

票数 4
EN

Stack Overflow用户

发布于 2015-11-02 14:30:16

使用calc从100%宽度中移除固定宽度和边距宽度

代码语言:javascript
复制
#container {
  float: left;
  width: 100%;
  padding: 50px;
  background: lightgrey;
}
#fixed-width {
  float: left;
  width: 200px;
  height: 500px;
  margin-right: 60px;
  background: blue;
}
#flexible-width {
  float: left;
  max-width: 500px;
  /* my goal is that the width always fills up independent of browser width */
  width: calc(100% - 260px); /* Use calc to remove the fixed width and margin width from the 100% width */
  height: 500px;
  background: red;
}
代码语言:javascript
复制
<div id="container">
  <div id="fixed-width"></div>

  <div id="flexible-width"></div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33479841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档