如何"向左漂浮"两个div,使得包含其他div的第二个div正确调整大小

时间:2017-05-22 15:45:37

标签: html css

我在页面上有两个div。第二个包含其他div。我希望第二个div在屏幕变窄时重新塑造。但相反,整个容器移动到前一个div之下。这个小提琴应该澄清: https://jsfiddle.net/gf6zq3pk/7/。我希望绿色div保持在适当位置,因为屏幕宽度会改变,白色框会相应地浮动。



.container {
  display: inline-block;
  float: left;
  border: solid;
  margin-left: 10px;
  overflow: auto;
  max-width: 550px;
  width: auto;
  background-color: green;
}

.menu {
  display: inline-block;
  float: left;
  border: dashed;
  border-color: blue;
  text-align: left;
  padding: 10px;
}

.box {
  display: inline-block;
  float: left;
  margin: 20px;
  padding: 10px;
  width: 70px;
  height: 50px;
  border: solid;
  background-color: white;
}

<div class="menu">Menu
  <ul>
    <li>Link A</li>
    <li>Link B</li>
  </ul>
</div>
<div class="container">
  <div class="box"> Text1 </div>
  <div class="box"> Text2 </div>
  <div class="box"> Text3 </div>
  <div class="box"> Text4 </div>
  <div class="box"> Text5 </div>
  <div class="box"> Text6 </div>
  <div class="box"> Text7 </div>
  <div class="box"> Text8 </div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

如果您对flexbox开放,可以添加包装容器并删除浮动属性......

@charset "utf-8";
.wrapper {
  display: flex;
}

.container {
  border: solid;
  margin-left: 10px;
  overflow: auto;
  max-width: 550px;
  width: auto;
  background-color: green;
}

.menu {
  display: inline-block;
  border: dashed;
  border-color: blue;
  text-align: left;
  padding: 10px;
  flex: 1 0 100px;
  height: 90px;
  /* control the height of the menu here */
}

.box {
  display: inline-block;
  float: left;
  margin: 20px;
  padding: 10px;
  width: 70px;
  height: 50px;
  border: solid;
  background-color: white;
}
<div class="wrapper">
  <div class="menu">Menu
    <ul>
      <li>Link A</li>
      <li>Link B</li>
    </ul>
  </div>
  <div class="container">
    <div class="box"> Text1 </div>
    <div class="box"> Text2 </div>
    <div class="box"> Text3 </div>
    <div class="box"> Text4 </div>
    <div class="box"> Text5 </div>
    <div class="box"> Text6 </div>
    <div class="box"> Text7 </div>
    <div class="box"> Text8 </div>
  </div>
</div>

答案 1 :(得分:1)

我会使用flexbox并将左侧菜单嵌套在一个元素中,因此它不会&#34;拉伸&#34;高度,将根据内容自动高度。我也会把白盒子容器做成柔性布局。

&#13;
&#13;
@charset "utf-8";
.container {
  border: solid;
  margin-left: 10px;
  overflow: auto;
  flex: 1 0 0;
  max-width: 550px;
  background-color: green;
  display: flex;
  flex-wrap: wrap;
}

.menu {
  border: dashed;
  border-color: blue;
  text-align: left;
  padding: 10px;
}

.box {
  margin: 20px;
  padding: 10px;
  width: 70px;
  height: 50px;
  border: solid;
  background-color: white;
}

.flex {
  display: flex;
}
&#13;
<div class="flex">
  <aside>
    <div class="menu">Menu
      <ul>
        <li>Link A</li>
        <li>Link B</li>
      </ul>
    </div>
  </aside>
  <div class="container">
    <div class="box"> Text1 </div>
    <div class="box"> Text2 </div>
    <div class="box"> Text3 </div>
    <div class="box"> Text4 </div>
    <div class="box"> Text5 </div>
    <div class="box"> Text6 </div>
    <div class="box"> Text7 </div>
    <div class="box"> Text8 </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

如果您还不想使用flexbox,请尝试此操作。 只需使用position: absolute;而不是浮动到两个容器。

@charset "utf-8";
.container {
  display: inline-block;
  position: absolute;
  border: solid;
  margin: 0 10px 10px 125px;
  overflow: auto;
  max-width: 550px;
  width: auto;
  background-color: green;
}

.menu {
  display: inline-block;
  position: absolute;
  border: dashed;
  border-color: blue;
  text-align: left;
  padding: 10px;
}

.box {
  display: inline-block;
  float: left;
  margin: 20px;
  padding: 10px;
  width: 70px;
  height: 50px;
  border: solid;
  background-color: white;
}
<div class="menu">
  Menu
  <ul>
    <li>Link A</li>
    <li>Link B</li>
  </ul>
</div>
<div class="container">
  <div class="box"> Text1 </div>
  <div class="box"> Text2 </div>
  <div class="box"> Text3 </div>
  <div class="box"> Text4 </div>
  <div class="box"> Text5 </div>
  <div class="box"> Text6 </div>
  <div class="box"> Text7 </div>
  <div class="box"> Text8 </div>
</div>