8

有没有办法将不同大小的多个标题的第一行对齐到同一基线?也不管后面的文字,也应该对齐。

请参阅http://snapplr.com/snap/z1mw上的图片

编辑:重新上传:

替代文字 http://img144.imageshack.us/img144/7615/screenshot2010021722h53.png

在我看来,唯一的解决方案是将每个标题和每个正文文本放入单独的 DIV 中,然后将标题与 padding-top 或 margin-top 一起使用以对齐它们(例如,H1 将是 36px 与 0px 边距,而 H3 将是24 像素,上边距 12 像素)。像这样的东西:

<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>

但这不是很好的解决方案。请问有更好的吗?

非常感谢!

4

2 回答 2

4

Web 处理基线的方式与您在打印中习惯的方式不同。浏览器会将您的文本对齐到行高的中心,而不是底部。这意味着与 Web 网格对齐的文本相对于彼此垂直居中,而不是位于同一基线上。

如果您对此感到满意,您仍然可以从中获得强大的网格。你只需要精确地结合使用行高、填充、边框和边距。这需要一些数学,但完全有可能。更多在这里:

http://24ways.org/2006/compose-to-a-vertical-rhythm/

如果您需要的东西实际上与您在印刷中所知道的基线对齐,那么游戏会更加困难。问题是您将需要根据确切的字体推送不同数量的内容 - 当每个浏览器可能使用不同的字体时几乎不可能。这是使之成为可能的一种尝试:

http://baselinecss.com/

无论哪种解决方案都存在跨浏览器问题。网络还没有为此提供适当的工具。但在任何情况下,您都不应该需要 div。您可以对标题本身进行所有调整。在您的示例中,您甚至不需要它们。为什么 div 在那里?

于 2010-03-03T01:04:23.593 回答
-1

只是一个快速的想法(如“我有这个想法,这可能有效,但我没有测试它”):使用 line-height 怎么样?您可以对所有标题使用相同的行高,从而确保所有行的基线一致。

问候杰斯珀豪格

于 2010-02-23T23:16:32.663 回答