Home >Web Front-end >CSS Tutorial >How Can I Maintain Fixed Cell Widths in HTML Tables?

How Can I Maintain Fixed Cell Widths in HTML Tables?

Susan Sarandon
Susan SarandonOriginal
2025-01-01 06:59:09403browse

How Can I Maintain Fixed Cell Widths in HTML Tables?

Maintaining Fixed Table Cell Width

Tables continue to be a common layout method, prompting questions about controlling cell widths. Traditional cell styling using width attributes often fails, as cell content expansion overrides the specified width.

How jqGrid Achieves Fixed Cell Widths

jqGrid utilizes a combination of elements:

  • Tag: This tag defines table column characteristics. jqGrid sets the width attribute within to specify the desired column width.
  • table-layout: fixed Style: Applied to the table, this style locks the column widths regardless of cell content.
  • overflow: hidden Style on Table Cells: This style prevents cell content from overflowing beyond the specified width.

Implementing Fixed Cell Widths

To implement fixed cell widths in your own tables:

  1. Include the tag and specify the desired width for each column.
  2. Apply the table-layout: fixed style to the table using CSS.
  3. Set the overflow: hidden style on individual table cells.

Example Code

HTML:

<table>
    <col width="20px">
    <col width="30px">
    <col width="40px">
    <tr>
        <td>text</td>
        <td>text</td>
        <td>text</td>
    </tr>
</table>

CSS:

table { table-layout: fixed; }
table td { overflow: hidden; }

The above is the detailed content of How Can I Maintain Fixed Cell Widths in HTML Tables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn