Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
In XHTML and HTML Essential Training, Bill Weinman helps designers and coders understand XHTML and HTML. In the process, Bill covers document structure, block and inline-level tags, floating images, controlling white space, phrase and font markup, and tables and frames. He even provides a good introduction to CSS. Bill offers step-by-step guidance for building a complete working web site. Exercise files accompany the course.
In our lesson on paragraphs, we talked about how the browser automatically formats a paragraph taking multiple occurrences of white space, spaces, new lines, etcetera and folds them into one space for the purpose of formatting the paragraph. In this lesson, we are going to talk about how to control the line breaks in the browser. Here in our Editor on the top of the screen, we have a couple of paragraphs and we are displaying those paragraphs in the browser on the bottom of the screen. Let me go ahead and shrink the browser down so that the paragraphs wrap a little bit.
For the purposes these lessons we can show how to control those wraps. You will notice that the first paragraph up here extends past the Editor window with no wraps whatsoever and you will notice that down in the browser it wraps normally. You will notice that the second paragraph is wrapped normally on the screen and it wraps normally down here in the browser as well. So this first paragraph goes beyond the Editor window and if we break it at some place, let's break it over here, and save it, of course this does not affect how it's rendered in the browser whatsoever.
So how do we actually get aligned to break where we intend it to? For this purpose we use the BR tag, br, and it's a standalone tag. It does not have any content. So we use the shortcut notation here to satisfy XHTML's need for beginning and an end tag. If we save this and reload it in the browser, you will notice that the paragraph breaks there and yet the paragraph is still formatted as a paragraph. In other words, it has got extra space between the two paragraphs and this is normal behavior for block elements in a browser window.
Now we'll go ahead and take that out and the paragraph is back the way it was and save, reload it in the browser so we can see that intentional line break is done. Now how do we get a line break to not happen? You will notice that between the words Notice and How over here that there is line break that's happened, because the browser decided that it needs to wrap there in order to make it into a paragraph. What if we wanted to not wrap there? If I go ahead and put in a non-breaking space here, this is a special character entity called non-breaking space, then this will prevent a break between these two words and these two words will be on one line, so they will get wrapped down to the next line.
Before we save this and look at how it works, let's talk about the character entity here. You notice it has an ampersand at the beginning and a semicolon at the end and in between the ampersand and the semicolon we have letters, nbsp. This is how you use a character entity in HTML and XHTML. The ampersand denotes the beginning of the character entity and the semicolon is the end of it and in between you have the designation of the character entity. In this case, it's a named entity and it's named nbsp, which stands for non-breaking space.
So go ahead and save this and reload the browser and you will see that now the paragraph does not wrap between the words Notice and How. It doesn't break there. It only breaks before it or after it and so it takes the whole thing as a one for here and puts it on the next line, and that's how a non-breaking space works in HTML. In fact, if you wanted this paragraph to not wrap at all, you can select the entire line and I'm just going to select it from here to the end and not include that space before & and use the Editor's Search and Replace feature to replace the space.
The Editor fills in whatever you have selected when you do that and replace all the spaces with non-breaking space entities. Replace All in selection, 26 occurrences, Close. I will go ahead and Save and reload in the browser and we see now that that paragraph doesn't break at all. It goes all the way across and you have to scroll sideways in order to see the entire paragraphs. It puts it all on one line and that's because there aren't any spaces that it can break and all the spaces in the paragraph are non-breaking spaces.
I am going to undo this and go ahead replace this element here with a space and character entity non-breaking space entity, save and reload in the browser and we see that our paragraph is now back to normal. There is another way that we can force this paragraph to not break and this is a non-standard element. It is not in the standard and it tends to work in most of the browsers that I have seen including Internet Explorer and Firefox and all the popular browsers. But it's not in any of the standard.
So it's an unsupported rogue non-standard element. It's called a no-break and it's spelled nobr. I'll go ahead and insert it there and at the end of the line. So everything between those beginning and end tag will not break. So I'm going to Save that and reload in the browser and you see we now have a non-breaking paragraph. So this is shortcut for making a non-breaking paragraph, should you actually want one and you don't feel like search and replace all the spaces in the paragraph.
It is not supported. It's not part of the standard. It's just one of those elements that's cropped up out of a necessity, but was never made a part of the standard. So you can use that, if you want to and it will work in most browsers, but just know that it's not part of the standard. So this is how you control the line breaks in a paragraph. You can use the br element to insert a line break where you want it and you can use the nbsp character entity to prevent line breaks where you don't want them and you can use the unsupported non- standard no-break tag to prevent line breaks for a block of text.
Find answers to the most frequently asked questions about XHTML and HTML Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
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.