Are leading and trailing spaces ignored in html?

html4

says this :

To avoid issues with SGML line break rules and inconsistencies between existing implementations, authors should not rely on user agents to display white space immediately after the start tag or immediately before the end tag. Thus, authors, and in particular authoring tools, should write:

<P> We offer free technical support </A> for subscribers. </>

and not:

<P> We offer free <A> technical support </A> for subscribers. </P>

and this :

SGML (see [ISO8879], section 7.6.1) specifies that line breaks immediately after the start tag should be ignored, since the line should be broken just before the end tag. This applies to all HTML elements without exception.

The following two HTML examples should render the same:

<P> Thomas is watching TV. </>

     

<p>
  Thomas is watching TV.
  </P>

So there should be the following two examples:

<A> My favorite site </A>

     

& l, A>
  My favorite site
  </A>

Thus, one cannot rely on ignoring them or not. What about html5

?

UPD . Or let us put it this way: can I see them as ignored or sometimes do they matter (manifest in one way or another)? In what cases, if any?

UPD Um, should I say I had a refactoring ...? I am trying to make templates a little more readable, which got me thinking.

+3


source to share


2 answers


Spaces are certainly not ignored in the embedded tags (eg <a>

, <span>

, <strong>

, ...), for example, in this example,

<p>We offer free <a>technical support</a> for subscribers.</p>
<p>We offer free<a> technical support </a>for subscribers.</p>

      

if you set the CSS to something like this

a { text-decoration: underline; }

      

you can definitely see the difference.

Sometimes line breaks can cause strange results in inline tags, for example if you write code like this,



<p>We offer free <a>
technical
support
</a> for subscribers.</p>

      

it seems to ignore the first line break, but not the last.

Here's a script for both examples: http://jsfiddle.net/Niffler/fnnanru2/

Inside tag blocks (i.e. <p>

, <h1>

, <div>

, ...) gaps and breaks at the start or end tags must always be ignored (i.e., <p>test</p>

should look the same as <p> test </p>

).

And as another user wrote in a comment, a line break will usually look just like a space.

In addition, multiple spaces or line breaks, or combinations thereof, are usually summed up in the same space.

+5


source


FOLLOWING spaces are ignored by browsers and spaces ignored by browsers are two completely different questions.

You shouldn't put spaces after the start tag, even if it (sometimes) works. Due to the documentation on this subject, browsers may change to comply with this rule without notice.

They don't have to display the same, but most browsers currently display

<P>We offer free<a> technical support </a>for subscribers.</P>

and



<P>We offer free <a>technical support</a> for subscribers.</P>

same.

Remember: all of this can change without warning, so I will definitely follow the rules of the documentation.

+2


source







All Articles