How to Create Vertical Table Headers with CSS in HTML?

Patricia Arquette
Release: 2024-10-25 03:25:29
Original
409 people have browsed it

How to Create Vertical Table Headers with CSS in HTML?

Vertical Table Headers in HTML

In standard HTML tables, rows are displayed horizontally, with table headers located at the top. However, there are situations where you may want to create tables with vertical rows and headers on the left side.

To achieve this, you can leverage CSS to modify the display of rows and table cells. A simple CSS fix would be:

<code class="css">tr { display: block; float: left; }
th, td { display: block; }</code>
Copy after login

This CSS code forces each row to display as a block element and float to the left, effectively creating vertical rows. The display: block; setting on table cells ensures that they also display as blocks within the vertical rows.

Note that this CSS solution assumes that your table cells are single-line. If you have multi-line cells, the table behavior may break down.

To enhance the display, you can add additional CSS rules to manage borders and remove borders where they are not needed:

<code class="css">/* single-row column design */
tr { display: block; float: left; }
th, td { display: block; border: 1px solid black; }

/* border-collapse */
tr>*:not(:first-child) { border-top: 0; }
tr:not(:first-child)>* { border-left:0; }</code>
Copy after login

With this CSS, you can create tables with vertical rows that are both visually appealing and maintain the functionality of accessible rows via tags.

The above is the detailed content of How to Create Vertical Table Headers with CSS in HTML?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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!