align right in a table cell with CSS

后端 未结 4 1365
走了就别回头了
走了就别回头了 2020-12-09 00:27

I have the old classic code like this


which does what it says: it right aligns the content in the cell. So if I

相关标签:
4条回答
  • 2020-12-09 00:53

    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>
    
    0 讨论(0)
  • 2020-12-09 01:00

    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/

    0 讨论(0)
  • 2020-12-09 01:02

    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/

    0 讨论(0)
  • 2020-12-09 01:16

    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 {
      /* there is a shorthand, TODO!                                                                     
    0 讨论(0)
提交回复
热议问题