How To Center The Contents Of An HTML Table? - Stack Overflow

    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 How to center the contents of an HTML table? Ask Question Asked 12 years, 11 months ago Modified 9 months ago Viewed 1.2m times 361

I am using an HTML <table> and I want to align the text of <td> to the center in each cell.

How do I center align the text horizontally and vertically?

Share Improve this question Follow edited May 28, 2021 at 15:22 TylerH's user avatar TylerH 21.2k76 gold badges79 silver badges110 bronze badges asked Jan 24, 2012 at 11:43 Rajagopal 웃's user avatar Rajagopal 웃Rajagopal 웃 8,4717 gold badges33 silver badges44 bronze badges Add a comment |

10 Answers 10

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

Here is an example with CSS and inline style attributes:

td { height: 50px; width: 50px; } #cssTable td { text-align: center; vertical-align: middle; } <table border="1"> <tr> <td style="text-align: center; vertical-align: middle;">Text</td> <td style="text-align: center; vertical-align: middle;">Text</td> </tr> </table> <table border="1" id="cssTable"> <tr> <td>Text</td> <td>Text</td> </tr> </table>

http://jsfiddle.net/j2h3xo9k/

EDIT: The valign attribute is deprecated in HTML5 and should not be used.

Share Improve this answer Follow edited Jun 11, 2018 at 15:12 Sean the Bean's user avatar Sean the Bean 5,3825 gold badges39 silver badges40 bronze badges answered Jan 24, 2012 at 11:49 David Laberge's user avatar David LabergeDavid Laberge 16k15 gold badges55 silver badges84 bronze badges 3
  • 1 Isn't the vertical-align:middle supposed to be applied to the tr element? – posfan12 Commented Jun 10, 2018 at 9:00
  • 2 If you put an img tag in the same td didnt work – IgniteCoders Commented Jul 16, 2020 at 18:45
  • @posfan12 TLDR: no. Explanation: td elements (under normal circumstances) have the same height as their parent tr element, so changing the vertical alignment css property of the tr element will do nothing at all. There is no "available" space above or below the td elements. – Skeets Commented Sep 7, 2022 at 5:55
Add a comment | 78

The CSS to center text in your td elements is

td { text-align: center; vertical-align: middle; } Share Improve this answer Follow edited Sep 8, 2022 at 11:50 Lee Goddard's user avatar Lee Goddard 11.1k5 gold badges50 silver badges68 bronze badges answered Jan 24, 2012 at 11:48 Carsten's user avatar CarstenCarsten 18.4k4 gold badges51 silver badges55 bronze badges Add a comment | 55

HTML in line styling example:

<td style='text-align:center; vertical-align:middle'></td>

CSS file example:

td { text-align: center; vertical-align: middle; } Share Improve this answer Follow edited Feb 10, 2021 at 0:08 Brandon Andre's user avatar Brandon Andre 174 bronze badges answered Jan 24, 2012 at 11:49 Hayden Thring's user avatar Hayden ThringHayden Thring 1,79017 silver badges26 bronze badges Add a comment | 43

Try to put this in your CSS file.

td { text-align: center; vertical-align: middle; } Share Improve this answer Follow edited Sep 8, 2022 at 11:50 Lee Goddard's user avatar Lee Goddard 11.1k5 gold badges50 silver badges68 bronze badges answered Jan 24, 2012 at 11:47 Boris's user avatar BorisBoris 7498 silver badges22 bronze badges Add a comment | 20 <td align="center" valign="center">textgoeshere</td>

Is the only correct answer imho, since you're working with tables which is old functionality most common used for e-mail formatting. So your best bet is to not use just style but inline style and known table tags.

Share Improve this answer Follow edited Aug 2, 2022 at 10:52 answered Jun 15, 2013 at 19:18 Remi's user avatar RemiRemi 5,33713 gold badges60 silver badges94 bronze badges 1
  • 12 valign is deprecated in HTML5. Do not use it. For email formatting, a style tag or an inline style attribute would be the best solution. – Sean the Bean Commented Jun 11, 2018 at 15:02
Add a comment | 3

Selector > child:

.text-center-row>th, .text-center-row>td { text-align: center; } <table border="1" width='500px'> <tr class="text-center-row"> <th>Text</th> <th>Text</th> <th>Text</th> <th>Text</th> </tr> <tr> <td>Text</td> <td>Text</td> <td>Text</td> <td>Text</td> </tr> <tr class="text-center-row"> <td>Text</td> <td>Text</td> <td>Text</td> <td>Text</td> </tr> </table>

Share Improve this answer Follow edited Nov 27, 2019 at 14:16 answered Nov 27, 2019 at 8:20 Fernando's user avatar FernandoFernando 1291 silver badge7 bronze badges Add a comment | 3

If you are using Bootstrap, you can use inbuilt class

