Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

javascript - Strange margin in a resized img tag

Conisder the html code below:

<a>
<img src="//path/to/img.jpg" />
</a>

The CSS code is

a {
   height: 64px;
   width: 100%;
   padding: 10px;
   display: block;
}

img {
   max-height: 100%;
}

The original size of the image is 246 x 74. Due to the effect of css codes, the height of image will be resized into 64 - 10 * 2 = 44px. The aspect ratio of the image will remain unchanged, which means the width of the image will becoem 44 / 74 * 246 = 146px. The browser renders the image tag as expected. However, the width of the a tag is 246px instead of 146px;

enter image description here

enter image description here

As shown in the figure, there is a strange "brown block" to the left of the img tag, which is not listed as margin.

My question is how can I make the width of the a tag equal to the width of the img tag without explicity set width: 246px for the a tag.

question from:https://stackoverflow.com/questions/65642289/strange-margin-in-a-resized-img-tag

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use width: max-content


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...