Content of the lesson:
Every table is a structure of a particular number of rows. Every row contains ending number of columns. Every column in a row is called a cell.
Every HTML table has to begin with the <table> tag and end with the </table> tag. The following elements can be used inside any table:
At the beginning we are going to create a simple table with two rows and two columns:
<table>
<tr>
<td>ORDER</td>
<td>Last
name, First name</td>
</tr>
<tr>
<td>1</td>
<td>Světlík Jaromír</td>
</tr>
</table>
You can see the beginning tag in the first line, then the tag for the beginning of new row. The following two lines contain two separated cells. There is the end of the first row on the fifth line. The rest of the table is the same and the table ends with proper tag.
The final HTML table:
ORDER | Last Name, Sure Name |
1 | Světlík Jaromír |
You can see that the table almost cannot be recognized in a browser so a border around it would be great to better orientate in it. You can add a border attribute in the HTML file: <table border="1">.
Our HTML table will change like this one:
ORDER | Last Name, Sure Name |
1 | Světlík Jaromír |
The structure is clearly visible but there are two new problems:
There is an attribute cellpadding available at every table which requires an integer value [0..n] and it sets the distance between the border of a cell and its content. In a common HTML this situation could look like this one:
<table cellpadding="5" border="1" >
<tr>
<td>ORDER</td>
<td>Last name, First name</td>
</tr>
<tr>
<td>1</td>
<td>Světlík Jaromír</td>
</tr>
</table>
The final HTML table should gain additional space (you can try to copy it inside a new HTML document and view it inside your browser):
ORDER | Last Name, First Name |
1 | Světlík Jaromír |
It would be easy to write cellpadding attribute to one table but you should realize that you may create a page with tens or hundreds of tables. Then your customer can order you to change cellpadding inside all of them and you will have to rewrite them all. Much better and faster way is to automate this procedure using CSS styles and set the padding parameter via CSS.
Take a look how a CSS rule for the cellpadding can be added. There is no cellpadding property inside CSS but you can find the padding property (in general it is inner space between a border and inner content). What is the right element to which you want to apply the padding property? You want to create space around the content of every cell so the required element is the cell (<td>).
td {padding: 10px;}
After applying this rule all our tables should change as we set.
ORDER | Last Name, Sure Name |
1 | Světlík Jaromír |
We should solve the problem about the border of the table. The first idea which could be the right one is using the border property for the table element. We should know that the border property requires color, width of the border and type.
table {border-width: 1px; border-color: #666; border-style: solid;}
These three settings can be written in a short way:
table {border: 1px #666 solid;}
The border can be seen below this text - only the whole table has border but not the cells.
ORDER | Last Name, Sure Name |
1 | Světlík Jaromír |
Now try to write and apply a rule to draw border around every cell. The rule is analogue but you should apply it for every td element.
td {border: 1px #666 solid;}
The final table can be seen below this text - the border is drawn around every cell inside the table.
ORDER | Last Name, Sure Name |
1 | Světlík Jaromír |
In case we do not want any space between the border of the table and every cell, you can add the cellspacing=0 attribute inside the HTML file. This parameter sets the spacing between every cell and if set to zero, no spacing will be displayed.
<table cellspacing="0" >
<tr>
<td>ORDER</td>
<td>Last Name, Sure Name</td>
</tr>
<tr>
<td>1</td>
<td>Světlík Jaromír</td>
</tr>
</table>
The adjusted table:
ORDER | Last Name, Sure Name |
1 | Světlík Jaromír |
Because of the possibility to change every table by changing one line of code we should use a CSS rule for this.
table {border-spacing: 0;}
The final table is illustrated below this text.
ORDER | Last Name, Sure Name |
1 | Světlík Jaromír |
Explain the usage of the border-collapse property in connection to the table element. Try to create a sample using this property.