CSS中如何对齐两列文本

发布于 2024-11-09 07:20:13 字数 331 浏览 0 评论 0原文

我在排列一些文本时遇到一些问题。我需要两列,一列包含数字,一列包含文本,如下所示:

1 个条目
2 条目二
3 条目三
4 条目五
5 条目六

左栏是Georgia,右栏是Arial(字体大小略有不同)。我可以为每一行设置一个容器 div,并将数字和文本段落绝对定位在这些段落的顶部或底部,这样效果很好。问题是,这意味着我必须给每一行一个固定的高度,以便它正确显示,如果文本需要流到不止一行,这会导致问题(由于文本条目是动态的,因此很可能会这样做)。

我想在不使用表格的情况下执行此操作,并且不使用绝对定位,以便各个文本条目可以跨越多行(并且跨浏览器兼容)。

I'm having some trouble lining up some text. I need two columns, one with numbers and one with text, like so:

1 Entry one
2 Entry two
3 Entry three
4 Entry five
5 Entry six

The left column is Georgia and the right column is Arial (slightly different font sizes). I could have a container div for each row and absolutely position the number and text paragraphs to be at the top or bottom of these, which works fine. The problem is this means I have to give each row a fixed height so that it displays properly, which causes a problem if the text needs to flow onto more than one line (which it may well do as the text entries are dynamic).

I want to do this without using a table, and without using absolute positioning so that the individual text entries can span over more than one line (and is cross-browser compatible).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-11-16 07:20:13

根据我的评论,我认为这项工作的最佳元素是 有序列表

ol {
   font-family: georgia, serif;
   font-size: 16px;
   font-weight: bold;
}
ol li span {
   font-family: arial, sans-serif;
   font-weight: normal;
   font-size: 12px;
}
<ol>
  <li><span>Entry one<br>text on another line</span></li>
  <li><span>Entry two</span></li>
  <li><span>Entry three</span></li>
  <li><span>Entry five</span></li>
  <li><span>Entry six</span></li>
</ol>

通过允许在列表“项目符号”和其中的内容之间更改 font-family 的跨度,如果您有块内容,这些可能是 div。

As per my comment, I think the best element for the job is an ordered list.

ol {
   font-family: georgia, serif;
   font-size: 16px;
   font-weight: bold;
}
ol li span {
   font-family: arial, sans-serif;
   font-weight: normal;
   font-size: 12px;
}
<ol>
  <li><span>Entry one<br>text on another line</span></li>
  <li><span>Entry two</span></li>
  <li><span>Entry three</span></li>
  <li><span>Entry five</span></li>
  <li><span>Entry six</span></li>
</ol>

With the span to allow changing of font-family between the list "bullets" and the content within, these could be divs if you have block content.

内心旳酸楚 2024-11-16 07:20:13

您应该只使用适当样式的 ol 元素,如下所示:

See: http://jsfiddle.net/tPjQR/

如果您希望数字与列表内容具有不同的样式,则需要将每个 li 的内容包装在类似 span 的东西。只是没有更好的办法了。

ol {
    font-family: Georgia, serif;
}
ol span {
    font-family: Arial, sans-serif;
    font-size: 17px
}
<ol>
    <li><span>Entry one</span></li>
    <li><span>Entry two</span></li>
    <li><span>Entry three</span></li>
    <li><span>Entry five</span></li>
    <li><span>Entry six</span></li>
    <li><span>Entry Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long</span></li>
</ol>

You should just use an appropriately styled ol element, something like this:

See: http://jsfiddle.net/tPjQR/

If you want to have different styles on the number versus the list content, you'll need to wrap the content of each li in something like a span. There just isn't a better way.

ol {
    font-family: Georgia, serif;
}
ol span {
    font-family: Arial, sans-serif;
    font-size: 17px
}
<ol>
    <li><span>Entry one</span></li>
    <li><span>Entry two</span></li>
    <li><span>Entry three</span></li>
    <li><span>Entry five</span></li>
    <li><span>Entry six</span></li>
    <li><span>Entry Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long</span></li>
</ol>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文