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!
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>
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.
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;
}
Hi Murray,
Thanks for your prompt reply. What I had done is that I have created a class style called .centraliseItem and apply to the image like so :
.centraliseItem{
margin: 0 auto;
display:block;
}
<a href="try-out.htm"><img src="images/main_banner_img/1.jpg" width="593" height="281" class="centraliseItem" /></a>
I have essentially applied the display:block the the image item but still the link applies to the surrounding elements.
Accordingly to what you had mentioned since you mentioned I have to apply display:block to the image, I had even went to the extent of changing to inline style like so :
<a style="display:block; margin-left:auto;margin-right:auto;" href="try-out.htm"><img src="images/main_banner_img/1.jpg" width="593" height="281" class="centraliseItem" /></a>
to no avail.
Sorry, but I am still clueless!
Regards,
James
Hi osgood_,
Thanks for your reply.
Again, I have tried your recommendation and it works. However, I've noticed that you have the CSS applied to the anchor which is fine. The problem comes in when I want to centralise images with differing widths yet having the links applied to only the images and not the surrounding elements. There is no way I have to create multiple styles for differing widths!
For example, I cannot create like one that you had suggested :
#imgWrapper a {
width: 595px;
margin: 0 auto;
display: block;
}
Then I have another image of a width of 400px or for that matter 350px, it wouldn't be centralised anymore.
I guess the problem stems from the fact that in CSS, in order to centralise an item, we have to change the display to block which in actual fact it is an inline graphic which allows an image to be clickable and not the surrounding areas.
Do you have another solution?
Thanks!
North America
Europe, Middle East and Africa
Asia Pacific