Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Paragraphs are perhaps the most basic formatting tag that you'll use in HTML. In fact, if you don't wrap text in an element, most browsers simply default to using a paragraph to represent the text. Here I have the paragraph.htm file opened up and if I preview the page in my browser right now, you can see nothing much going on here. We have our headings on the page that we had before, and all of the text other than the headings is basically displaying as if it were inside of a paragraph. You'll notice that there is vertical margins between the headings and between the remainder of the text, and that's exactly what we see when we are looking at a paragraph being rendered within the browser. But if go back into my code, I can see that I don't have any paragraphs.
The text is just sitting inside the page and not being identified at all by any elements. So we're going to change that, and we're going to see something that the browser was doing there that we may have missed at first glance. So the first thing I'm going to do is I'm going to go up to the paragraph directly underneath the heading 2 here and I'm going to go ahead and wrap that in a paragraph tag. So we have an opening tag with the p element and then a closing tag with the p character as well. So opening paragraph tag, closing paragraph tag. So I'm just going to go through all the paragraphs on the page and do exactly the same thing.
It shouldn't take too long, until we get down towards the bottom, and that's when I want to point something out down here. So if I get down to this little section here where I've got line breaks going on, you can see that when we previewed our page earlier, you can see that just down below Line breaks, this looks like one big section of text. But if I go back into my code, I can see that actually I have three different sections of text, and these are all going to be separate paragraphs. Because of the fact that none of them are wrapped in a paragraph, the browser just treated them as one large section of text.
And that's something that you need to keep in mind if you're ever formatting your copy and you leave a paragraph off, really, anything that comes after that, until it encounters a new element, it considers that to be one big paragraph. So you need to be very careful with that. I'm going to take the Occasionally, the paragraph that starts with Occasionally there, and wrap that in a paragraph. And then the same thing here for the next one, wrap that in a paragraph. And finally, we have an address at the end of the document, and we'll go ahead and wrap that in a paragraph as well.
Now, you may notice that sometimes you see this formatted slightly differently. For example, you can see that right now this line and their two opening and closing paragraph tags are on the same line. Well, occasionally you may see code represented like this. It doesn't really matter which one you use. However, this is using three lines of code instead of two, so you're better off just keeping them all in the same line. But if it helps you read it to place them on separate lines, there's really nothing wrong with that at all.
So if I save this, and preview this in my browser, I can see that what updates--not so much up here, because those guys were already sort of displaying as if they were paragraphs, but the three paragraphs down at the bottom are really sort of changed. Now remember, just because something is displaying as a paragraph doesn't mean that it is. These elements that we have up here, they were displaying as a paragraph, but they really weren't, so to other user agents that can cause a problem. One of the little-known facts about paragraphs is that especially since HTML5 came out, the closing tag of a paragraph is optional.
You don't really need it, in terms of having the browser parse it correctly. Now in the spec, it will tell you that you need it, but you don't really. So for example, if I were to take the closing paragraph tag off of this paragraph and save that and go back into my browser and refresh the page, there's no visual change; in fact, there's no change at all. What happens is this opening tag tells the user agent, hey, I'm a paragraph, it reads through it and then by the time it gets to this tag, it goes, oh, well, the paragraph is over, so it just ends it for you.
Just because you can do something doesn't mean you should, and leaving that optional closing tag off will actually fail in a lot of validators, so there's no real good reason to leave it off. I just want to let you know that that is actually optional when it comes to the opening and closing tags. You may have also noticed in the browser that we have a specific amount of whitespace above and below margins, if you will, these paragraphs, and there is no mechanism in HTML to control the spacing of those vertical margins. They are controlled through styles.
Each browser or user agent has its own default styles that tells it how much space to give between each paragraph. By using CSS you can control that. Overall paragraphs, they are pretty straightforward. Just make sure to avoid having empty paragraphs in your code, and make sure that all your paragraph's opening and closing tags are wrapped correctly around the appropriate page elements.
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.