How To Make A Table With Equal Column Widths In CSS?

    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.

    Explore Teams Create a free Team
  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 make a table with equal column widths in CSS? Ask Question Asked 10 years, 5 months ago Modified 1 year, 5 months ago Viewed 151k times 88

I have a document containing a table like this:

<table> <tr> <td> Word </td> <td> Definition </td> </tr> <tr> <td> Word </td> <td> Definition </td> </tr> <tr> <td> Word </td> <td> Definition </td> </tr> </table>

Using CSS, I need to set all of the cells to 50% width, with text in the left "column" being right-aligned and text in the left column being left-aligned. I've tried many different options, e.g. width: 50%; text-align: left, but the results are always unusual. The results should look like this:

_________________________ | word | definition | |____________|____________| | word | definition | |____________|____________|
  • I cannot adjust the table's actual code directly. I can only modify the CSS style.
  • The answer in Make columns of equal width in <table> is not suitable, because I do not know the width of the page, and that width might vary.

How can I set equal-width cells in a two-column table in CSS with everything aligned to the middle?

Share Improve this question Follow edited Nov 11, 2018 at 10:00 Brian Tompsett - 汤莱恩's user avatar Brian Tompsett - 汤莱恩 5,83772 gold badges60 silver badges132 bronze badges asked Feb 1, 2014 at 7:58 Village's user avatar VillageVillage 23.8k50 gold badges125 silver badges166 bronze badges 4
  • 1 Do you have a fiddle with your attempts? Setting fixed widths and the text alignment on the cells should work – helion3 Commented Feb 1, 2014 at 8:00
  • 17 Use table-layout: fixed; – Mr. Alien Commented Feb 1, 2014 at 8:01
  • 50% width always works on cells, I'm guessing your problem is really with the table not being 100% in width. – slash197 Commented Feb 1, 2014 at 8:03
  • Oh, I see. I had put padding on the outsides and between the cells. Also, some text that has really long words was pushing some tables to not be 50%/50%. – Village Commented Feb 1, 2014 at 8:35
Add a comment |

3 Answers 3

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

You can set table-layout: fixed; on your table. Using this you can override the browser's automatic column resizing. Then column widths will be set based on the widths that you assign to the cells in the first row.

Share Improve this answer Follow edited Jan 29, 2023 at 3:38 Cam Jackson's user avatar Cam Jackson 12.2k8 gold badges46 silver badges79 bronze badges answered Feb 1, 2014 at 8:04 TechGuy's user avatar TechGuyTechGuy 4,48215 gold badges58 silver badges91 bronze badges 1
  • 1 MDN link: developer.mozilla.org/en-US/docs/Web/CSS/table-layout – Flimm Commented Jan 13, 2021 at 14:18
Add a comment | 45

I had the same issue in one of my projects, however it got fixed when I combined both the solutions above (Thanks to them). Here is my code snippet :

.Table{ width: 100%; table-layout: fixed; } Share Improve this answer Follow answered Nov 27, 2018 at 5:42 Mazhar MIK's user avatar Mazhar MIKMazhar MIK 1,11212 silver badges14 bronze badges Add a comment | 31

The table should have width: 100% property. See example here

table{ width: 100%; border-collapse:collapse; } td{ width: 50%; text-align: left; border: 1px solid black; }

About the text alignment you said two different things:

Using CSS, I need to set all of the cells to 50% width, with text in the left "column" being right-aligned and text in the left column being left-aligned.

and then

How can I set equal-width cells in a two-column table in CSS with everything aligned to the middle?

If the first is what you want and you cannot change your HTML you can use td:first-child to style your first column differently.

If the second is your best option, just change the text-align value to center.

Share Improve this answer Follow edited Feb 1, 2014 at 9:25 answered Feb 1, 2014 at 8:04 Itay Gal's user avatar Itay GalItay Gal 10.8k6 gold badges40 silver badges77 bronze badges 2
  • 24 What if I have a dynamic number of columns? – Shimmy Weitzhandler Commented Nov 24, 2015 at 20:10
  • override this property and set it using javascript with the average when you are rendering them – Jackal Commented Aug 28, 2020 at 12:55
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.

  • Featured on Meta
  • Upcoming sign-up experiments related to tags
  • Policy: Generative AI (e.g., ChatGPT) is banned
  • The [lib] tag is being burninated
  • What makes a homepage useful for logged-in users

Linked

2 Set td width by td number in a table 1 Problems while trying to center the a table's content and border line? 0 Dynamic width on table child 87 Make columns of equal width in <table> -2 How to center the divider line in an HTML table? 0 Resize the width of specific table cell without affecting others 122 Table with 100% width with equal size columns 11 How do you make a 100% width table with the columns all equal width? 169 CSS table-cell equal width 87 Make columns of equal width in <table> 1 Equal Column width on a table created by divs 0 How can I make all table cell sizes the same width in css? 1 How can two columns in a table be made to have the same width? 3 Making two of 4 columns equal width 2 make the width of the table equal to the width of its columns in css 0 CSS Table uniform column widths

Hot Network Questions

  • What kind of sequence is between an arithmetic and a geometric sequence?
  • Visit USA via land border for French citizen
  • Do known physical systems all have a unique time evolution?
  • Is it possible to complete a Phd on your own?
  • What type of black color text for brochure print in CMYK?
  • Weird behavior by car insurance - is this legit?
  • Is it unfair to retroactively excuse a student for absences?
  • Do I need to indicate 'solo' for wind/brass instruments in shared staff?
  • Is Légal’s reported “psychological trick” considered fair play or unacceptable conduct under FIDE rules?
  • Why was the animal "Wolf" used in the title "The Wolf of Wall Street (2013)"?
  • What was the first game to intentionally use letterboxing to indicate a cutscene?
  • How many steps are needed to turn one "a" into 100,000 "a"s using only the three functions of "select all", "copy" and "paste"?
  • "All due respect to jazz." - Does this mean the speaker likes it or dislikes it?
  • Is automorphism on a compact group necessarily homeomorphism? How about N-dimensional torus?
  • Does Matthew 7:13-14 contradict Luke 13:22-29?
  • How to Draw Gabriel's Horn
  • Integration of the product of two exponential functions
  • How are "pursed" and "rounded" synonymous?
  • Is there any other reason to stockpile minerals aside preparing for war?
  • How to "refresh" or "reset" the settings of a default view?
  • Folk stories and notions in mathematics that are likely false, inaccurate, apocryphal, or poorly founded?
  • How to Pick Out Strings of a Specified Length
  • Is the zero vector necessary to do quantum mechanics?
  • Was BCD a limiting factor on 6502 speed?
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 Table Equal Column Width Css