Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Directly after the opening html tag, you'll find a document's head. Now the head element is where you'll place information about the document, references to external scripts and styles, and additional instruction to the browser about how the document should be rendered. To add the head to our document I am going to open up the structure.htm file, which can be found in 02_03 folder. To be honest with you though, if you're coming directly from the last exercise and you still have that file open, you just keep it open, because we are simply building on the last file that we have did. You can see, as a matter of fact, in this one the only thing we have is the document type declaration that we built in our last exercise.
Before we can add the head element to our HTML file, we have to have an HTML file, and we don't have one yet. So what I am going to do is directly after the doctype declaration, I'm going to go down to the next line and I am just going to type in an opening html tag. And then what I'll do is I'll go down on the line below that and I'll close the html tag out. So we just have an opening and a closing a html tag. That identifies this as being an HTML document and now we are free to sort of move on and add all the content to it. Before we do that, however, I am going to go back to the html tag and I am going to add an attribute to it.
So what I am going to do is click just inside the opening html tag right after the characters html tag, and I am just going to hit the spacebar to give myself a little bit of whitespace between the characters html and the attribute itself. And I am going to do the language attribute, and that is lang= "en" for English. Now what that's going to do is it's going to tell the browser that, hey, this entire HTML document is in English. Now if I've an individual paragraph for example that is in Spanish or French or Italian, I could indicate that at the paragraph level as well.
That's helpful for a number of reasons: translators will know which language you are looking at, screen readers will know how to enunciate or pronounce certain words. It's important you do this. It's not essential. It's not something you have to do for every HTML file you work with, but it's nice practice. I'll go ahead and save that and now we are ready to add our head. So what I am going to do is again, I am going to go up to the opening html tag. I am going to go right inside that and I am going to go ahead and open up the head. And then directly below that, I am going to go ahead and close the head tag.
Now you'll notice that I'm beginning to show you a pattern that I use as I author any code, really, that I'm working on. You can pick up this practice or you can choose not to adopt it. It's totally up to you. This is a personal preference of mine. If I open up a tag, even if there is going to be a lot of content inside that element, I'll go ahead and close that element before I start adding content. That does two things for me. The first is, it really kind of just shows the simplicity of what I'm building, because this head might have hundred lines of code in it, but it's still just a head element.
So it sort of identifies those basic structural elements first. The second thing that it does for me is it ensures that I close the element properly. With HTML it's not as bad as with some other languages, but whenever you open up an element and you begin adding a bunch of stuff, it's really easy, say, thirty minutes on down the line to forget that you've opened up the head tag in the first place and you just forget to close it. That happens quite often, and it can result in some serious syntax errors. So I advise you to sort of pick up that practice and it'll help prevent syntax errors on down the line.
I mentioned earlier that inside the head element of the document you're sort of putting the information about that document. You know, what is this document, what's the subject of it, are there any links to external scripts that you need me to execute or styles that you want me to show, that sort of thing. So, all the invisible stuff that goes either to give more information about the file or make the page work are going to go inside the head. So let's go ahead and just throw in a couple of simple things that we typically find in the head of an HTML document. Inside the head tag making sure that I'm inside the opening and closing tags, I am going to create a blank line, and the first thing that I am going to do is I am going to add a character set meta tag.
So meta tags are very important. I am going to go ahead and open up a tag, and I am just going to type in the word meta. So this is a meta tag, and there are several different types of meta tags that you can use. You can use meta tags to write a description of the page. We will do that in a minute, to add keywords to the page. You can use the meta tag to describe the viewport, or how large the window should be, which is helpful for mobile devices and things like that. So there are all sorts of things that you can do with meta tags, so I really recommend researching these as much as you can. The meta tag that we are going to use is going to use the character set attribute, and you can see it's the first one that's coming up here in my code hinting.
I am just going type in charset, character set. And then inside the equals I'm going to type in "utf-8," so that's the encoding that I am going to use for this particular character set. Typically, that's the one that everybody's going use, unless you are using a different language that requires a different character set. And it's very, very important that you specify this. In most cases your web server will send out a header that has a character set encoding in it, but in case it doesn't, it's very important for the HTML page do that.
So that's the first thing that you should do inside the head of your document to make sure that one of the first things the browser encounters is the instructions about which character set to use. So after that I am going to go down and I am going to add another meta. And in this case, I want to do a meta that has a description for the page. So I am going to type in meta. Once again, this time I am going to use name instead of character set, and the name for this one, in quotation marks, is going to be description. So I'm just basically letting the browser know, or any of these user agents know, that this was going to be description of the page contents. And then after the name attribute, I am going to go ahead and give it a content attribute, and this is where I actually pass the description itself.
So again, in quotation marks here, I am going to type in "A page for exploring basic HTML syntax." So this just sort of describes what the page is. And then finally, I am going to close using the right angle bracket. So you'll notice that meta tags don't have a closing tag. They are not optional. It just doesn't have one, so you don't need them at all. So you can see meta name= "description" content= whatever I want my description to be. Now, what's the point of this? Well, it's very helpful for search engines. When they start displaying search results, they'll actually go into the meta description tag and display your content there for a brief description of the page.
So it's very helpful here to be descriptive in terms of what is on this page and how you'd want is to display. There is also another meta that you can use for keywords. For the most part, search engines sort of ignore them, because people have used them for so long. So there is dubious value in doing keywords now, except for the fact that you could sort of type in keywords that means something to you and within your blog or some like that, you can actually use that to do an intelligent search or retrieve your own things for keywords or just a way of indexing the page. So they're still valuable, but they're not valuable anymore in terms of search engines.
I am going to do one more thing within the head of the document, so I am going to hit Enter and go down to the next line, and here I am going to add a title. So we are going to use the title element, and to do the title, you do an opening tag, you'll type in what you want the title of the page to be, and then you'll close that opening tag. And for this one, I am just going to type in the BASIC HTML document. You'll notice that that text is surrounded by the opening title tag and then the closing title tag. So this is like the one thing that goes in the head that is just visible for other people.
If we save this file, for example, and then I were to preview this file, say, in one of my browsers-- so I preview this page on a browser, you can there is really nothing on the page here. It doesn't even have a body to it. But what we do see, up in the title bar of the tab here, is BASIC HTML document, so that's one of the places that the title will show up. And other user agents will use the title in different ways to represent the page, so it's a very important part of any HTML page. You need to make sure there's a title within the head of the file. You can also write local styles and scripts within the head, as well as adding references to external styles and scripts that are going to be used by the page.
Now we are going to explore adding styles to the head of our document a little later on in the course, but for right now, we're done with the basic structure of the head of our document.
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.