Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fit anchor parent to child image

How do you get rid of the element outline that the a tag inserts, without ruining the child element?

<a href="">
     <img alt="picsum" src="https://picsum.photos/200/300">
</a>

Anchor element outline

Ideally highlighting the anchor tag should yield the full width and height of the child element

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

display: block should fix this issue:

a,
img {
  display: block;
}
<a href="">
  <img alt="picsum" src="https://picsum.photos/200/300">
</a>

What’s happening is the <a> and img are display: inline by default, which creates extra space underneath them as inline is made to work well with text. It may seem stupid that things are this way but it allows an image to fit in nicely with text like you would do in a word processor.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading