【问题标题】:How to Align two p elements displayed inline with different font-size at top?如何对齐在顶部以不同字体大小显示的两个 p 元素?
【发布时间】:2017-04-22 20:35:10
【问题描述】:

我必须内联具有不同字体大小的 p 元素,并希望它们在顶部对齐,以便它们的边缘匹配。

这是如何实现的?

<!doctype html>
<html>
<head>
<style>
body {border:0.1em red solid; width:100%; padding:5em;}
p {display:inline; vertical-align:top}
.big {font-size:9em; }
.text {font-size:2em; }
.container{height:15em;}
</style>
<meta charset="UTF-8">
<title>Text Align</title>
</head>

<body>

<p class="big">x</p><p class="text"> HELLO WORLD</p>


</body>
</html>

更新:

这是一个更真实的示例中的代码。在调整行高时,我无法将它们移到页脚容器的上边缘。

    <!doctype html>
<html>
<head>
<style>
body {border:0.1em red solid; width:600px; padding:1em; }
 p {display:inline-table; vertical-align:top; padding:0em; margin:0em; background-color:red;}
.big {font-size:9em; line-height:35px; padding:0em;}
.text {font-size:2em;}
.footer {background-color:green; padding:0em; line-height:1.7em; height:auto; overflow:hidden;}
.area {background-color:yellow; height:500px; margin-bottom:1em; padding:0em;}
</style>
<meta charset="UTF-8">
<title>Text Align</title>
</head>

<body>
<div class="area">
</div>
<div class="footer" >
<div style="margin:5em auto; display:table;">
<p class="big">x</p>
<p class="text"> HELLO WORLD</p>
</div>
</div>

</body>
</html>

【问题讨论】:

  • 它们都与行框的顶部对齐 - 给它们一个背景,你会看到它。这里的问题是行高,加上 x 当然比其他段落中的那些大写字母要小得多。给第一个 p 大约 0.3 的行高,或者给第二个 5 的行高,你会更接近(!)你可能想要达到的目标。

标签: css vertical-alignment


【解决方案1】:

由于您不能使用 CSS 自动调整它,我建议您手动调整。

更多详情on the reason see this link

使用line-height: 49px; 或line-height: .35em; 匹配这些p 元素

<!doctype html>
<html>
<head>
<style>
body {border:0.1em red solid; width:100%; padding:5em;}
p {display:inline; vertical-align:top}
.big {font-size:9em; line-height: 49px; }
.text {font-size:2em; }
.container{height:15em;}
</style>
<meta charset="UTF-8">
<title>Text Align</title>
</head>

<body>

<p class="big">x</p><p class="text"> HELLO WORLD</p>


</body>
</html>

更新:-

将margin-top:0; 添加到.text 和padding:0em 1em 1em 1em; .big。因为line-height会影响.big的高度

<!doctype html>
<html>
<head>
<style>
body {border:0.1em red solid; width:600px; padding:1em; }
 p {display:inline-block; vertical-align:top;}
.big {font-size:9em; padding-top:0em; margin:0em; line-height:0.35em}
.text {font-size:2em;margin-top:0;}
.footer {background-color:green; overflow:hidden; padding:0em 1em 1em 1em;}
.area {background-color:yellow; height:500px; margin-bottom:1em; padding:0em;}
</style>
<meta charset="UTF-8">
<title>Text Align</title>
</head>

<body>
<div class="area">
</div>
<div class="footer" >
<p class="big">x</p>
<p class="text"> HELLO WORLD</p>
</div>

</body>
</html>

【讨论】:

  • 是的,没有其他办法。因为如果这是最严重的错误,您可以使用一些 javascript 进行调整。因为这是line-height 我猜只能手动调整。原因see this link
  • 谢谢,但我如何让两者都到达容器 div 的上边缘?我更新了问题以表明我的意思。调整 line-height 效果很好,但它把两个元素都推得太低了。
  • 我添加了一个新的 sn-p。并坚持.35em 我觉得它是平等的。
  • 我再次更新了我的第二个代码块,我注意到的是,即使使用您的第二个解决方案,它也只能在 chorme 和 safari 中使用,但在 Firefox 中不起作用。感谢您的耐心等待
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 2013-09-02
  • 1970-01-01
相关资源
最近更新 更多