Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As you first start authoring HTML, you will notice what seems to be a curious behavior among browsers regarding the use of whitespace. No matter how much whitespace you put into your code, your browser will ignore everything after the first space. So, I've got a little paragraph down here that says, "Try it here. Add as much white space as you want." I'm just going to go ahead and after the sentence Try it here, I am going to go ahead and just do exactly what I told myself to do and add a ton of whitespace there. Okay, fair enough! Save this, preview this in one of my browsers, and right here is where we should expect to see all that whitespace and we don't.
That's simply a default behavior in all browsers. They're basically told to ignore anything past the first character of whitespace. And the reason for that is that a lot of people have different ways that they like to format their code, and a lot of code isn't written by hand; it's written by machines. And so, because of that, there's no real way to consistently say, hey, all whitespace is going to be controlled. So, anything after the first character is ignored. Well, what do you do if you want that whitespace to display? Well, you have a couple of options. The first option I'm going to show you guys is actually something we looked at earlier, which is the preformatted tag.
So, what I'm going to do is I'm going to go in and I'm going to wrap that text inside the paragraph with a pre, or preformatted, tag. Ideally, I would probably replace the paragraph tag, but just to kind of show you guys how this is going to work, let me save this and preview this in my browser, just refresh that. And you can see that now, indeed I do get the whitespace I'm looking for, but something else happens: the text changes to more of a monospaced font, and that's simply the default rendering of preformatted text within the browser. So you could use styles to change that so that looks like the rest of your paragraph text.
Why does that work? The preformatted tag basically tells the browser that however the text is formatted inside this--so no matter how many line breaks it has or how much whitespace--you should honor that spacing. You should honor that particular formatting. That's why it's a very helpful way of showing text that's been tabbed over or indentations or things like that. But you probably are not going to use it just to display whitespace like this, because it's really not designed for that. It's designed to sort of show prebuilt sections of code or poems or things like that.
So, you want to make sure you're using all of your elements the way that they're supposed to be used. You do have another option, and that option is what's known as the nonbreaking space character. As a matter of fact, you can see, right above that we're talking about it right there. I'm going to remove some of this whitespace, just so you can maybe see this a little bit more clearly. If I add one character of whitespace and then I come in and say okay   for non-breaking space, if I save this, go back in and preview it, you can see that I do get an additional character of whitespace there.
And if you're maybe not seeing that, I'm going to copy this and then just paste it, one after the other. Save that, try it, and you can see, it pushed the text over a little bit. Now, even that is really sort of abusing this particular special character, because the purpose of the nonbreaking space is just that: to inform the browser where there is a segment of text that when text wraps or breaks, that these particular strings of text shouldn't be a part of that break.
Let me give you an example. Let's say we have this text right here that says Formula One. If I go back into the browser, you can see that right here it's butting right against the edge of this. If I resize this browser--I can go right up to here--you can see that right now it's allowing it to break between the words Formula and One. However, if I come in and I insert a nonbreaking space right there between the two of them, save that, and preview this again, you can see that now Formula One are not allowed to break, and that's exactly what the nonbreaking space character is supposed to be for.
So, in a pinch, you can certainly use it to add a little bit more whitespace to certain sections of code, but you want to be very careful about how often you use it and in which contexts you use it. It's okay to use them every now and then. As a matter fact, it's perfectly acceptable to add a nonbreaking space when you need just a little bit of extra whitespace, such as before a ZIP code for example. However, make sure that you're very careful about how you use it, and that you don't use it to create false margins, indents, or tabs. That type of visual formatting is responsibility of CSS, not HTML.
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.