使用 CSS 在表格单元格中右对齐

发布于 2024-08-15 16:49:41 字数 198 浏览 9 评论 0原文

我有像这样的旧经典代码,

<td align="right">

它的作用如下:它正确对齐单元格中的内容。 因此,如果我在此单元格中放置两个按钮,它们将出现在单元格的正确位置。

我正在将其重构为 CSS,但似乎没有任何右对齐的内容。我看到text-align,但是是一样的吗?

I have the old classic code like this,

<td align="right">

which does what it says: it right aligns the content in the cell.
So if I put two buttons in this cell, they will appear at the right site of the cell.

I was refactoring this to CSS, but there doesn't seem to be anything as right align. I see text-align, but is it that the same?

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

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

发布评论

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

评论(4

残月升风 2024-08-22 16:49:41

使用

text-align: right

text-align CSS属性描述
内联内容(如文本)如何
与其父块元素对齐。
文本对齐不控制
块元素本身的对齐方式,
仅其内联内容。

请参阅

文本对齐

<td class='alnright'>text to be aligned to right</td>

<style>
    .alnright { text-align: right; }
</style>

Use

text-align: right

The text-align CSS property describes
how inline content like text is
aligned in its parent block element.
text-align does not control the
alignment of block elements itself,
only their inline content.

See

text-align

<td class='alnright'>text to be aligned to right</td>

<style>
    .alnright { text-align: right; }
</style>
浅忆 2024-08-22 16:49:41

不要忘记 CSS3 的“nth-child”选择器。如果您知道希望将文本右对齐的列的索引,则只需指定

table tr td:nth-child(2) {
    text-align: right;
}

在大型表的情况下,这可以为您节省大量额外的标记!

这是给你的小提琴...... https://jsfiddle.net/w16c2nad/

Don't forget about CSS3's 'nth-child' selector. If you know the index of the column you wish to align text to the right on, you can just specify

table tr td:nth-child(2) {
    text-align: right;
}

In cases with large tables this can save you a lot of extra markup!

here's a fiddle for ya.... https://jsfiddle.net/w16c2nad/

黄昏下泛黄的笔记 2024-08-22 16:49:41

现在对我有用的是:

CSS:

.right {
  text-align: right;
  margin-right: 1em;
}

.left {
  text-align: left;
  margin-left: 1em;
}

HTML:

<table width="100%">
  <tbody>
    <tr>
      <td class="left">
        <input id="abort" type="submit" name="abort" value="Back">
        <input id="save" type="submit" name="save" value="Save">
      </td>
      <td class="right">
        <input id="delegate" type="submit" name="delegate" value="Delegate">
        <input id="unassign" type="submit" name="unassign" value="Unassign">
        <input id="complete" type="submit" name="complete" value="Complete">
      </td>
    </tr>
  </tbody>
</table>

请参阅以下小提琴:

http:// /jsfiddle.net/Joysn/3u3SD/

What worked for me now is:

CSS:

.right {
  text-align: right;
  margin-right: 1em;
}

.left {
  text-align: left;
  margin-left: 1em;
}

HTML:

<table width="100%">
  <tbody>
    <tr>
      <td class="left">
        <input id="abort" type="submit" name="abort" value="Back">
        <input id="save" type="submit" name="save" value="Save">
      </td>
      <td class="right">
        <input id="delegate" type="submit" name="delegate" value="Delegate">
        <input id="unassign" type="submit" name="unassign" value="Unassign">
        <input id="complete" type="submit" name="complete" value="Complete">
      </td>
    </tr>
  </tbody>
</table>

See the following fiddle:

http://jsfiddle.net/Joysn/3u3SD/

追我者格杀勿论 2024-08-22 16:49:41

如何在 td 单元格中定位块元素

提供的答案对于在 td 单元格中右对齐文本非常有用。

当您希望按照接受的答案中的注释对齐块元素时,这可能不是解决方案。为了使用块元素实现这一点,我发现使用边距很有用;

一般语法

selector {
  margin: top right bottom left;
}

右对齐

td > selector {
/* there is a shorthand, TODO!

How to position block elements in a td cell

The answers provided do a great job to right-align text in a td cell.

This might not be the solution when you're looking to align a block element as commented in the accepted answer. To achieve such with a block element, I have found it useful to make use of margins;

General syntax

selector {
  margin: top right bottom left;
}

Justify right

td > selector {
  /* there is a shorthand, TODO! ???? */
  margin: auto 0 auto auto;
}

Justify center

td > selector {
  margin: auto auto auto auto;
}

/* or the short-hand */
margin: auto;

Align center

td > selector {
  margin: auto;
}

JSFiddle example

Alternatively, you could make you td content display inline-block if that's an option, but that may distort the position of its child elements.

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