I am using this script to center my div horizontally and vertically.
When the page loads the div gets centered vertically, not horizontally until I resize the browser.
What am I doing wrong?
$(document).ready(function (){ $(window).resize(function (){ $('.className').css({ position:'absolute', left: ($(window).width() - $('.className').outerWidth())/2, top: ($(window).height() - $('.className').outerHeight())/2 }); }); $(window).resize(); });
To center a div vertically on a page, you can use the CSS position property, top property, and transform property. Start by setting the position of the div to absolute so that it's taken out of the normal document flow. Then set the top property to 50%.
When the element to be centered is an inline element we use text-align center on its parent. When the element is a block level element we give it a width and set the left and right margins to a value of auto. With text-align: center in mind, most people look first to vertical-align in order to center things vertically.
align="center" attribute is now obsolete. You can use text-align attribute for this as following. This will center all the content inside the parent DIV. An optional way is to use margin: auto CSS attribute with predefined widths and heights.
I normally use this "technique":
$(function() { $('.className').css({ 'position' : 'absolute', 'left' : '50%', 'top' : '50%', 'margin-left' : -$('.className').width()/2, 'margin-top' : -$('.className').height()/2 }); });
UPDATE:
I'm updating the solution, as suggested by the user Fred K, using .outerWidth()
and .outerHeight()
to have a more precise centering.
$(function() { $('.className').css({ 'position' : 'absolute', 'left' : '50%', 'top' : '50%', 'margin-left' : -$('.className').outerWidth()/2, 'margin-top' : -$('.className').outerHeight()/2 }); });
Some additional notes from jQuery' documentation (.outerWidth()
, .outerHeight()
):
The number returned by dimensions-related APIs, including .outerWidth(), may be fractional in some cases. Code should not assume it is an integer. Also, dimensions may be incorrect when the page is zoomed by the user; browsers do not expose an API to detect this condition.
The value reported by .outerWidth() is not guaranteed to be accurate when the element's parent is hidden. To get an accurate value, you should show the parent first, before using .outerWidth().
UPDATE 2:
A simple update to show how you could use this
inside the css()
method in case there are more elements with the same class
tag with different sizes.
$(function() { $('.className').css({ 'position' : 'absolute', 'left' : '50%', 'top' : '50%', 'margin-left' : function() {return -$(this).outerWidth()/2}, 'margin-top' : function() {return -$(this).outerHeight()/2} }); });
use this to center:
$.fn.center = function () { this.css("position","absolute"); this.css("top", ( $(window).height() - this.height() ) / 2 + "px"); this.css("left", ( $(window).width() - this.width() ) / 2 + "px"); return this; } $('yourElem').center();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With