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 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2008
    Location
    Jupiter
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Centering Images in css help

    Im trying to center two images on my site by using css instead of using the deprecated <center> tag in html. So i made a div, added in some information and it now it looks distorted. Any links to tutorials on centering images. Would there happen to be an option in float o do this, or do float just move objects left and right? Thanks.

  • #2
    New Coder
    Join Date
    Jan 2009
    Location
    Florida
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Uhm could you link us to your site?

  • #3
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,675
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Hello tokio,
    You don't give us much to go on.
    Are you talking about vertically or horizontally centering them?

    Either way, we'll need to see your code. A test link would be best but to post code here in the forum, please use the code tags. (the * button in the edit post window)
    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

  • #4
    New Coder
    Join Date
    Jun 2008
    Location
    Jupiter
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Excavator View Post
    Hello tokio,
    You don't give us much to go on.
    Are you talking about vertically or horizontally centering them?

    Either way, we'll need to see your code. A test link would be best but to post code here in the forum, please use the code tags. (the * button in the edit post window)
    Im building everything from my local machine and my site is too bland to go live now. I would like to vertically center it.

    Code:
    #picture {
    
    position: absolute;
    top: 50%
    left: 50%
    width: 200px;
    height: 200px;
    margin: -100px 0 0 -150px;
    background: #ccc;
    }

  • #5
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,675
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Some vertical center demos here - http://nopeople.com/CSS/vertical%20center%20with%20CSS/

    View the source to see how it's done.
    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

  • #6
    New Coder
    Join Date
    Jun 2008
    Location
    Jupiter
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Excavator View Post
    Some vertical center demos here - http://nopeople.com/CSS/vertical%20center%20with%20CSS/

    View the source to see how it's done.
    Thanks bud, but i did some searching before i posted this and saw that link and did try to reverse engineer the code but couldnt get it to work. Im kind of confused about how exactly dimensions work.

  • #7
    New Coder
    Join Date
    Jan 2009
    Location
    Florida
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    -removed-
    Last edited by WillyR; 02-22-2009 at 09:38 PM.

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    San Francisco
    Posts
    455
    Thanks
    19
    Thanked 15 Times in 15 Posts
    give the images IDs, then you can use CSS's margin attribute to center them

    In the CSS:
    Code:
    #pony, #cow { 
         display: block;
         margin: 5px auto; }
    When you only use two values, the first one is for top and bottom, the second one is for right/left. In this case there will be 5 pixels of margin on the top and bottom, and auto means evaluate the width of the containing element and set the margin accordingly. Because a value was not given for either left or right, it will automatically center it.


    In the body
    Code:
    <img id="pony" src="pony.jpg" alt="pony">
    <img id="cow" src="cow.gif" alt="cow">
    Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx


  •  

    Posting Permissions

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