Skip navigation
Currently Being Moderated

Centering Images using CSS but having links on the Images

Aug 8, 2012 2:48 AM

Hi all,

 

I have a problem on centering an image using CSS. I read on many forums that you have to use margin-left , margin-right and set to auto. and also set the display:block which I had done.

 

The problem is if I want to have a link from the image. Because I have set the display to block, the link will span the entire container containing it! How do I resolve that? I only want the image to be linkable and not the entire container!

 

Thanks for reading!

 
Replies
  • Currently Being Moderated
    Aug 8, 2012 3:06 AM   in reply to clavin007

    Set the container css which houses your image to - text-align: center; - rather tah using display: block; on the image.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 8, 2012 5:01 AM   in reply to clavin007

    Because I have set the display to block, the link will span the entire container containing it!

     

    That's incorrect.  If the display:block is applied to only the IMAGE, the anchor tag will be exactly the same dimensions as the image and will not fill the container.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 8, 2012 5:16 AM   in reply to MurraySummers

    Murray *ACP* wrote:

     

    Because I have set the display to block, the link will span the entire container containing it!

     

    That's incorrect.  If the display:block is applied to only the IMAGE, the anchor tag will be exactly the same dimensions as the image and will not fill the container.

    Hi Murray,

     

    Weirdly it does in my experiments. (code below). Even if I set the 'a' tag to the width of the image it still makes the whole <div> container clickable:

     

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>Untitled Document</title>

    <style type="text/css">

    #imgWrapper {

        width: 900px;

        margin: 0 auto;

        border: 1px solid #000;

    }

    #imgWrapper img {

        margin: 0 auto;

        display: block;

    }

    </style>

    </head>

     

    <body>

    <div id="imgWrapper">

    <a href="http://www.bbc.co.uk"><img src="slice_2.jpg" width="595" height="130" /></a></div>

    </body>

    </html>

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 8, 2012 5:18 AM   in reply to osgood_

    Well, that's because of the margin:0 auto; style which I forgot about when I penned my reply.  Since margin adds to the effective width of the image, you would certainly expect it to be reflected in the size of the wrapping anchor.  If that style were not in the rule, then you would see that this is what happens.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 8, 2012 5:20 AM   in reply to MurraySummers

    Murray *ACP* wrote:

     

    you would certainly expect it to be reflected in the size of the wrapping anchor.  If that style were not in the rule, then you would see that this is what happens.

     

    yes, sorry it does. I made an error in the css when I tested but adding the below works:

     

    #imgWrapper a {

        width: 595px;

        margin: 0 auto;

        display: block;

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 8, 2012 5:22 AM   in reply to MurraySummers

    Murray *ACP* wrote:

     

    Well, that's because of the margin:0 auto; style which I forgot about when I penned my reply.  Since margin adds to the effective width of the image,

     

    Yes, or course that's easily forgotten when there is 'no' margin added to the image.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 1:15 AM   in reply to clavin007

    Just use text-align: center; on the <div> to horizontally center your images:

     

    <div id="myImage"><img scr="myImage.jpg" width="200" height="200" alt="" /></div>

     

    #myImage {

    width: 500px;

    text-align: center;

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points