Reputation: 47
img.imgdemo {
background: white;
width: 65%;
margin: auto;
}
<a class=imagelink href=periodictable.htm>
<img class=imgdemo src=ptabledemo.png>
</a>
I'm trying to center an image that also acts as a link, but when it has a style of display: block, it acts as a block element, making everything in it's width a link as well. Is there any way to fix this?
Upvotes: 1
Views: 2040
Reputation: 11
If your image is 200px
img {
width: 200px;
margin-left: calc(50% - 200px / 2);
}
If your image is 65%
img {
width: 65%;
margin-left: calc(50% - 65% / 2);
}
Upvotes: 1
Reputation: 15160
Images are inline just like text so:
.imagelink {
text-align:center;
}
Upvotes: 4
Reputation: 9583
One option you could use is the transform: translate()
property and give the a.imagelink
display: inline-block
. This will let you set the size of the image to whatever you want and make sure the link is the same size and stays centered at all times:
a.imagelink {
display: inline-block;
background: blue;
position: relative;
left: 50%;
transform: translateX(-50%);
}
img {
width: 50%;
display: block;
}
Upvotes: 2