【问题标题】:Floated Div Tags Wrapping to New Line When Window Resizes窗口调整大小时浮动的 div 标签换行
【发布时间】:2014-06-09 16:48:30
【问题描述】:

我有两个要浮动在同一行上的 div。在窗口宽度达到 250 像素左右之前,我不希望正确的换行。

我将 div 的初始宽度设置为百分比,这似乎会导致问题。右边的 div 在缩小到 100px 的最小宽度之前会换行;

<div id="#container">
    <div id="left">
        <div id="box"></div>
    </div>
    <div id="right">
        <h1>Hello World</h1>
    </div>
</div>

#container {
    display: table;
    width: 100%;
}
#box {
    width: 100px;
    height: 100px;
    background: gray;
    display: inline-block;
    max-width: 100%;
}
#left {
    float: left;
    width: 23.6%;
    min-width:150px;
    background: gold;
    text-align: center;
}
#right {
    float: left;
    width: 76.4%;
    min-width:100px;
    background: pink;
}

http://jsfiddle.net/5C6GB/

【问题讨论】:

    标签: css positioning


    【解决方案1】:

    移除右侧 div 的宽度部分解决了问题。

    但看来我不得不求助于display:table*

    http://jsfiddle.net/5C6GB/3/

    <div id="container">
        <div class="tr">
            <div id="left" class="td">
                <div id="box"></div>
            </div>
            <div id="right" class="td">
                <!-- <h1>Hello World</h1> -->
            </div>
        </div>
    </div>
    
    #container {
        display: table;
        width: 100%;
        background: red;
        height: 100px;
    }
    #box {
        width: 100px;
        height: 100px;
        background: gray;
        display: inline-block;
        max-width: 100%;
    }
    #left {
        width: 25%;
        min-width:150px;
        background: gold;
        text-align: center;
        height: 100%;
    }
    #right {
        min-width:100px;
        background: pink;
        width: 75%;
        vertical-align: bottom;
    }
    .tr {
        display: table-row;
        width: 100%;
    }
    .td {
        display: table-cell;
    }
    
    
    @media only screen and (max-width: 600px) { 
        #left, #right {
            display:block;
            width: 100%;
            min-height: 100px;
        }
        .tr {
            display: block;
        }
        #container {
            display: block;
        }
    }
    

    【讨论】:

      【解决方案2】:

      这是左侧 div 中的 min-width:150px; 导致右侧 div 在缩小到 100 像素的最小宽度之前就换行了;

      #left {
          float: left;
          width: 23.6%;
          min-width:150px; /*remove  or change this to a smaller amount */
          background: gold;
          text-align: center;
      }
      

      FIDDLE

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 2015-02-23
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多