Consider using absolute positioning
for optimal results.
HTML:
<a href="link.html" >
<img id="centerimage" src="https://pbs.twimg.com/profile_images/378800000442759461/b483cdd049f470928e7b20051f95b8cc.jpeg" />
</a>
CSS:
#centerimage {
position: absolute;
left: 0;
right: 0;
top:0;
margin: auto;
}
To ensure the element stays within the structure of your document, utilize a wrapper
with a relative
CSS class.
HTML
<div class="wrapper">
<a href="link.html" >
<img id="centerimage" src="https://pbs.twimg.com/profile_images/378800000442759461/b483cdd049f470928e7b20051f95b8cc.jpeg" />
</a>
</div>
CSS
.wrapper {
position:relative;
margin-top:25px;
}
#centerimage {
position: absolute;
left: 0;
right: 0;
top:0;
margin: auto;
}
Enhancement: The issue with your link stretching across the page was due to using display:block;
without a proper containment. A wrapper
is essential, along with defining an absolute position
for your img
inside it to maintain control over its location within the document structure.
Simply wrapping it and styling the wrapper element while setting absolute position
for the img
will keep it contained, allowing you to position the wrapper
as needed in your document
through regular means, hence the use of relative
style.