Tips on how to Create Tables in HTML: Easy Steps for Novices

On this table-themed HTML tutorial, I’ll clarify how one can make a fundamental desk, add and take away borders, create layouts, make cells the form and dimension you need, place cell contents, and add coloration.

It’s straightforward to observe and designed for rookies. I’ve included photos so you’ll be able to see what the HTML 5 code ought to seem like.

I’d additionally advocate utilizing this free HTML Code Editor. Enter your code within the left-hand part, and the preview will seem on the correct. It’s a terrific web site to make use of in the event you plan to discover the world of HTML sooner or later.

Create a fundamental desk

Earlier than you are able to do fancy stuff with desk structure, you should create a desk. The <desk> and </desk> tags enclose all the opposite components of a desk. Every row in a desk is ready up with a <tr> (desk row) tag, which is adopted by a <td> (desk information) tag for every cell in that row. The next code units up a easy 2-by-2 desk:

<desk>
<tr>
<td>Cell contents&nbsp;</td>
<td>Cell contents</td>
</tr>
<tr>
<td>Cell contents</td>
<td>Cell contents</td>
</tr>
</desk>

The <desk>, <tr>, and <td> tags all have varied attributes that allow you to management the look of the desk itself in addition to the position of its contents. (Learn the remainder of our desk tricks to get acquainted with them.) Within the absence of these attributes, the desk defaults to suit across the cell contents. You’ll discover I added &nbsp; on the third line as I needed an area between the phrases. Thus, in most browsers, the code above yields a borderless desk that appears like this:

A easy, borderless desk. Picture: HTML Code Editor

SEE: 5 Important HTML Guidelines for Novices (TechRepublic)

Add and take away borders

Tables don’t need to include textual content solely, after all. A lot of the complicated layouts you see on the Internet mix photos and textual content inside varied desk cells — you simply can’t see the traces, or borders, between the cells. The border attribute of the <desk> tag permits you to assign a thickness (in pixels) to the border traces.

To make a desk with a border of two pixels, simply add border=”2″ to the <desk> tag. To make an invisible border, set the border attribute to 0. (Though most browsers default to a desk border of 0, particularly stating it ensures that the border shall be invisible in all browsers.)

Under are two examples of what this seems to be like. On the left are the codes for one desk with a 2-pixel border and one other desk with an invisible border. The completed merchandise are on the correct.

Instance one:

Sample HTML table output.
A desk with a 2-pixel border. Picture: HTML Code Editor

<desk border=”2″>
<tr>
<td>See our merchandise</td>
</tr>
<tr>
<td>Discover out about us</td>
</tr>
</desk>

Instance two:

Sample HTML table output.
A desk with an invisible border. Picture: HTML Code Editor

<desk border=”0″>
<tr>
<td>See our merchandise</td>
</tr>
<tr>
<td>Discover out about us</td>
</tr>
</desk>

Right here’s a helpful trick — design the desk with a visual border, which is able to present you simply how your components are damaged up. When you’ve every thing in place, change the border attribute to 0.

SEE: AI-Generated Code is Inflicting Outages and Safety Points in Companies (TechRepublic)

Create desk layouts

Two attributes for laying out desk content material had been cellpadding and cellspacing. Nevertheless, in HTML 5, these attributes are not supported. That’s not an issue; utilizing the model sheet language Cascading Fashion Sheets will assist. CSS is sweet to know as a result of it may be used to specify the presentation and styling of a doc.

The CSS part in our instance begins with <model> and ends at </model>. The wording must be self-explanatory, because the border is ready at 2 pixels and is strong black, whereas the padding is ready at 10 pixels. The latter controls the space (in pixels) between the cell’s contents and its sides.

Sample HTML table output.
With CSS positioned earlier than the HTML, we’ve a sublime resolution. Picture: HTML Code Editor

<model>
   desk {
      border: 2px strong black;
      border-collapse: collapse;
   }
   td {
      padding: 10px;
   }
</model>

<desk>
<tr>
<td>See our merchandise</td>
</tr>
<tr>
<td>Discover out about us</td>
</tr>
</desk>

Make cells the form you need

HTML doesn’t stick you with plain grids in your desk structure. With the rowspan and colspan attributes of the <td> tag, you may make a given cell span the peak or width of a number of different cells. To make use of these attributes, merely assign them a price primarily based on the variety of cells you need to span.

As an illustration, the next desk has two rows of three columns every:

Sample HTML table output.
Columns on present. Picture: HTML Code Editor

<desk border=”2″>
<tr>
<td>Cell contents</td>
<td>Cell contents</td>
<td>Cell contents</td>
</tr>
<tr>
<td>Cell contents</td>
<td>Cell contents</td>
<td>Cell contents</td>
</tr>
</desk>

To make the primary cell span all three columns, add colspan=”3″ to its <td> tag and delete the opposite two <td> tags in that row:

Sample HTML table output.
The colspan attribute provides a brand new dimension to the columns. Picture: HTML Code Editor

