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/

相关文章:

javascript - 如何从相对 URL 获取 Css 背景图像 URL 文本?

filter - 有什么理由在投影上使用 css box-shadow 吗?

css - 如何正确地包含带有 @import 声明的 CSS 文件并同时使用 Bundling。

Chrome 中的 jQuery 后台附件

php - 表情符号显示为?在MySQL数据库中

dart - 在 Flutter 上显示带有表情符号的文本

android - 使用正则表达式解析 iOS 6 表情符号

delphi - 从 Delphi 代码中调出表情符号面板

ios - 动态添加图像到 uitextview 的各个位置

html - 图像在 CSS3 高度过渡时模糊