css - 段落中放表情符不影响 `line-height`

标签 css emoji

无论表情有多大,如何在不影响 line-height 的情况下将表情插入段落? IE。喜欢:

enter image description here

我得到的最接近的是 position: absolutevertical-align: text-top,它们都不起作用。

p img {
    height: 35px;
    display: inline;
}
#one img {
    vertical-align: text-top;
}
#two img {
    position: absolute;
}
<p id="one">Marzipan chupa chups marzipan. Bear claw donut candy powder cupcake tart. Tiramisu cotton candy jelly biscuit pie <img src="https://rawgit.com/github/gemoji/master/images/emoji/bowtie.png"> Jelly beans muffin croissant. Cupcake cookie pudding tootsie roll wafer. Bear claw jelly gummies sugar plum bear claw candy chocolate jelly. Donut brownie pie dessert cupcake donut oat cake marshmallow.</p>
<p id="two">Marzipan chupa chups marzipan. Bear claw donut candy powder cupcake tart. Tiramisu cotton candy jelly biscuit pie <img src="https://rawgit.com/github/gemoji/master/images/emoji/bowtie.png"> Jelly beans muffin croissant. Cupcake cookie pudding tootsie roll wafer. Bear claw jelly gummies sugar plum bear claw candy chocolate jelly. Donut brownie pie dessert cupcake donut oat cake marshmallow.</p>

我知道类似的问题herehere ,但是两者都已经过时并且没有正确的答案。

最佳答案

你可以使用

margin-top: [something];
margin-bottom: [something];
vertical-align: middle;

[something](containerLineHeight - imageHeight)/2

另请注意,如果没有左右边距,您可以只使用 margin: [something] 0

例如,由于图像有height: 35px,假设容器有line-height: 20px

margin: -7.5px 0; // (20px - 35px) / 2

p {
  line-height: 20px;
}
p img {
  height: 35px;
  display: inline;
  vertical-align: middle;
  margin: -7.5px 0;
}
<p>Marzipan chupa chups marzipan. Bear claw donut candy powder cupcake tart. Tiramisu cotton candy jelly biscuit pie <img src="https://rawgit.com/github/gemoji/master/images/emoji/bowtie.png"> Jelly beans muffin croissant. Cupcake cookie pudding tootsie roll wafer. Bear claw jelly gummies sugar plum bear claw candy chocolate jelly. Donut brownie pie dessert cupcake donut oat cake marshmallow.</p>

请注意,使用 7.5px 以上的值不会因为 vertical-align: middle 而受到伤害。因此,您可以使用类似

margin: -1000000px 0;

p img {
  height: 35px;
  display: inline;
  vertical-align: middle;
  margin: -1000000px 0;
}
<p>Marzipan chupa chups marzipan. Bear claw donut candy powder cupcake tart. Tiramisu cotton candy jelly biscuit pie <img src="https://rawgit.com/github/gemoji/master/images/emoji/bowtie.png"> Jelly beans muffin croissant. Cupcake cookie pudding tootsie roll wafer. Bear claw jelly gummies sugar plum bear claw candy chocolate jelly. Donut brownie pie dessert cupcake donut oat cake marshmallow.</p>

或者,您可以使用百分比,该百分比将根据生成框的 containing block 的宽度进行计算。 .

因此,假设容器的宽度大于图像的高度,margin: -50% 0 应该足够了。

p img {
  height: 35px;
  display: inline;
  vertical-align: middle;
  margin: -50% 0;
}
<p>Marzipan chupa chups marzipan. Bear claw donut candy powder cupcake tart. Tiramisu cotton candy jelly biscuit pie <img src="https://rawgit.com/github/gemoji/master/images/emoji/bowtie.png"> Jelly beans muffin croissant. Cupcake cookie pudding tootsie roll wafer. Bear claw jelly gummies sugar plum bear claw candy chocolate jelly. Donut brownie pie dessert cupcake donut oat cake marshmallow.</p>

为了更安全,您还可以使用 margin: -1000000% 0

p img {
  height: 35px;
  display: inline;
  vertical-align: middle;
  margin: -1000000% 0;
}
<p>Marzipan chupa chups marzipan. Bear claw donut candy powder cupcake tart. Tiramisu cotton candy jelly biscuit pie <img src="https://rawgit.com/github/gemoji/master/images/emoji/bowtie.png"> Jelly beans muffin croissant. Cupcake cookie pudding tootsie roll wafer. Bear claw jelly gummies sugar plum bear claw candy chocolate jelly. Donut brownie pie dessert cupcake donut oat cake marshmallow.</p>

关于css - 段落中放表情符不影响 `line-height`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822351/

相关文章:

html - 在 div 内对齐内容

javascript - 在 img 大于 div 的不同 div 尺寸中垂直对齐不同的 img 尺寸

css - Div 没有完全扩展 100% 高度

android - 我如何处理 Android 中 Emojipicker 和键盘之间的切换?

Javascript 语法错误 : Unexpected token ILLEGAL when trying to use emoji's UTF code

css - 向网站添加多种 Icomoon 字体

javascript - 编写此代码以使表 td :button I created strikes with a line my listed items? 的正确方法是什么

android - 使用 iOS 键盘输入时表情符号不显示在 NSAttributedString 中,在 Android 上输入时显示

postgresql - 如何在 Postgres 中将 url 编码的表情符号保存为文本(例如 "%F0%9F%98%80")? (罗尔)

php - 苏格兰国旗在我的页面上不起作用,但其他 Emoji 5 可以