Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this chapter, we're going to focus on using tables in HTML. Tables are one of the most useful elements in all of HTML, as they allow us to represent data in something other than a linear fashion. Let's take a look at the basic syntax of a table. Now, to do that, I've opened up the tables.htm file, which you can find in the 05_01 folder, and I have opened it up in a browser. And that's because I sort of wanted to go over some of these tags before we start working on our sample table. So, our basic table structure is composed of really four basic tags, and you can find them right up here in this paragraph: a table tag which identifies to the browser that we're beginning a table, and then a table row, and that tr or table row tag establishes a new row every time it appears.
Inside your table rows you'll either have a table header or a table cell tags. Yes, I know it's td and the d stands for data, but most people will refer to that as a table cell tag. We have a table here. This is a sample HTML code, a little sample table that we're going to be building. Now the sample code we're looking at here is sort of the finished version of this. So throughout this chapter we're going to begin to build this table starting with the most simple basic table elements and then getting more complex as we keep adding more and more to the table. So, I'm going to switch over to my code editor and go into the same file that we had opened up in the browser.
This is tables.htm from 05_01. And we're going to start coding our table using these basic table elements. Now, you want to scroll down through the code. There is this little section right here, this preformatted section, that has this sample code in it. I've collapsed that so that I'm saving a little bit of screen real state. So if you're wondering why you don't see it on mine and you see on yours, that's why, you're going to scroll all the way down until you find this little comment that says, "create table here," and I'm just going to create an empty space or two to begin our table.
Now, as I mentioned before, the tables start with a table tag. So I'm going to begin by entering an opening table tag. And I like to go ahead and close my table out, so I'm going to go ahead and create a closing table tag as well, so just create a little bit of empty space in between that. Now again, this is a habit I like to get into for any complex structure. Once you start creating a table, a lot of times you're going to be working on that for a good twenty or thirty minutes. It's really easy to forget to close that table tag, so I like to close one as soon as I open one up, leave that space in between it empty, and just begin working.
Okay, so once you've created a table, you need to define its rows and columns. The rows are going to be defined by using a tr, or table row, tag, so that's the first thing I'm going to do is go ahead and create a table row tag. Now the other thing, I'm going to go right underneath that and once again I'm going to close this table row tag back out again. So we've just created the first row in the table. Now we need to start creating columns. Now, there is no table column tag. That does not exist; rather, columns are created by placing individual cells which represent each column.
So in the case of this particular table, we're going to have five rows and three cells, so inside this row we need to have three cells. The basic table cell tag is the td tag, so we're going to start with that. So I'm going to open up a td tag, and inside of that I'm just going to go ahead and put a nonbreaking space. You don't need to do this. It's just a habit that I have of just putting a little bit placeholder content in that table, data; it sort of holds the tag apart and lets me know that content is going to go there. If you want to leave them empty, be my guest.
And what I'm going to do is I need two more of those, so I'm just going to go ahead and do another td tag, another empty or another nonbreaking space I should say, and then close that td tag out, and one more. Now you're probably wondering, "hey, wouldn't it have been okay to just copy and paste that?" and you know what, you're absolutely right; it sure would be. So once you are satisfied with your syntax, that everything is correct and formatted the way you want it to, tables have a lot of repeating structures and there is absolutely nothing wrong with copying and pasting code.
But one of the things that you want to be careful about is that if you have an error in your syntax and then you copy and paste that error, you're compounding it. So you really, before you start copying and pasting things, you want to make sure that your syntax is right. So I'm going to copy this entire row, and like I said, we need five rows, so I'm going to go ahead and hit Return and paste that and I'll hit Return and paste it again. That gives me three, four, and finally five. All right! So I have got five tables rows and we have three columns in this table.
Now, if I save my file right now, go back into the browser and refresh this, if I scroll down, it doesn't look like we've done anything at all. Well, remember, this table is empty. But I do notice there is a good bit more space here. If I highlight the page I could begin to see the very, very beginnings of our table structure. We can see the three columns that are very thin. They don't have anything in them except that one little piece of whitespace. And then we have the five rows to our table. So really, with just those three elements-- the table tag, the table row tag, and the table cell tag--you can create basic tables.
Of course, as a table, it's not going to do you a whole lot of good unless you have some content inside of it, so we're going to look at adding content in our next exercise.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.