有没有办法将不同大小的多个标题的第一行对齐到同一基线?也不管后面的文本,也应该对齐。
请参阅http://snapplr.com/snap/z1mw上的图片
编辑:重新上传:
alt text http://img144.imageshack.us/img144/7615/screenshot2010021722h53.png
在我看来,唯一的解决方案是将每个标题和每个正文文本放入单独的DIV中,然后使用标题来填充顶部或边缘顶部以对齐它们(例如,H1将是36px,0px边距,而H3将是24px,12px的上边距)。像这样:
<html>
<head>
<style type="text/css" media="all">
div {
width: 240px;
float: left;
}
h1 {
font-size: 36px;
margin: 0;
padding: 0;
}
h3 {
font-size: 24px;
margin: 0;
padding: 10px 0 0 0; /*for some reason I must use 10px instead of 12px to align. Why??*/
}
</style>
</head>
<body>
<div>
<h1>H1 heading</h1>
</div>
<div>
<h3>H3 heading</h3>
</div>
<div>
<h3>H3 heading that is somewhat longer and spans multiple lines</h3>
</div>
<div style="clear:both;"></div>
<div>
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
</div>
<div>
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
</div>
<div>
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
</div>
</body>
但这不是一个很好的解决方案。请问有什么好的吗?
非常感谢!
答案 0 :(得分:4)
网页处理基线的方式与您以前用于打印的方式不同。浏览器会将文本与行高的中心对齐,而不是与底部对齐。这意味着与网格对齐的文本相对于彼此垂直居中,而不是坐在相同的基线上。
如果你对此感到满意,你仍然可以从中获得强大的网格。您必须精确地结合使用行高,填充,边框和边距。这需要一些数学,但完全有可能。更多信息:
http://24ways.org/2006/compose-to-a-vertical-rhythm/
如果您需要实际与基线对齐的东西,就像您在印刷中所知,那么游戏就更难了。问题是你需要根据确切的字体推送不同数量的东西 - 当每个浏览器使用不同的字体时几乎不可能。以下是尝试实现这一目标的一种尝试:
使用任一解决方案都存在跨浏览器问题。网络还没有为此提供适当的工具。但在任何情况下你都不需要div。您可以对标题本身进行所有调整。在你的例子中你甚至不需要它们。为什么那里有div?
答案 1 :(得分:-1)
只是一个快速思考(如“我有这个想法,那可能有效,但我没有测试它”):如何使用行高?您可以对所有标题使用相同的行高,从而确保所有行的基线均匀。
此致 Jesper Hauge