Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to synchronize table column widths with HTML + CSS?

Tags:

html

css

I have a number of tables with the same columns and it would look a lot nicer if they shared the same column widths. Is such a thing possible? Putting them in the same table with some rows with no borders between them isn't an option.

Edit: Yeah I'm aware I can fix the widths myself but I was hoping for something that would tie in to the browser's column width algorithm but simply tied two or more tables together for the purpose of doing that layout.

I didn't think such a thing was possible but I thought I'd check just in case.

like image 281
cletus Avatar asked Jan 19 '09 08:01

cletus


People also ask

How do I make table columns the same width in HTML?

Just add style="table-layout: fixed ; width: 100%;" inside <table> tag and also if you do not specify any styles and add just style=" width: 100%;" inside <table> You will be able to resolve it.

How do I make table columns the same width in CSS?

If you set the style table-layout: fixed; on your table, you can override the browser's automatic column resizing. The browser will then set column widths based on the width of cells in the first row of the table. Change your to and remove the inside of it, and then set fixed widths for the cells in .

How do I keep my column width fixed in HTML?

The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.

How do you align columns in a table in HTML?

Table data defaults to left alignment; table headers to center. In order to change the alignment in one cell, insert the appropriate "ALIGN=" attribute within the code for that cell. In order to change the alignment in all cells in a row, insert the appropriate alignment attribute within the code for that row.


2 Answers

If you're not too picky about which column widths the browser comes up with, as long as they're the same across different tables, you can use the CSS table-layout property (supported by all major browsers) in combination with a table width:

table {     table-layout: fixed;     width: 100%; } 

This causes all columns (without a specified width) to have the same width, regardless of the table content.

like image 193
mercator Avatar answered Sep 18 '22 11:09

mercator


It's only possible if you can fix-width the columns. If you can set a fixed width then some css like this should work:

td {     width: 25%; } 

You can customize each columns width like this:

<table>   <tr>     <td class="col1">...</td>     <td class="col2">...</td>   </tr> </table> ... <table>   <tr>     <td class="col1">...</td>     <td class="col2">...</td>   </tr> </table> 

and then specify the widths like this:

.col1 {    width: 25%; } .col2 {    width: 75%; } 
like image 25
Ken Browning Avatar answered Sep 16 '22 11:09

Ken Browning