Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float a div without defining height property

Tags:

html

css

It looks very simple (and maybe is, just got stucked) -> just for fun, no practical need right now.

I got this:

enter image description here

<div class="master">
    <div class="left">
        <p>LEFT</p>
    </div>
    <div class="right">
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</p>
    </div>
</div>

.master {
    width: 100%;
    height: 100%;
}
.left {
    width:10%;
    float: left;
    background: red;
}
.left p {
    padding: 5px;
    color: #fff;
    text-align: center;
}
.right {
    width: 89%;
    margin-left: 1%;
    float: left;
    background: blue;
    color: #FFF;
}
.right p {
    padding: 0px 15px 0px 15px;
    text-align: justify
}

So, as you can see, I didn't define height property, so red div just takes the needed height.

What I am looking for is something like this: The red div takes all height of master, while blue is staying more tall.

enter image description here

JSFiddle here.

like image 657
fiskolin Avatar asked Jan 03 '14 17:01

fiskolin


2 Answers

I think the best way is:

.master {
    width: 100%;
    height: 100%;
    display: table;
}
.left {
    width:10%;
    height: 100%;
    float: left;
    background: red;
    display: table;
}
.right {
    width: 89%;
    margin-left: 1%;
    float: left;
    background: blue;
    color: #FFF;
    display: table;
}
like image 197
Michael Silunsky Avatar answered Sep 19 '22 12:09

Michael Silunsky


An alternative approach that does not use CSS tables is as follows:

.master {
    width: 100%;
    height: 100%;
    border: 1px dotted blue;
    position: relative;
}
.left {
    width:10%;
    background: red;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
}
.right {
    width: 89%;
    margin-left: 11%;
    overflow: auto;
    background: blue;
    color: #FFF;
}

If the .right block is the one that controls the overall height, keep .right in regular content flow and set margin-left: 11% to leave some white space.

Use absolute position to place and size the .left block.

See demo at: http://jsfiddle.net/audetwebdesign/97CY2/

However, if you don't know which of the two child elements is the taller, than table-cell's would be the way to go.

like image 31
Marc Audet Avatar answered Sep 19 '22 12:09

Marc Audet