Dex
Dex

Reputation: 12749

Floated Div Tags Wrapping to New Line When Window Resizes

I have two divs that I want to float on the same line. I don't want the right one to wrap until the window gets around 250px wide.

I am setting the initial widths of the divs to percentages and this seems to be causing issues. The right div will wrap to a new line well before it shrinks to a min-width of 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/

Upvotes: 0

Views: 998

Answers (2)

Dex
Dex

Reputation: 12749

Removing the width on the right div partially solved the problem.

But it looks like I had to resort to using 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;
    }
}

Upvotes: 1

Danield
Danield

Reputation: 125443

It's the min-width:150px; in the left div which is causing the right div to wrap to a new line well before it shrinks to a min-width of 100px;

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

FIDDLE

Upvotes: 0

Related Questions