Can you create equal width table cells with varying content using purely CSS?

Barbara Streisand
Release: 2024-11-05 07:32:02
Original
551 people have browsed it

Can you create equal width table cells with varying content using purely CSS?

Equal Width Table Cells with Variable Content

Consider a scenario where you have multiple table-cell elements inside a table container. Despite having different content sizes, you want these table-cells to have equal widths. Is there a purely CSS solution to achieve this?

Solution:

To achieve equal-width table-cells regardless of content size, you can utilize the following CSS approach:

  1. Fixed Table Layout: Apply table-layout: fixed; to the parent div representing the table. This essentially instructs the browser to adhere strictly to the specified cell widths.
  2. Minimum Cell Width: Set a minimum width, even a small one like 2%, for each cell (div > div). This triggers the desired table layout algorithm, where the browser attempts to respect the specified cell dimensions.

Code Example:

<code class="css">div {
  display: table;
  width: 250px;
  table-layout: fixed;
}

div > div {
  display: table-cell;
  width: 2%;
}</code>
Copy after login

Note:

This solution works with Chrome, IE8, and Safari 6 (with some caveats). However, it's important to remember that Safari 6 may have different table layout behaviors compared to other browsers.

The above is the detailed content of Can you create equal width table cells with varying content using purely CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!