Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to: "Separate table rows with a line"

I have a basic HTML table which contains table rows. My goal is to separate those table rows with a visible line (for better readability of the content).

How could I do this?

like image 656
Milos Cuculovic Avatar asked Nov 29 '12 11:11

Milos Cuculovic


People also ask

How do you break a line in a table row?

The line break code allows data to be split into multiple lines. Place the line break code <BR> within the text at the point(s) you want the line to break.

How do I add a line between rows in CSS?

If you want to add internal lines to the individual cells of that table, you need to add borders to the interior CSS elements. You can use the HR tag to add lines inside individual cells.

How do I add a line between tables in HTML?

To create table border in HTML, the border attribute was used. But the introduction of HTML5, deprecated the border tag. Create table border using the CSS property border. Set table border as well as border for <th> and <td>.


2 Answers

There are several ways to do that. Using HTML alone, you can write

<table border=1 frame=void rules=rows> 

or, if you want a border above the first row and below the last row too,

<table border=1 frame=hsides rules=rows> 

This is rather inflexible, though; you cannot e.g. make the lines dotted this way, or thicker than one pixel. This is why in the past people used special separator rows, consisting of nothing but some content intended to produce a line (it gets somewhat dirty, especially when you need to make rows e.g. just a few pixels high, but it’s possible).

For the most of it, people nowadays use CSS border properties for the purpose. It’s fairly simple and cross-browser. But note that to make the lines continuous, you need to prevent spacing between cells, using either the cellspacing=0 attribute in the table tag or the CSS rule table { border-collapse: collapse; }. Removing such spacing may necessitate adding some padding (with CSS, preferably) inside the cells.

At the simplest, you could use

<style> table {   border-collapse: collapse; } tr {    border: solid;   border-width: 1px 0; } </style> 

This puts a border above the first row and below the last row too. To prevent that, add e.g. the following into the style sheet:

tr:first-child {   border-top: none; } tr:last-child {   border-bottom: none; } 
like image 64
Jukka K. Korpela Avatar answered Sep 20 '22 14:09

Jukka K. Korpela


Just style the border of the rows:

​table tr {     border-bottom: 1px solid black; }​  table tr:last-child {      border-bottom: none;  } 

Here is a fiddle.

Edited as mentioned by @pkyeck. The second style avoids the line under the last row. Maybe you are looking for this.

like image 38
cem Avatar answered Sep 20 '22 14:09

cem