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
    Jul 2007
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    centering img in css

    I'm trying to find an efficient way for centering hyperlinked images in CSS. I wan this way to work with IE, firefox, and opera. I can easily use the <center> element, but XHTML 1.0 Strict doesn't support that.

    Code:
    .newspost img {
    display:block;
    margin-left:auto;
    margin-right:auto;
    border:0px;
    text-align:center;
    }
    Right now that's what I use. The problem with that is that when I'm trying to put two or more images next to each other, the second image just moves onto the next line right underneath the first image. Is there a better way of doing this?

  • #2
    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 Bob42,

    margin:0 auto; should work for centering one image. When your putting 2 images side by side you are no longer centering an image... you'll have to do something different than your margin:0 auto;

    Try float:left; on both images and adjust the positions with margin.
    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

  • #3
    Regular Coder Actinia's Avatar
    Join Date
    Feb 2007
    Location
    Essex, UK
    Posts
    250
    Thanks
    1
    Thanked 23 Times in 22 Posts
    You could put the two images into one div (called say wrap), then center that using margin auto.
    Code:
    .newspost #wrap {
    display:block;
    border:0px;
    text-align:center; /* keep IE6 happy */
    width: 500px; /* sufficient to hold both images */
    }
    .newspost #wrap img{
    margin-left:auto;
    margin-right:auto;
    }
    John

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    What's wrong in giving just text-align:center to the container of all these hyper linked imgs and then giving
    Code:
    a img {border:none}
    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    Because that leaves the image inline, and that leaves extra space for descending letters =P


  •  

    Posting Permissions

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