对齐文本。使用表格或 CSS 或  ?

发布于 2024-09-11 16:55:50 字数 288 浏览 8 评论 0原文

我经常对齐文本,例如:

To:07/02/2010
来自: 07/02/2010

我希望它显示如下:

To:   07/02/2010
From: 07/02/2010

那么最快/最简单/最好的方法是什么? CSS?使用一些nbsp(如果是单间距就可以)或使用表格。通常,如果我没有反讨厌桌子的心情,我就会使用桌子。

你有什么建议吗?

Frequently I am aligning text such as:

To: 07/02/2010
From: 07/02/2010

Id like it to be displayed like this:

To:   07/02/2010
From: 07/02/2010

So what is the quickest/easiest/best way to do this? CSS? using a few nbsp (would work if its mono spacing) or using tables. Normally if I am not in a anti-hating table mood, ill use tables.

What do you recommend?

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

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

发布评论

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

评论(6

忘你却要生生世世 2024-09-18 16:55:50

绝对定义列表 (

)

<dl>
    <dt>From:</dt><dd>07/02/2010</dd>
    <dt>To:</dt><dd>07/02/2010</dd>
</dl>

/* CSS */

dl {
    overflow: hidden;
}

dt {
    width: 50px;
    float: left;
}

Definitely definition list (<dl>).

<dl>
    <dt>From:</dt><dd>07/02/2010</dd>
    <dt>To:</dt><dd>07/02/2010</dd>
</dl>

/* CSS */

dl {
    overflow: hidden;
}

dt {
    width: 50px;
    float: left;
}
待天淡蓝洁白时 2024-09-18 16:55:50

我会推荐桌子。这确实是最好的方法,特别是因为它确实是表格数据,并且 HTML 不支持制表位。

但为了避开桌子而避开桌子确实很愚蠢。除非您希望稍后的选项像这样设置样式:

To:          From:
07/02/2010   07/02/2010

如果由于某种原因您不想使用表格,您可以这样做:

CSS

.sideheading { width: 3em; float: left; }

HTML

<div class="sideheading">To:</div>07/02/2010
<div class="sideheading">From:</div>07/02/2010

或者使用定义列表(但如果您避免表的原因是由于语义,那么对于同一件事将避免 DL)。

但当然,这是关于布局的,没有客户或网络冲浪者会关心你是如何做的,只要他们能读懂它!

I'd recommend tables. It really is the best way, especially seeing as it really is tabular data there, and HTML doesn't support tab stops.

But it really is silly to avoid tables for the sake of avoiding tables. Unless you want the option later to style like so:

To:          From:
07/02/2010   07/02/2010

You could do something like this, if for some reason you didn't want to use tables:

CSS

.sideheading { width: 3em; float: left; }

HTML

<div class="sideheading">To:</div>07/02/2010
<div class="sideheading">From:</div>07/02/2010

Or use a definition list (but if the reason you are avoiding tables is due to semantics, then DLs would be avoided for the same thing).

But of course, it's about the layout, no customer or web surfer is ever going to care how you do it, as long as they can read it!

对风讲故事 2024-09-18 16:55:50

使用定义列表或空格 nowrap。

Use a definition list or white-space nowrap.

恍梦境° 2024-09-18 16:55:50

我以前见过这个问题,快速谷歌搜索:

http://www.google .com/search?q=css+forms

...把​​我带到这里:

http://www.webcredible.co.uk/user-Friendly-resources/css/css-forms.shtml

...我将 HTML 和 CSS 复制粘贴到此:

<html>
    <head>
        <style>
        label
        {
            width: 5em;
            float: left;
            text-align: right;
            margin-right: 1em;
            display: block
        }

        .submit input
        {
            margin-left: 4.5em;
        }
        </style>
    </head>
    <body>
        <form action="#">
            <p><label for="name">Name</label> <input type="text" id="name" /></p>
            <p><label for="e-mail">E-mail</label> <input type="text" id="e-mail" /></p>
            <p class="submit"><input type="submit" value="Submit" /></p>
        </form> 
    </body>
</html>

我觉得不错,将其保存为 .html 并亲自查看。

I've seen this problem before, a quick google search:

http://www.google.com/search?q=css+forms

...brought me here:

http://www.webcredible.co.uk/user-friendly-resources/css/css-forms.shtml

...and I copypasted the HTML and CSS into this:

<html>
    <head>
        <style>
        label
        {
            width: 5em;
            float: left;
            text-align: right;
            margin-right: 1em;
            display: block
        }

        .submit input
        {
            margin-left: 4.5em;
        }
        </style>
    </head>
    <body>
        <form action="#">
            <p><label for="name">Name</label> <input type="text" id="name" /></p>
            <p><label for="e-mail">E-mail</label> <input type="text" id="e-mail" /></p>
            <p class="submit"><input type="submit" value="Submit" /></p>
        </form> 
    </body>
</html>

Looks good to me, save it in a .html and see for yourself.

等你爱我 2024-09-18 16:55:50

使用  s 进行填充听起来很混乱。像这样的事情怎么样:

<span class="header">To:</span> 07/02/2010
<span class="header">From:</span> 07/02/2010

.header { display: inline-block; width: 5em;}

不过,在这种情况下,我实际上会说表格是合适的;它看起来确实像表格数据,带有一列标题。

Padding with  s sounds messy. How about something like this:

<span class="header">To:</span> 07/02/2010
<span class="header">From:</span> 07/02/2010

.header { display: inline-block; width: 5em;}

In this case, though, I'd actually say tables are appropriate; it does look like tabular data, with a column of headers.

风为裳 2024-09-18 16:55:50

这在工作中出现过很多次,我最终为隐藏边框的 2 列表创建了一些样式。 技术上,这是表格数据,但考虑到在规范内实现它所需的标记量,只有 2 行和 2 列的表格相当蹩脚。

我经常后悔创建这个类,因为现在每个人都使用它太多了,我必须在代码审查中不断地寻找它。如果您没有预见到这个问题,那么这是一个语义上正确的解决方案,并且比您使用 DL、跨度等跳过的麻烦稍微优雅一些​​。

This has come up at work many times and I ended up creating some styling for a 2-column table which hides borders. Technically, this is tabular data, but a table with only 2 rows and 2 columns is pretty lame considering the amount of markup needed to achieve it within spec.

I've often regretted creating the class, as now everyone uses it far too much and I have to constantly be on the lookout for it in our code reviews. If you don't anticipate that problem, it's a semantically-correct solution, and slightly more elegant than the hoops you'll jump through with DL's, spans, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文