Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable changing of width in html table

I'm trying to create a "fixed-width" table, but it somehow changes the column width whenever the data in column is bigger than rest of them.

For example, following table changes the width on the last number, which is 10.

<table>
    <tr>
        <td rowspan="3">1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
    <tr>
        <td>5</td>
        <td rowspan="2" colspan="2">7</td>
    </tr>
    <tr>
        <td>6</td>
    </tr>
    <tr>
        <td>8</td>
        <td colspan="2">9</td> 
        <td>10</td>
    </tr>
</table>

Here's my CSS:

table {
    border: 1px solid;
    width: 400px;
    height: 300px;
}
like image 427
Dawid Zbiński Avatar asked Jan 09 '14 23:01

Dawid Zbiński


People also ask

How do I stop HTML table from resizing?

Use style="table-layout:fixed;" , but this will make text or images overlap with other columns in case it exceed the width. Make sure not to place long text phrases without spaces.

How do I fix the width of a table in HTML?

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 stop a table column from resizing?

Turn off AutoFit Select your table. On the Layout tab, in the Cell Size group, click AutoFit. Click Fixed Column Width.


1 Answers

There are 2 algorithms for tables in CSS, triggered by the property table-layout:

  • table-layout:fixed will adapt cell widths to what the author (you) want, as far as possible
  • table-layout:auto (default value) will adapt cell widths to their content.

CSS

  table {
    table-layout: fixed;
    width: 400px;
    height: 300px;
    border: 1px solid;
  }

Here's a fiddle: http://jsfiddle.net/tk4J8/

like image 57
FelipeAls Avatar answered Sep 29 '22 20:09

FelipeAls