Vertically Centering Child Div In Parent Container With Javascript On Page Load Is Not Setting Position?
I am using JavaScript to vertically center a child div within a fluid container. I essentially accomplish this by calculating the height of the parent container and the height of t
Solution 1:
Try this fiddle: http://jsfiddle.net/nRRn6/
used css:
html, body {
height:100%;
}
.lead {
width:100%;
height:100%;
background:red;
position:relative;
}
.message {
width:120px;
height:200px;
background:yellow;
position:absolute; /* <---required*/
left:0; /* <---required*/
bottom:0; /* <---required*/
}
used html:
<div class="lead">
<div class="message"></div>
</div>
used jQuery:
var homepageLead = function () {
var lead = $('.lead'),
message = $('.message'),
lead_height = lead.height(),
message_height = message.height(),
lead_center = .5 * lead_height,
message_center = .5 * message_height,
center = (lead_center - message_center);
return message.css('bottom', center);
};
$(function () {
$(window).resize(function () {
homepageLead();
}).resize();
});
Post a Comment for "Vertically Centering Child Div In Parent Container With Javascript On Page Load Is Not Setting Position?"