Align Right In A Table Cell With CSS - Stack Overflow

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Align right in a table cell with CSS Ask Question Asked 14 years, 11 months ago Modified 1 year, 8 months ago Viewed 437k times 182

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?

Share Improve this question Follow edited Mar 12, 2023 at 19:11 Peter Mortensen's user avatar Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Dec 15, 2009 at 10:22 Michel's user avatar MichelMichel 23.5k46 gold badges157 silver badges252 bronze badges Add a comment |

4 Answers 4

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 231

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> Share Improve this answer Follow answered Dec 15, 2009 at 10:24 rahul's user avatar rahulrahul 187k50 gold badges237 silver badges264 bronze badges 6
  • 5 is a <input type="button"> a block element because that one doesn't get aligned right? – Michel Commented Dec 15, 2009 at 10:38
  • 1 It depends. I have a paragraph, which is block, inside a table cell (css display: table-cell), and if I give that paragraph a width of 100% it starts to respect text-align right. I assume defining a width isn't always the best thing. – Costa Michailidis Commented Mar 26, 2013 at 18:50
  • 3 I take it then, that the property text-align is not very well named if it applies to buttons and controls as well as text. Perhaps this should have been called content-align? – Ben Commented Feb 26, 2014 at 10:10
  • 5 Michel: set the block element to inline-block, e.g.: td input { display:inline-block; } – shalamos Commented Nov 14, 2014 at 13:15
  • 1 or me only works float:right or good old align="right". w t f ? – Tone Škoda Commented Feb 20, 2015 at 1:12
| Show 1 more comment 16

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/

Share Improve this answer Follow answered Jul 22, 2017 at 1:49 Suite 404 not found's user avatar Suite 404 not foundSuite 404 not found 1771 silver badge3 bronze badges 1
  • 1 Great: easiest and cleanest solution I saw. – ncrypticus Commented Nov 24, 2017 at 20:42
Add a comment | 10

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.

Share Improve this answer Follow edited Mar 12, 2023 at 19:14 Peter Mortensen's user avatar Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges answered Nov 22, 2019 at 13:06 John Mutuma's user avatar John MutumaJohn Mutuma 3,5702 gold badges21 silver badges33 bronze badges Add a comment | 10

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/

Share Improve this answer Follow edited Mar 12, 2023 at 19:15 Peter Mortensen's user avatar Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges answered Sep 1, 2012 at 20:52 Joysn's user avatar JoysnJoysn 1,0291 gold badge19 silver badges35 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • The app that fights for your data privacy rights
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

17 How to right align last two columns of a table with CSS 4 Table cells right align inside row 2 How to align the table in CSS 0 How to make text in a table cell locate a little right of the centre 18 HTML table column align right 4 How to right align text in table cell with <col/> 2 Right Align Table? 0 html - align table column right 0 Align table cells to the left 10 How to get a table-cell aligned to the right? 21 How can I align entire table to right with CSS? 0 Right align Text in table cell with CSS 0 Right-align table-cell div position 4 Table cells right align inside row

Hot Network Questions

  • Phrase out of figures
  • Missile Impact Velocity
  • Why Adam and Eve were created naked?
  • H-bridge transistors are too hot
  • Will Spirit trade with SAVEQ when it recovers?
  • Has -な become more common recently
  • Accused of violating NDA on thesis
  • Movie about a post apocalyptic world with a scorching hot sun
  • Does the earliest known use of an "average" occur after the invention of calculus?
  • Strange Brackets in String Writing
  • Can you make 5 x 3 “magic” rectangles?
  • Do referees get to see each other's reports?
  • An idiom similar to 'canary' or 'litmus test' that expresses the trend or direction a thing is pointed
  • How to reconstruct the vol surface given Level, Slope and Curvature
  • Does the twin paradox hold in a universe that's empty except for the twins?
  • How can I solve my equation with the best numerical precision?
  • I need some turkey broth to keep my turkey leftovers from drying out. How can I make it so it doesn't become gelatinous stock?
  • A dark animated movie about an orphaned girl working in an oppressive factory
  • Simplifying Exp[I Mod[x, 2π]] to Exp[I x]
  • Disabling a cart while moving
  • What if someone comits murder when they are younger but weren't caught till they got a mental disease like dementia?
  • Is it problematic to use percentages to describe a sample with less than 100 people?
  • Hollow shape produced by Geometry Nodes is filled-in when sliced in Creality Print
  • Short story about a man living In an apartment who's curious about his neighbor who turns out to be a monster or demon
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

default

Từ khóa » Html Td Align Right