Results 1 to 8 of 8

Thread: Css center div

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Css center div

    Hello everyone,
    I'm sure there is a very simple answer to my question, but I just can't work it out.
    How do you center a div?
    It's that simple. Currently I'm using margin-left:29%; but this won't center it if there screen is bigger or smaller.
    Yes I've googled it, no I can't find an answer.
    Any help?

  2. #2
    Join Date
    Jan 2012
    Location
    India
    Posts
    45
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default

    try this

    Code:
    margin:auto;

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Thanks for your reply,

    It still won't work. Tried that but it only centers it vertically not horizontally. I'd either like it to just do horizontally or do both, preferably the latter.

    I'm using this to style a div, which contains more divs including the navigation menu and the main content window.

    Any help??????????

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by keyboard1333 View Post
    Tried that but it only centers it vertically not horizontally.
    I think you have your horizon mixed up with your vertex/zenith line.
    Last edited by jscheuer1; 03-03-2012 at 01:59 AM. Reason: English
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    When I tried the marign:auto; it moved it down the page vertically but it was still right up against the left of the page.
    Keyboard1333

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    margin:auto; usually works. We'll need a link to your page to figure out why this time it is different.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Can't give you a link right now. Will post one in a couple of days.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It may or may not be adaptable to what you have in mind, but there's always:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Center</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
    html, body {
    	height: 100%;
    	margin: 0;
    }
    #center {
    	position: absolute;
    	margin: 0;
    	top: 50%;
    	left: 50%;
    	height: auto;
    	width: auto;
    	border: 1px solid #000;
    	padding: 1ex;
    }
    </style>
    <script type="text/javascript" defer>
    (function(){
    	function center(){
    		var center = document.getElementById('center');
    		center.style.marginLeft = center.offsetWidth / -2 + 'px';
    		center.style.marginTop = center.offsetHeight / -2 + 'px';
    	}
    	if (window.addEventListener){
    		window.addEventListener('DOMContentLoaded', center, false);
    	}
    	else if (window.attachEvent){
    		center();
    	}
    })();
    </script>
    </head>
    <body>
    <div id="center">Where Am I?</div>
    </body>
    </html>
    Works in IE 6+, virtually all current version browsers. A word of caution though - if the window is narrower or shorter than the center div, part of it will be off screen to the left or top respectively, with no possibility of scrolling that part into view. And if images are used within the center div, they should have their width and height specified.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •