【发布时间】:2014-03-21 21:57:24
【问题描述】:
我正在尝试在 div 中显示文本
<div style='line-height: 16px;'>$text</div>
在我将一些图像放入 $text 之前,16px 的行高是可以的(基本上我用 15px 高的笑脸图像替换了笑脸标志)。在这种情况下,行高变为 19px。 无论是否有图像,如何强制线条保持 16px 高度? (边距、内边距、边框:每个元素为 0px)
这里的例子:
<style type="text/css">
div {
color: black;
font-family: Verdana;
font-size: 12px;
margin: 0px;
padding: 0px;
box-sizing: border-box;
border: none;
position: absolute;
-webkit-box-sizing: border-box;
-o-box-sizing: border-box;
-moz-box-sizing: border-box;}
div.comment_text {
left: 10px;
right: 10px;
line-height: 16px;
overflow: auto;}
</style>
<div class='comment_text' style='top: 20px;'>
the measured line height here 16px which is OK
<br/><a href="mailto:myaddress@gmail.com" target='_blank'>myaddress@gmail.com</a>
<br/><a href="http://www.facebook.com" target='_blank'>http://www.facebook.com</a>
<br/>this
<br/>lines
<br/>are
<br/>for
<br/>example
<br/>to
<br/>demonstrate
<br/>my
<br/>problem
</div>
<div class='comment_text' style='top: 300px;'>
<img class='relative' style='top: 0px; max-height: 16px;' src='../images/smileys/tongue_out.png'/> the measured line-height here 17px which is wrong
<br/>i'm a 15px high image: <img class='relative' style='top: 0px; max-height:16px;' src='../images/smileys/grinning.png'/>
<br/>me too: <img class='relative' style='top: 0px; max-height: 16px;' src='../images/smileys/winking.png'/>
</div>
【问题讨论】:
-
它不会被忽略...只是图像(如果比字体高)会根据需要将其分开。
-
它们并不高。行高:16px,图片高度:15px
-
它们比你的 font-size 高,它是 12pt。 12pt 类型设置为 16pt 行高将在类型上方和下方添加 2pt 空间(16-12 = 4. 4/2 = 上方和下方 2px)。混合了 15 像素的图像,您现在最终会在其上方和下方 2 像素,总共 19 像素 (15 + 2 + 2)。
-
好的,谢谢。那么如果我想保持 12px 字体大小并使用我的图像,有什么解决方案呢?
-
见下面我的回答。我为您发布了一些示例标记。
标签: css