Table size, borders, cell padding, and cell spacing, and can all be changed by using table attributes. Additionally, multiple rows or columns can be merged into one if necessary.
Below is the code for our basic table with no attributes. The table size will only be as big as it needs to be to display the information. There will be no borders, and cell spacing/padding will be at a minimum.
Grade
Class
A-
Math
B
English
B+
History
C+
Science
It may look like something like this, depending on your browser.
Border Attribute
We can place the border attribute inside the opening table tag and set it to a value of 1 to achieve a border.
If we set the border value to a larger number, like 4, we get this.
Setting Cell Padding and Spacing
There are two attributes called cellpadding and cellspacing which are used to adjust the white space in the table cells. The cellspacing attribute sets space between table cells, while cellpadding sets the distance between cell borders and the content within a cell.
By setting the cellspacing attribute to zero, we could collapse the borders to eliminate the double border interior of our table.
Table Width and Height
The table width attribute and height attribute can be used to set the size of the table. Typically, we would set the table width and let the height auto set so that we don’t distort the table (we could better use spacing to get the desired height look).
Pixels or percentage can be used. When using pixels, there is no need for the px.
Spanning Rows
The rowspan attribute can be used to merge two or more rows into a single row.
Grade
Class
NO INFO
Math
English
B+
History
C+
Science
NoteWhen combining rows, notice that the next tr /tr set will now contain one fewer td for each row spanned.
Spanning Columns
The colspan attribute can be used to merge two or more columns into a single column.
Grade
Class
NO INFO
B
English
B+
History
C+
Science
NoteWhen combining columns, notice that we no longer need two sets of td tags within that tr row.
Many of the things you can do to style a table can also be done via CSS. It is usually a best practice to do it via CSS. This does not include spanning rows or columns which must be done by attribute.
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.
If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.