Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post

    Centering page contents (gallery/galleries)

    Hello

    I am using
    Code:
    <style type="text/css">
    
    #myGallery
    {
      margin-left: auto;
      margin-right: auto;  
    }
    
    </style>
    in the <head>tags of my main page and
    Code:
    <body bgcolor="#000000">
    
    <div id="myGallery">
    
    .CONTENTS
    
    </div>
    	</body>
    in the <body> tags, but my page will not centre. I have validated the code (HTML) and don't get any errors. I thought
    Code:
    {
      margin-left: auto;
      margin-right: auto; }
    was the correct way to centre page contents? The page is here:

    http://www.bayingwolf.com/Helena/Helena.html

    Thanks

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    The css.

    body
    {
    width: 1024px;
    margin: 0 auto;
    }

    The content will fit and be centered in that width.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • Users who have thanked DrDOS for this post:

    SteveH (11-18-2013)

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Thank you Dr Dos

    It's not really centred but at least its away from that left-hand side!

    Thanks again..

    Steve

  • #4
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,668
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Hello SteveH,
    To center an element you need three things:
    1. a valid DocType
    2. an element with a width
    3. that elements right/left margins set to auto


    Since you can't center a full width element and your containing #vlightbox1 through 5 have a 100% width, you would need change that width to something like 695px.

    ---

    You can center it dynamically too, you already use display: inline-block; on each anchor, just make the CSS for the body look like this - #body
    {text-align: center;}


    ---

    A background color shows why it's "not really centred"
    Look at your page with this bit highlighted in red added to the CSS and you'll see that 1024px has some extra room -
    Code:
    #body
    {
    width: 1024px;
    margin: 0 auto;
    background: #f00;
    }
    Last edited by Excavator; 11-18-2013 at 03:19 AM.
    Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
    Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad

  • Users who have thanked Excavator for this post:

    SteveH (11-18-2013)

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Hello Excavator

    Oh! That's a lot better, thank you.

    I didn't know that
    Code:
    {text-align: center;}
    could be used to align images/image placeholders as well as text.

    Thanks again.

    Steve


  •  

    Posting Permissions

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