Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML table: column width percentage

Tags:

html

css

I have a table with 7 columns. I want the columns to be of the following widths:

  • 3 columns x width=20%
  • 4 columns x width=10%

I have created 2 CSS classes, one per width and I am assigning them to each cell.

The thing I have, and that does not work is this. Instead, the width always wraps the content. If I just put one letter, than the width will be very small, if I put large string, the width will be too big. I want it constant.

CSS & HTML :

table {    width: 100%;  }  .ten {    width: 10%  }  .twenty {    width: 20%;  }
<table>    <tr>      <th class="ten">H1</th>      <th class="ten">H2</th>      <th class="ten">H3</th>      <th class="twenty">H4</th>      <th class="twenty">H5</th>      <th class="twenty">H6</th>      <th class="ten">H7</th>    </tr>      <tr>      <td class="ten">A1</td>      <td class="ten">A2</td>      <td class="ten">A3</td>      <td class="twenty">A4</td>      <td class="twenty">A5</td>      <td class="twenty">A6</td>      <td class="ten">A7</td>    </tr>      <tr>      <td class="ten">B1</td>      <td class="ten">B2</td>      <td class="ten">B3</td>      <td class="twenty">B4</td>      <td class="twenty">B5</td>      <td class="twenty">B6</td>      <td class="ten">B7</td>    </tr>    </table>

Could someone explain how to achieve what I want?

like image 892
wesleyy Avatar asked Jan 14 '17 20:01

wesleyy


People also ask

Can I use percentage values for td width?

The < td > width can be set either as an absolute value in pixels, or as in percentage (%).

How do you adjust column width in HTML table?

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.

How fix HTML table width?

To set the table width in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property width.

How do I make a table width in HTML?

To manipulate the height or width of an entire table, place the size attribute (either "WIDTH=" or "HEIGHT=") within the <TABLE> code. To manipulate individual cells, place the size attribute within the code for that cell.

How do I set the percentage of a column in HTML?

Let’s say we want the first column to be 40% of the table and the two remaning columns to be 30% (as 40% + 30% +30% = 100%). In order to do this, we use the <col> element. This element is to be placed between the <table> tag and the <thead> tag and we use the style attribute to define the width of the columns.

How do I set the width of my HTML table?

You can specify your HTML TABLE width as a set number value or use a percentage. In addition, you can set the widths of your TABLE columns to display your columns at a specific width.

How to set the width of a column group in HTML?

The <colgroup> attribute sets a predefined width of column group. pixels: It specify the width of the colgroup in terms of pixels. %: It specify the width of the colgroup element in percent. For ex-width=”50%” relative_length: It divides the length of the table in terms of pixels.

How to define the width of the columns of a table?

This element is to be placed between the <table> tag and the <thead> tag and we use the style attribute to define the width of the columns. The <col> element is a self-closing element and you need one for every columns of you table.


2 Answers

To fixe width, you can use table-layout:fixed; .

You may also want to use the colgroup and col tags to assign at once width and bg to your columns.

table {    width: 100%;    table-layout: fixed;  }  .ten {    width: 10%;    background: tomato;  }  .twenty {    width: 20%;    background: turquoise  }  /* see me */  td {    border: solid;  }  /* play with bg cells and cols ? */    tr:nth-child(even) :nth-child(odd) {    background:rgba(100,255,50,0.3);    }  tr:nth-child(odd) :nth-child(even) {    background:rgba(255,255,255,0.3);    }
<table>    <colgroup>      <col class="ten" />      <col class="ten" />      <col class="ten" />      <col class="twenty" />      <col class="twenty" />      <col class="twenty" />      <col class="ten" />    </colgroup>    <tr>      <th>H1</th>      <th>H2</th>      <th>H3</th>      <th>H4</th>      <th>H5</th>      <th>H6</th>      <th>H7</th>    </tr>      <tr>      <td>A1</td>      <td>A2</td>      <td>A3</td>      <td>A4</td>      <td>A5</td>      <td>A6</td>      <td>A7</td>    </tr>      <tr>      <td>B1</td>      <td>B2</td>      <td>B3</td>      <td>B4</td>      <td>B5</td>      <td>B6</td>      <td>B7</td>    </tr>   <tr>      <td>A1</td>      <td>A2</td>      <td>A3</td>      <td>A4</td>      <td>A5</td>      <td>A6</td>      <td>A7</td>    </tr>      <tr>      <td>B1</td>      <td>B2</td>      <td>B3</td>      <td>B4</td>      <td>B5</td>      <td>B6</td>      <td>B7</td>    </tr>    </table>
like image 117
G-Cyrillus Avatar answered Sep 19 '22 15:09

G-Cyrillus


Your CSS works as expected, in a way that the widths of the table heads and cells are correct. However:

th by default have text-align: center applied, while the td element has not.

So you should add either to the td or th the same text alignment. E.g.:

th {     text-align: left; } 
like image 29
Kees van Lierop Avatar answered Sep 22 '22 15:09

Kees van Lierop