代码之家  ›  专栏  ›  技术社区  ›  Mohsin Younas

在不限制第一个分区的宽度的情况下,两个分区可以相邻站立。

  •  0
  • Mohsin Younas  · 技术社区  · 8 年前

    我试图找到一种方法,两个div可以并排站在一起,而div one有一个内容,一些写作可能。而第2部分也包含一些文本。

    DIV 1应根据DIV 2的宽度自动调整宽度,以便当DIV 1中的内容增加时,高度增加而不是宽度增加,不应将DIV 2推出或不影响DIV 2。

    请按照链接操作。其中红色部分应根据绿色部分调整其宽度。

    fiddle-jsfiddle link

    #box1 {
      background: red;
      height: 100px;
      width: auto;
      float: left
    }
    
    #box2 {
      background: green;
      height: 100px;
      width: 400px;
      float: right
    }
    <div id="wrapper">
      <div id="box1">
        <bold>BOX 1</bold><br>Is there any way, box2 can stand next to the box 1 without limiting the width of box1. (Repeat)Is there any way, box2 can stand next to the box 1 without limiting the width of box1</div>
      <div id="box2">
        <bold>BOX 2</bold><br>Is there any way, box2 can stand next to the red without limiting the width of box1</div>
    </div>
    5 回复  |  直到 8 年前
        1
  •  1
  •   Johannes    8 年前

    最简单的方法是使用 flex 和改变 height 到 min-height (并删除浮动,并添加如下灵活设置):

    #box1 {
      background: red;
      min-height: 100px;
    }
    
    #box2 {
      background: green;
      min-height: 100px;
      width: 400px;
      flex-shrink: 0;
      flex-grow: 0;
    }
    
    #wrapper {
      display: flex;
    }
    <div id="wrapper">
      <div id="box1">
        <bold>BOX 1</bold><br>Is there any way, box2 can stand next to the box 1 without limiting the width of box1. (Repeat)Is there any way, box2 can stand next to the box 1 without limiting the width of box1</div>
      <div id="box2">
        <bold>BOX 2</bold><br>Is there any way, box2 can stand next to the red without limiting the width of box1. Is there any way, box2 can stand next to the red without limiting the width of box1</div>
    </div>
        2
  •  0
  •   Oscar Zhang    8 年前

    这个CSS可以工作(使用flex box并将高度保留到wrapper div)

    #wrapper{
      display: flex;
      height: 150px;
    }
    #box1 {
        background: red;
        flex:1; /* you can change the width here */
        /* width: 50%; */    
        float:left
    }
    
    #box2 {
        background: green;
        flex:1;/* you can change the width here */
        /* width: 50%; */    
        float:right
    }
    

    http://jsfiddle.net/x7cfkpdy/14/

        3
  •  0
  •   jonask    8 年前

    flexbox是一个很好的解决方案。对于wrapper div,添加“display:flex;”。对于子分隔符,删除浮点数、宽度和高度,只需添加“flex:1 10;”。以下是更新的CSS:

    #wrapper {
    display: flex;
    }
    
    #box1  {
    background-color: red;
    flex: 1 1 0;
        }
    
    #box2 {
    background-color: green;
    flex: 1 1 0;
        }
    
        4
  •  0
  •   Dunsin Olubobokun    8 年前

    使用CSS flex , flex-grow 然后取出浮点数。然后用控制高度 min-height

    #wrapper{
      display: flex;
    }
    
    #box1 {
        background: red;
        flex:1; 
        /*flex-grow:1; depending on your preference*/
        min-height: 100px;
    }
    
    #box2 {
        background: green;
        flex:1;
        /*flex-grow:2; depending on your preference*/
        min-height: 100px;
    }
    
        5
  •  0
  •   Nitin Bisht    8 年前

    这将完全符合您的目标:

    #wrapper{
       display: flex;
       flex-flow: row nowrap;
       min-height: 150px;
    }
    
     #box1 {
       flex:1; 
       background: red;
     }
    
    #box2 {
       flex:0; 
       flex-basis:400px;
       background: green;
    }
    <div id="wrapper">
      <div id="box1">
        <bold>BOX 1</bold><br>
        Is there any way, box2 can stand next to the box 1 without limiting the width of box1. (Repeat)Is there any way, box2 can stand next to the box 1 without limiting the width of box1
      </div>
      <div id="box2">
        <bold>BOX 2</bold><br>
        Is there any way, box2 can stand next to the red without limiting the width of box1
      </div>
    </div>

    Fiddle example