<desk border=”2″>
<tr>
<td colspan=”3″>Cell contents</td>
</tr>
<tr>
<td>Cell contents</td>
<td>Cell contents</td>
<td>Cell contents</td>
</tr>
</desk>

Should you’d prefer to make that first cell span two rows as a substitute, add rowspan=”2″ to its <td> tag and delete the primary <td> tag from the second row:

Sample HTML table output.
The rowspan attribute alters the rows. Picture: HTML Code Editor

<desk border=”2″>
<tr>
<td rowspan=”2″>Cell contents</td>
<td>Cell contents</td>
<td>Cell contents</td>
</tr>
<tr>
<td>Cell contents</td>
<td>Cell contents</td>
</tr>
</desk>

After all, you may make your tables much more complicated than these examples. Should you select to take action, it’s all the time a good suggestion to sketch out your tables earlier than you create them.

Make cells the scale you need

Desk cells dimension themselves to their content material by default. However what in order for you cells of a unique dimension? Enter the width and top attributes of the <td> tag. Simply specify the scale in pixels, and also you’re all set. To make a cell 100 pixels large and 80 pixels excessive, for instance, you’d do that:

Sample HTML table output.
The width and top attributes permit you loads of choices. Picture: HTML Code Editor

<desk border=”2″>
<tr>
<td width=”100″ top=”80″>Cell contents</td>
</tr>
</desk>

Observe that width and top are solely recommended attributes. That’s, they take impact provided that the cell’s set width or top doesn’t battle with different cells in the identical column or row.

SEE: Fast Glossary: Internet Browsers (TechRepublic Premium)

Exactly place cell contents

When you begin altering the form and dimension of desk cells, the cells not form themselves round their contents. Thus, to position components the place you need inside such desk cells, you want two attributes of the <td> tag: align, which locations objects left, proper, or middle inside a cell; and valign, which strikes them up and down utilizing the high, center, and backside directions. (By default, components align horizontally to the left and vertically within the center.) As an illustration, to align textual content to the highest proper in a 100-by-80-pixel cell, you’d use the next code:

Sample HTML table output.
The align and valign attributes in motion. Picture: HTML Code Editor

<desk border=”2″>
<tr>
<td width=”100″ top=”80″ align=”proper” valign=”high”>Cell contents</td>
</tr>
</desk>

Observe: whenever you’re inserting objects in desk cells, and also you need them to align correctly, don’t depart area after the opening <td> or earlier than the closing </td> of a cell. The cell’s contents ought to contact the <td> tags to make sure correct alignment, particularly whenever you’re working with photos.

Make your desk colourful

Sick of getting your desk mix in together with your web page? Then change its background coloration! It was once a case of including the bgcolor attribute to the <desk> tag and assigning it a typical hexadecimal coloration code or a one-word coloration identify. Nevertheless, this attribute is not supported in HTML 5. Meaning we flip to CSS once more.

For instance, this code creates a easy desk with a pale blue background:

Sample HTML table output.
Not every thing must be black and white. Picture: HTML Code Editor

<model>
   desk {
      background-color: #CCFFFF;
   }
   td {
      padding: 10px;
   }
</model>

<desk>
<tr>
<td>Cell contents</td>
<td>Cell contents</td>
</tr>
<tr>
<td>Cell contents</td>
<td>Cell contents</td>
</tr>
</desk>

Place your desk on the web page

Along with formatting components inside a desk, you’ll be able to management the place your desk seems on the web page. Two <desk> attributes may also help you out:

  • The align attribute aligns the desk left, proper, or middle on the web page (left is the default).
  • The width attribute enables you to specify a set quantity of pixels for the desk’s width (through the use of a quantity, as in <desk width=”65″>) or enables you to make the desk occupy a share of the browser window’s width (by assigning a share, as in <desk width=”90″>).

Thus, the next code units up a desk 150 pixels large and centered on the web page:

<desk width=”150″ align=”middle”>

The code beneath units up a desk three-quarters the width of the browser window, aligned on the correct facet of the web page:

<desk width=”75%” align=”proper”>

TechRepublic Academy gives extra data about HTML, CSS

As you’ll be able to see, with fundamental information of HTML, tables will be constructed with ease. Actually, whenever you get extra conversant in HTML, you’ll discover a few of it may be changed with CSS.

Should you’re eager to be taught extra about HTML and CSS, then head over to TechRepublic Academy. There are many coaching programs obtainable on that web site. Completely happy searching!

Contributions by Donald St. John, with extra materials and recommendation from Cormac Foster, Mark Kaufman, Charity Kahn, and Matt Rotter.

Recent articles

FTC cracks down on Genshin Impression gacha loot field practices

Genshin Impression developer Cognosphere (aka Hoyoverse)...

New ‘Sneaky 2FA’ Phishing Package Targets Microsoft 365 Accounts with 2FA Code Bypass

Jan 17, 2025Ravie LakshmananCybersecurity / Menace Intelligence Cybersecurity researchers have...