Center div horizontally
Asked Answered
K

7

7

On this page, I would like to horizontally center the main #container div relative to the page. Normally I would achieve this by adding a CSS rule,

#container { margin: 0 auto }

However, the layout of this page (which I did not write), uses absolute positioning for #container and most of its child elements, so this property has no effect.

Is there any way I can achieve this horizontal centering without rewriting the layout to use static positioning? I've no problem with using JavaScript/JQuery if that's the only way to achieving my objective.

Khadijahkhai answered 12/1, 2011 at 7:37 Comment(3)
@Starx good question, I've updated my post with the answer (which is yes)Tracitracie
in that case @rquinn has the answer for youCrimson
FYI you would need to change the position of the #container to position:relative, which essentially you should be able to do without affecting things too much, as long as the #container is the outter most element.Swinford
C
7

Same as @rquinn answer but this will adjust to any width. Check this demo

http://jsfiddle.net/Starx/V7xrF/1/

HTML:

<div id="container"></div>

CSS:

* { margin:0;padding:0; }
#container {
    width:50px;
    position:absolute;
    height:400px;
    display:block;
    background: #ccc;
}

Javascript

function setMargins() {
    width = $(window).width();
    containerWidth = $("#container").width();  
    leftMargin = (width-containerWidth)/2;    
    $("#container").css("marginLeft", leftMargin);    
}

$(document).ready(function() {
    setMargins();
    $(window).resize(function() {
        setMargins();    
    });
});
Crimson answered 12/1, 2011 at 9:36 Comment(1)
@Don, Why haven't you accepted an answer? You liked it didn't you? :DCrimson
K
4

You can use also use jQuery:

  function setMargins() {
    var width = $(window).width();
    if(width > 1024){
        var leftMargin = (width - 1024)/2;
        $("#container").css("marginLeft", leftMargin);    
    }
 }

Then I put this code after the $(document).ready event:

$(document).ready(function() {

    setMargins();

    $(window).resize(function() {
        setMargins();    
    });
});
Kabul answered 12/1, 2011 at 8:32 Comment(0)
T
1

the container width is 1024px, so you can try to give a left value 50% and margin-left: -512px.

Tenuto answered 12/1, 2011 at 7:52 Comment(3)
this wouldn't work at screen with 800px would it? or 1280, or 1360 ........ SO ONCrimson
why not? you can try it with resizing your window.Tenuto
it will work on larger screens, but on smaller ones, the user will not be able to see the whole image. (He won't be able to scroll them into view)Unmanned
H
0

Using Javascript, this will put #container at the center of your browser's window.

document.getElementById("container").style.top= (window.innerHeight - document.getElementById("container").offsetHeight)/2 + 'px';
document.getElementById("container").style.left= (window.innerWidth - document.getElementById("container").offsetWidth)/2 + 'px';
Honkytonk answered 12/1, 2011 at 8:13 Comment(0)
D
0

If your main container is using absolute position, you can use this CSS to center it horizontally;

#container {
   position:absolute;
   width:1000px;  /* adjust accordingly */
   left:50%;
   margin-left:-500px; /* this should be half of the width */
}
Dibbrun answered 12/1, 2011 at 8:36 Comment(0)
A
0

It's simple. Try this

body {
    margin:50px 0px; padding:0px; /* Need to set body margin and padding to get consistency between browsers. */
    text-align:center; /* Hack for Internet Explorer 5/Windows hack. */
}

#Content {
    width:500px;
    margin:0px auto; /* Right and left margin widths set to "auto". */
    text-align:left; /* Counteract to Internet Explorer 5/Windows hack. */
    padding:15px;
    border:1px dashed #333;
    background-color:#eee;
}
Activate answered 12/1, 2011 at 12:32 Comment(2)
But will this work if #Content and it's child elements are absolutely positioned?Tracitracie
this will work even for child element if possible can you give me some more detailActivate
M
-1

This way worked best for me. Without changing margins, but the position. Required: object -> margin-left: 0; and position: relative;

check example here: jsfiddle Code:

function setObjPosition(container, object) {
    var containerWidth = $(container).width();
    var objectWidth = $(object).width();

    var position = (containerWidth / 2) - (objectWidth / 2);

    $(object).css('left', position);
}

function setPositionOnResize(container, object) {
    setObjPosition(container, object);
    $(container).resize(function () {
        setObjPosition(container, object);
    });
}

Use:

<script type="text/javascript">
    $(document).ready(function () {
        setPositionOnResize(window, "#PanelTeste");
    });
</script>

It can be centered within any container.

Ma answered 3/9, 2015 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.