Web Design and Development Forums

Child div expand to fit parent?

This is a discussion on "Child div expand to fit parent?" within the CSS Forum 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 > Design Your Website > CSS Forum

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Apr 20th, 2008, 00:22   #1 (permalink)
Junior Member
 
Join Date: Mar 2007
Location: Long Island, NY
Age: 22
Posts: 15
Send a message via AIM to linchpin311
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!!
linchpin311 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 20th, 2008, 01:28   #2 (permalink)
Up'n'Coming Member
 
Join Date: Mar 2008
Location: Chester, UK
Age: 17
Posts: 87
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.
Bocaj is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 20th, 2008, 11:16   #3 (permalink)
Junior Member
 
Join Date: Mar 2007
Location: Long Island, NY
Age: 22
Posts: 15
Send a message via AIM to linchpin311
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?
linchpin311 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 20th, 2008, 11:21   #4 (permalink)
 
Join Date: Jun 2007
Location: uk
Posts: 459
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
dab42pat is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 20th, 2008, 11:27   #5 (permalink)
Chief Moderator
 
aso186's Avatar
 
Join Date: Oct 2007
Location: UK
Posts: 701
Blog Entries: 2
Send a message via Skype™ to aso186
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.
__________________

aso186 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 20th, 2008, 21:16   #6 (permalink)
Junior Member
 
Join Date: Mar 2007
Location: Long Island, NY
Age: 22
Posts: 15
Send a message via AIM to linchpin311
Re: Child div expand to fit parent?

wow, very cool and informative, thanks a lot guys!!
linchpin311 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 10:07.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59