Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto adjust (stretch) div height and width using jQuery or CSS

I have 4 divs with ids A, B, C and D, like below;

<div id="A"></div>
<div id="content">
    <div id="B"></div>
    <div id="C"></div>
</div>
<div id="D"></div>

Div A & D have fixed width & height. Div B has fixed width. I want the height of Div B and height + width of Div C calculated automatically. I want to stretch Divs B and C in between div A and D. Also I want to stretch Div c in between div B and right margin. The page thus wont be having any scroll bars and empty space.

My expected layout is like below

enter image description here

How can I make this possible using jquery / css?? Anybody has a solution and pls give me a fiddle / demo ??

Thanks in advance...:)

blasteralfred

like image 840
Alfred Avatar asked Apr 06 '11 13:04

Alfred


People also ask

How auto adjust height in div?

Simple answer is to use overflow: hidden and min-height: x(any) px which will auto-adjust the size of div.

How do I stretch a div to a full height container?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.

How do I set dynamic height in jQuery?

Answer: Use the JavaScript height() method You can set the height of a <div> box dynamically using the jQuery height() method.

How do you auto adjust the div width according to content in it?

One way you can achieve this is setting display: inline-block; on the div . It is by default a block element, which will always fill the width it can fill (unless specifying width of course).


1 Answers

Well, despite asking, I'm still not entirely sure what you want.

I don't think you have to use jQuery for it either. How's this?

Live Demo

CSS:

#container {
    position: relative;
    width: 200px;
    height: 200px;
}
#top, #left, #right, #bottom {
    position: absolute
}
#top {
    top: 0;
    width: 100%;
    height: 50px;
    background: #00b7f0
}
#left {
    top: 50px;
    width: 50px;
    bottom: 50px;
    background: #787878
}
#right {
    top: 50px;
    left: 50px;
    right: 0;
    bottom: 50px;
    background: #ff7e00
}
#bottom {
    bottom: 0;
    width: 100%;
    height: 50px;
    background: #9dbb61
}

HTML:

<div id="container">
    <div id="top"></div>
    <div id="left"></div>
    <div id="right"></div>
    <div id="bottom"></div>
</div>

Or maybe you want it like this instead? http://jsfiddle.net/thirtydot/4BR9s/1/

like image 110
thirtydot Avatar answered Sep 18 '22 17:09

thirtydot