/    Sign up×
Community /Pin to ProfileBookmark

Adjust footer position dynamically

Hello,
I have tried some css & html methods to place the footer at the bottom of the page no matter at which height it will be after the javascript files will run. But I cannot do it.
I tried then to adjust the position of the footer dynamically after the window has loaded and the footer is placed indeed at the end of the document, but if the document gets bigger, the footer doesn’t go lower.
Specifically:

FILE html_test.html

[code=html]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>test</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>
<link rel=”stylesheet” href=”css_test.css”/>
<!–Include other scripts that produce code dynamically into the body–>
<script type=”text/javascript” src=”footer.js”></script>
</head>
<body>
<div id=”body”>
<!– HTML PRODUCED DYNAMICALLY –>
</div>
<div id=”footer” class=”hidden”>FOOTER</div>
</body>
</html>
[/code]

FILE footer.js

[CODE]
function getDocHeight(){
var D = document;
return Math.max(
Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
Math.max(D.body.clientHeight, D.documentElement.clientHeight)
);
}
$(window).load(function(){
document.getElementById(“footer”).style.top=getDocHeight()+40+”px”;
document.getElementById(“footer”).style.display=”block”;
});
[/CODE]

FILE css_test.css

[CODE]
.hidden{display:none;}
#body {
min-height: 100%;
height: auto !important;
height: 100%;
padding:10px;
padding-bottom:20px; /* Height of the footer */
}
#footer{
width: 100%;
position:absolute;
height:20px;
}
[/CODE]

With this frame of code, I succeed to place the footer at the end of the page when it is not bigger than the screen. But when the HTML code that is produced dynamically, gets more and more and the height of the body exceeds the height of the screen, then the footer remains at the end of the “first screen”.
I thought that I have to to run the js file after all others have finished, so as to get the updated height of the document. So, I include the footer.js at last of all the other js files, but it didn’t work.
How can I fix it ?
Thank you !!!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@cbVisionJun 30.2011 — Have you tried the stick footer approach here?

http://ryanfait.com/sticky-footer/
Copy linkTweet thisAlerts:
@exquisitenickauthorJun 30.2011 — I cannot make the sticky footer work. The Footer appears at the bottom of the screen, but not at the end of the dynamically created document. Any ideas?!

Congrats for your wedding !?
Copy linkTweet thisAlerts:
@exquisitenickauthorJul 05.2011 — Finally, I found what is wrong with the sticky footer but I still cannot find a solution.

If inside the <div class='wrapper'> there is another div with absolute position, the sticky footer cannot work and the footer is placed at the end of the screen, instead of the end of the document.

Here is the code:

FILE footer_test.html
[code=html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>footer_test</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="footer_test.css"/>
</head>
<body>
<div class="wrapper">
<div id="container">
test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>
<div class="push"></div>
</div>
</div>
<div class="footer">
<p>Copyright (c) 2011</p>
</div>
</body>
</html>
[/code]


FILE footer_test.css
[CODE]
html, body {
height: 100%;
}
.wrapper {
color: black;
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
height: 142px; /* .push must be the same height as .footer */
}
#container{
position: absolute; /******** If position is set,then the footer cannot be placed at the end of the document. *******/
left:200px;
top:10px;
border:2px solid #597FAA;
width:500px;
}
[/CODE]


How can I make it work ???
×

Success!

Help @exquisitenick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...