【发布时间】: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 的行高,你会更接近(!)你可能想要达到的目标。