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.

    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 make a table with equal column widths in CSS? Ask Question Asked 10 years, 11 months ago Modified 1 year, 11 months ago Viewed 158k times 90

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,87572 gold badges61 silver badges133 bronze badges asked Feb 1, 2014 at 7:58 Village's user avatar VillageVillage 24.3k51 gold badges127 silver badges167 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) 176

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.3k8 gold badges47 silver badges79 bronze badges answered Feb 1, 2014 at 8:04 TechGuy's user avatar TechGuyTechGuy 4,56015 gold badges59 silver badges94 bronze badges 2
  • 1 MDN link: developer.mozilla.org/en-US/docs/Web/CSS/table-layout – Flimm Commented Jan 13, 2021 at 14:18
  • This doesn't work for me with a dynamic width: width: auto. It has to be a static value. – RokeJulianLockhart Commented Aug 14, 2024 at 22:52
Add a comment | 49

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,17213 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 badges42 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.

  • 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

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 92 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 128 Table with 100% width with equal size columns 11 How do you make a 100% width table with the columns all equal width? 171 CSS table-cell equal width 92 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

  • Characters besides 年 that contain 年 as a component
  • How can point particles be Lorentz Contracted?
  • Application of windows in Welch and Barlett PSD methods
  • What religious significance does the fine tuning argument have?
  • Improving calculation speed of root finding
  • Is copper anti-seize good for aluminium?
  • Are NASA computers really that powerful?
  • Introductory references on curves over finite fields
  • Why do I need this extra condition on a vector space basis theorem?
  • Is there any denomination which officially rejects Young Earth Creationism?
  • References to "corn" in translations of the Jiuzhang Suanshu
  • Is humanity a "genetic algorithm"?
  • No other coauthors, just me and my supervisors—is this normal?
  • What is "Hammet-style"?
  • Should I use ChatGPT and Wolfram Mathematica as a student?
  • Why no bicycles have the rear sprocket OUTSIDE, of the frame spacing? (Single speed)
  • How to decide who takes on a class action law suit?
  • What is the origin of "litera" versus "littera"?
  • The chapter starts on page n but the content starts on page n+1
  • What did "tag tearing" mean in 1924?
  • Inverting band pass filter circuit not showing theoretical behavior at all in SPICE simulation. What am I missing?
  • Kids' book where a girl trades her heart for a heart made of lead
  • Why does Cutter use a fireaxe to save a trapped performer in the water tank trick?
  • Equivalent GLM formulation of Wood's lactation curve
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