Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML CSS Remainder of space

How do I get the footer to take up the remainder of the page's vertical space without actually knowing how tall the content is? I can't figure out how to use javascript/css to accomplish this...

Just to be clear...

Scenario 1: The content ends halfway through the page, the footer would take up the remaining half. No scrollbars necessary.

Scenario 2: The content takes up 1 1/2 pages, the footer would take up only what it needs (~200px). Scrollbars necessary.

<body>
 <div id="content">
 <div id="footer">
</body>

Oh, and I'm open to a jQuery way of doing this.

like image 529
myermian Avatar asked Sep 07 '10 16:09

myermian


People also ask

How do I fill a remaining space in CSS?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do you flex occupy remaining space?

Use the flex-grow property to make a flex item consume free space on the main axis. This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.

How do you make a div fill a remaining vertical space?

Another way of making a <div> fill the remaining space is to use the CSS position property. Just set the position to “absolute” to stretch the <div>.

How do you cover space in HTML?

To insert blank spaces in text in HTML, type &nbsp; for each space to add. For example, to create five blank spaces between two words, type the &nbsp; entity five times between the words. You do not need to type any spaces between the entities.


1 Answers

You can always try using jQuery to detect the height of the browser window, then deduct the content height from it to assign a height in pixels to the footer.

Though it would be different on different sized monitors.

To get the browser height, and store it as a variable you can use:

var browserHeight = $(window).height();

Content height can be stored using:

var contentHeight = $("#content").height();

Footer height can then be worked out like so:

var footerHeight = browserHeight - contentHeight;
$("#footer").height(footerHeight);

So altogether, you'd have:

<script type="text/javascript">
        $(document).ready(function(){
             //Get Browser and Content Heights
             var browserHeight = $(window).height();
             var contentHeight = $("#content").height();
             //Set footer height
             var footerHeight = browserHeight - contentHeight;            
             $("#footer").height(footerHeight);
        });
</script>

Or something like that :)

Anthony

like image 96
Ant Ali Avatar answered Sep 23 '22 23:09

Ant Ali