<table class="text-center align-middle"> Share Improve this answer Follow answered Sep 24, 2021 at 5:34 MohitGhodasara's user avatar MohitGhodasaraMohitGhodasara 2,7121 gold badge24 silver badges29 bronze badges Add a comment | 1

td { height: 50px; width: 50px; } #cssTable td { text-align: center; vertical-align: middle; } <table border="1"> <tr> <td style="text-align: center; vertical-align: middle;">Text</td> <td style="text-align: center; vertical-align: middle;">Text</td> </tr> </table> <table border="1" id="cssTable"> <tr> <td>Text</td> <td>Text</td> </tr> </table>

Share Improve this answer Follow answered Mar 24, 2024 at 10:15 Vinay Bavaraju's user avatar Vinay BavarajuVinay Bavaraju 111 bronze badge Add a comment | 0

The following worked for me to vertically align content (multi-line) in a list-table

.. list-table:: :class: longtable :header-rows: 1 :stub-columns: 1 :align: left :widths: 20, 20, 20, 20, 20 * - Classification - Restricted - Company |br| Confidential - Internal Use Only - Public * - Row1 col1 - Row1 col2 - Row1 col3 - Row1 col4 - Row1 col5

Using theme overrides .css option I defined:

.stub { text-align: left; vertical-align: top; }

In the theme that I use 'python-docs-theme', the cell entry is defined as 'stub' class. Use your browser development menu to inspect what your theme class is for cell content and update that accordingly.

Share Improve this answer Follow edited Feb 5, 2021 at 7:14 Dharman's user avatar Dharman 33.2k27 gold badges100 silver badges146 bronze badges answered Feb 5, 2021 at 7:08 shalz's user avatar shalzshalz 811 silver badge2 bronze badges Add a comment | -3

<td align="center"valign="center">textgoeshere</td>

more on valign

Share Improve this answer Follow edited Jan 24, 2012 at 12:08 answered Jan 24, 2012 at 11:51 Balaswamy Vaddeman's user avatar Balaswamy VaddemanBalaswamy Vaddeman 8,5103 gold badges32 silver badges40 bronze badges 2
  • 15 valign attribute is not supported in HTML5. Use CSS instead. – T30 Commented Feb 20, 2014 at 10:02
  • align attribute is not supported in HTML 5. You have to use CSS instead. – Asiqur Rahman Commented Aug 2, 2022 at 10:58
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
  • Developers want more, more, more: the 2024 results from Stack Overflow’s...
  • How AI apps are like Google Search
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries

Linked

45 How to align td elements in center -1 Center contents of table cell horizontally AND vertically? 1 CSS- Center elements inside td 0 Centering text in a <td> column on a PHP SQL display 2 Table: align text by column 3 Grid table cells aligning incorrectly in reStructuredText using Sphinx Read the Docs theme -1 How to center table data 0 how to add dynamic latitude and longitude to ifram in html table row 0 Center a table in the page 8 center table in HTML 0 center aligning a table 29 Center a Table inside a TD 0 I can't seem to center this table 1 Center a table using html and css 0 How can I center the text in a table? 1 HTML Table Centering 0 Centering a column in a table 0 Need help centering a table in HTML

Hot Network Questions

  • Is it possible to discover a "Universal formula" that generates and generalizes all odd Collatz numbers?
  • Unable to log into alternative unprivileged user with default GNOME shell on Edubuntu 24.04 — sudoer (uid=1000) works
  • Download a file with SSH/SCP, tar it inline and pipe it to openssl
  • What is Application Manager for macOS, and what does it do?
  • meaning of "last time out"
  • Did a peaceful reunification of a separatist state ever happen?
  • Why did Herod want to know the time of appearance of the Star of Bethlehem?
  • Connections between the path integral formulation and the Fourier transform
  • Measuring Hubble expansion in the lab
  • What religious significance does the fine tuning argument have?
  • What is this FreeDOS kernel loader found on the “W3x4NTFS” disk image?
  • Is the "wavefunction collapse" interpretation consistent with relativity?
  • Is this position possible to have been made legally?
  • Would a thermometer calibrated for water also be accurate for measuring the air temperature (or vice versa)?
  • Helping daughter with taxes - How do I report some freelance work for her?
  • Mama’s cookies too dry to bake
  • Advice for creating a clean table with tabularray
  • On iOS, can i move or copy a file from "Notes"to "Files"?
  • Why did my pancake stick to my pan?
  • Is "Klassenarbeitsangst" a real word? Does it accord with general rules of compound noun formation?
  • Why don't bicycles have the rear sprocket OUTSIDE of the frame spacing? (Single speed)
  • If God is good, why does "Acts of God" refer to bad things?
  • How many hours of daylight can a planet in an elliptical orbit receive?
  • No other coauthors, just me and my supervisors—is this normal?
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.

lang-html

Từ khóa » Html Column Align Center