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
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts

    Margin Centering Trick Not Working On Image

    HTML
    Code:
    <img src="/images/misc/important.jpg" id="importantimg" alt="Important Information" title="Important Information"/>
    CSS
    Code:
    #importantimg {
       width: 100px;
       height: 40px;
       border: 0px;
       margin: 0px auto;
    }
    I can't get the image to center in its containing div. I've never known margin 0px auto to fail like this. I even tried text-align center, but no dice. Is there a method for centering images that I'm unaware of? I believe this is the first time I've tried to center an image instead of something like a div. I tried in both Firefox and IE, with no luck.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    <img> is an inline element and not a block level element.

    If you put the <img> in a <div> and set the div width to the width of the img src and then set the div's left and right margins to auto, it should centre within the div's parent element.

  • Users who have thanked bullant for this post:

    Joseph Witchard (05-13-2011)

  • #3
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts
    I had no idea that even mattered. Thanks There wouldn't happen to be a list of the different tags and what type of element they are anywhere online, would there?

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    You're welcome

    A list of html elements and their types. Not sure how complete it is though.

  • #5
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,673
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Wouldn't this be easier than adding an unnecessary div?
    Code:
    #importantimg {
       width: 100px;
       height: 40px;
       border: 0px;
       margin: 0px auto;
    display: block;
    }
    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


  •  

    Posting Permissions

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