如何消除HTML页面中图片与边框之间的异常间隙?
问题
我正在开发的HTML页面的article区块中,图片与其边框之间存在一处小间隙。尝试调整内边距、外边距及高度后均未解决问题,希望得到消除该间隙的方法。
HTML代码
<article> <section class="image"> <a href=""><img src="images/Thumbnail.gif" height="150" width="150" alt=""/></a> </section> <aside class="textBox"> <h3>Project</h3> <p>description</p> </aside> </article>
CSS代码(清理中)
@charset "UTF-8"; body { font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif; background-color: #64748B; padding: 4em; color: #000; background-image: url("images/background.png"); background-repeat: no-repeat; background-position: top absolute; background-size: cover; display: flex; flex-direction: row; flex-wrap: wrap } H1 { font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif; color: #fff; float: right; margin-right: 1em; font-size: 40pt; } .NavUl { margin-top: 5px; font-weight: bold; text-align: center; width: 100%; } td { background-color: #000; border: 5px outset; border-color: #22222B } .textBox { height: 150px; color: #ffffff; font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif; padding-left: 1em; } header { background-color: #090B16; width: 100%; border: 6px outset #22222B; } article { margin-top: 5px; margin-bottom: 5px; display: flex; background-color: #090B16; border: 6px outset #22222B; padding: 0 } a:link, a:visited { text-decoration: none; color: #fff; } .logo { padding: 1em; } p { font-size: 12pt; } .image { padding: 0; margin: 0; border: 6px inset #7079B1 } .post { width: 100%; }
解决方案
图片与边框的间隙多因<img>默认是行内元素,会和文字基线对齐导致底部留白,以下几种方法可解决:
方法1:将img设为块级元素
给.image内的img添加样式:
.image img { display: block; }
方法2:修改img的垂直对齐方式
让img对齐顶部/底部,规避基线留白:
.image img { vertical-align: top; /* 或 bottom、middle */ }
方法3:清除a标签默认样式
若间隙来自<a>标签,给.image a设置块级属性并清除内外边距:
.image a { display: block; padding: 0; margin: 0; }
方法4:给.image设置line-height: 0
行高会影响行内元素的垂直空间,设为0可消除留白:
.image { padding: 0; margin: 0; border: 6px inset #7079B1; line-height: 0; }
任选一种即可消除间隙,推荐方法1或2,简洁直接。
内容的提问来源于stack exchange,提问作者user22219074
相关产品推荐
相关产品推荐

