With HTML4 some table properties were controlled by attributes of HTML tags but with HTML5 are now controlled by CSS. These include width, cellpadding, cellspacing and borders.
By default, the contents are right next to the border and that can make it look crowded.
1 | 2 |
3 | 4 |
padding: unit
. The unit can be em, px pt, etc.
td { padding: 10px; }
adds ten pixels to all four sides. If you
only want padding on the left and right use td { padding-left: 30px; padding-right: 40px; }
<style> td { padding: 10px; } </style> <table> <tr> <td>1</td><td>2</td> </tr> <tr> <td>3</td><td>4</td> </tr> </table> |
|
Cell spacing is another way to put some space between contents of cells but
instead of putting space between the contents and the border of a cell it adds
space between the cell. Add spacing with cell-padding: unit
. The unit can be em, px pt, etc.
<table style="border-spacing: 1em;"> <tr> <td style="border-spacing: 1em;">1</td> <td style="border-spacing: 1em;">2</td> </tr> <tr> <td style="border-spacing: 1em;">3</td> <td style="border-spacing: 1em;">4</td> </tr> </table> |
|
|
Without CSS, tables have no borders. |
|
||||
|
With CSS you can add borders to tables, table cells and any other element. If you add a border to both the table and its cells you get a double border. |
|
||||
|
With border-collapse set to collapse you can merge the
table border with its cell's: |
|
In the examples above, I used the solid style border. Other border styles
are dotted, dashed, double groove, ridge, inset, outset, none and hidden.
The default width of border is 1px so you can't see the 3D effects. To
see the 3D effects, change the border width with border-width
.
Values for border-width
are thin, medium, thick or units
(px, em, etc). Border color is set with
border-color
Colors values can be named colors or hex
NOTE: Declare border-style
before width and color. If
you don't have a border, you can't change it's width.
By using the border shorthand, you can set
border-style, border-width and border-color all
in one rule.
border: type width color;
With the border-radius
property, you can give any element rounded corners. For the examples below I use an image. Note that the colors in the second example have eight digits instead of six. The last two are
for the alpha channel that sets transparency.
Border style, border color, border width and border radius can all be styled independently i'e., the top border, right border, bottom border, left border of a image can all be different from each other and the same for color and width. When styling all four sides the order is top, right, bottom, left.