Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About the space and line-break in HTML source code

Sometimes, the space and line-break between two HTML tag will ignored by the browser, sometimes not.

I feel that space and line-break between two line box show, two block box don't show.but how about one line box and one block box?

I want the rule or specification, could you tell me?

like image 458
lovespring Avatar asked Jan 17 '11 15:01

lovespring


People also ask

What is the line break code in HTML?

<br>: The Line Break element. The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.

How do you put a line break in space in HTML?

If you want to prevent a line break between two words, use a non-breaking space. If you want to insert a line break, use the HTML break tag, written as <br>. You don't need a closing tag here — just writing <br> adds a line break.

How do you draw a line break in HTML?

To create line breaks in HTML, use the <br> tag. There is no closing tag necessary.

How do you put space between text lines in HTML?

It turns out that's pretty simple: just use the line-height CSS property and then apply it. I have an example below. Then, you can apply that CSS class to your HTML. Now, the space between the lines changes.


2 Answers

In general, browsers ignore extra white space (extra being more than one space).

If you have at least one space between two inline elements, then the browser will render one space (but only one space no matter how many you have). If you want more than one space to display, use the html character code &nbsp;. That will render as many spaces as you need. (although as Pointy pointed out, it is much better to use CSS for spacing)

As for line breaks, some browsers (such as firefox) will treat a line break the same as a space (where one space will be added no matter how many line breaks you have). Other browsers, however, (such as internet explorer) will ignore the line break completely. To force browsers to render a line break, use the <br> tag.

I hope that cleared everything up!

like image 196
Zak Avatar answered Oct 19 '22 00:10

Zak


The specs say that all spaces, tabs and line-breaks are interpreted as a word separator. So no matter how many spaces, tabs and line-breaks you have it will only be rendered as a single space.

The only exception is the pre-tag. The spaces, line-breaks and tabs in a pre-tag are all shown. You can style every tag as a pre-tag by applying the rule white-space: pre.

Specs: http://www.w3.org/TR/html401/struct/text.html#h-9.1

For showing line-breaks the browser checks the type of the content (inline or block). If it's inline (strong, em, a, ...) it doesn't show an line-break. If it's a block (p, table, div, ...) it shows a line-break before and after the element.

Again with css you can change the block-type (with the display property) and so alter how your site looks.

like image 35
Stefaan Colman Avatar answered Oct 18 '22 23:10

Stefaan Colman