Child div expand to fit parent?

This is a discussion on "Child div expand to fit parent?" within the Web Page Design section. This forum, and the thread "Child div expand to fit parent? are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Apr 20th, 2008, 00:22
Junior Member
Join Date: Mar 2007
Location: Long Island, NY
Age: 23
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Question Child div expand to fit parent?

So i have done many a search in various forums and on google and i came up short so i figure i'll post my delima here.

Ive been using tables for some time now but knowing it will be better for me in the long run, im trying to do away with them and use div's and css. I have a parent div (we'll call it main) with three children div's (we'll call them top, middle and bottom) inside. The main div's height is set to 100% (thats 100% of the body) while the top and bottom div's heights are both set to 100px. To keep the site looking the way i want it to, i would like the middle div to expand to fit the remaining space in the parent div. If i set the middle div's height to 100% it displays at the same size of the parent div (i know this is because of the way div's work and the box model and so on...). But i need it to display at 100% of the remaining space inside the main div.

I know this is pretty easy using tables but how can it be done using div's and css? Any ideas? Many thanks in advance!!
Reply With Quote

  #2 (permalink)  
Old Apr 20th, 2008, 01:28
Reputable Member
Join Date: Mar 2008
Location: Chester, UK
Age: 17
Posts: 418
Thanks: 2
Thanked 28 Times in 28 Posts
Re: Child div expand to fit parent?

Could you post your code so far?

Setting the height to 100% restricts the vertical flow,

For the footer try using position: absolute;

I quickly wrote this css which should work...

Code: Select all
html { 
    padding:0px;
    margin:0px;
}

body {
    background-color: #e1ddd9;
    font-size: 12px;
    font-family: Verdana, Arial, SunSans-Regular, Sans-Serif;
    color:#564b47;  
    padding:0px;
    margin:0px;
}

#container{
    height:100%;
}

#top{
    position:relative;
    height:100px;
    background-color:red;
}


#content {
    height:100%;
    padding: 0px;
    background-color: #ffffff;    
}

#bottom{
    position:absolute;
    height:100px;
    width:100%;
    bottom:0px;
    background-color:red;
}
The colours are just so the sections are easily visible...

And this structure for the divs

Code: Select all
       <div id="content">
        <div id="top">
          Blah blah Blah blah Blah blah Blah blah
        </div>
           Blah blah Blah blah Blah blah Blah blah
       <div id="bottom">
        Blah blah Blah blah Blah blah Blah blah
       </div>
     </div>
Notice the way i've nested them within the content, this is mainly due to the footer, as it create a page size of 100%+100px

So nesting them creates a page of 100% and then subtracts the 100px needed for the footer.

Is this any help to you?

P.S neither have been checked for validation, I generally leave this until after i have a working model.

Last edited by Bocaj; Apr 20th, 2008 at 01:39.
Reply With Quote
  #3 (permalink)  
Old Apr 20th, 2008, 11:16
Junior Member
Join Date: Mar 2007
Location: Long Island, NY
Age: 23
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Child div expand to fit parent?

wow, thats real clever the way you nested the div's like that. and it works great too!!

my code was just like:
Code: Select all
<html><head>

<style type="text/css">

body {
    height:100%;
    margin:0px;
    padding:0px; }

div.main {
    height:100%;
    width:984px;
    background-color:yellow; }

div.top {
    height:100px;
    width:100%;
    background-color:blue; }

div.middle {
    height:100%;
    width:100%;
    background-color:green; }

div.bottom {
    height:100px;
    width:100%;
    background-color:red; }

</style>

</head><body>

<div align="center">
<div class="main">

    <div class="top">header</div>
    <div class="middle">content</div>
    <div class="bottom">footer</div>

</div>
</div>

</body></html>
i gave my content its own div inside of the main div and as you can see the results would have never gave me the layout i was looking for.

on a short side note: ive noticed that when defining css attributes many people (you included, Bocaj) will say something like:
Code: Select all
#main {
    height:100%;
    width:984px;
    background-color:yellow; }

#top {
    height:100px;....so on and so forth
and then in the body put:

Code: Select all
<div id="main">text</div>
<div id="top">more text</div>
what is the difference between the class and id attribute? are there benifits to id over class? can you use class and id in the same div tag for even more control in your css stylesheets?
Reply With Quote
  #4 (permalink)  
Old Apr 20th, 2008, 11:21
SuperMember

SuperMember
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Child div expand to fit parent?

The difference between <div id and <div class is

id can be used only once on a page and class can be used as many times as required.

Hope that helps.

Pat
Reply With Quote
  #5 (permalink)  
Old Apr 20th, 2008, 11:27
Aso's Avatar
Aso Aso is online now
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,129
Blog Entries: 2
Thanks: 5
Thanked 36 Times in 33 Posts
Re: Child div expand to fit parent?

Here's a quick read on ID's vs classes.

Generally, most developers will use ID's for things like footers, wrappers and navigation.

It just separates them from the mix of classes, and since there's only one per page, it makes sense to use ID's in these conditions.
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Reply With Quote
  #6 (permalink)  
Old Apr 20th, 2008, 21:16
Junior Member
Join Date: Mar 2007
Location: Long Island, NY
Age: 23
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Child div expand to fit parent?

wow, very cool and informative, thanks a lot guys!!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript Parent and Child menu in Safari?? TR123 JavaScript Forum 3 Mar 8th, 2007 14:15
Submitting a parent form from a child window in IE 7 livehed JavaScript Forum 0 Feb 15th, 2007 16:23
parent child forms mrproggie ASP.NET Forum 1 Aug 12th, 2006 17:49
Parent Child accesssing greenkhan ASP.NET Forum 1 Jun 20th, 2005 09:21
Filling a textbox on a parent web user control from a child neilault ASP.NET Forum 8 Oct 27th, 2004 08:06


All times are GMT. The time now is 00:51.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43