How are nested tags in html translated by web browsers and how does order of nesting tags in matter?

被刻印的时光 ゝ 提交于 2019-12-05 19:02:20

For html elements in general, there are 2 major groups, inline elements, for example

<span> <code> <a> <img> <b>

and block elements

<div> <pre> <p> <h1> <ul>

To be nested valid, a block element can contain

  • an inline element
  • a block element

and an inline element can contain

  • an inline element

There are exceptions, though I will not bring them up here.

Also, if one would put a block element inside an inline, it will often still show the same output.
Why? ... Most browsers will try to interpret what one did and try to "fix it and make it work anyway".

So to answer your question

does the ordering of these tags matter?

Yes, in general, block elements are not allowed inside inline elements, but there can be other reasons and exceptions as well.


Regarding your question (comment)

Which is translated first? The code inside <code> or the <pre> tags are given higher priority?

Short answer, the inner element's property, when set, either explicit or predefined, overrides the outer element's equal property.

So in your case, the pre element's white-space property is set, as default, to white-space: pre and as the white-space property is inherited by default, the code uses that value, hence the output looks the same as for the pre element.

But if we were to explicit set the white-space property on the code element, it will behave as you might expect, with no line breaks etc.

#reset code {
  white-space: normal;
}
<div>
  <p>Example: inheritence of property</p>
  <pre>
    <code>
    var person = {
firstName:"John2",
lastName:"Doe"
}  
    </code>
  </pre>
</div>

<hr>

<div id="reset">
  <p>Example: explicit set property</p>
  <pre>
    <code>
    var person = {
firstName:"John2",
lastName:"Doe"
}  
    </code>
  </pre>
</div>

does the ordering of these tags matter? E.g. Are the following equivalent?

Although the output will be equivalent both ways, the ordering does matter when writing valid and semantic html5 code

According to the html5 spec regarding the <pre> element:

Contexts in which this element can be used: Where flow content is expected.

whereas regarding the <code> element:

Contexts in which this element can be used: Where phrasing content is expected.

(Phrasing content is the text of the document, as well as elements that mark up that text at the intra-paragraph level. (spec))

Would there be any technical difference between the above two approaches?

Yes. Nesting the pre element within the code element isn't valid html5 (using this validator) and produces the error:

Error: Element pre not allowed as child of element code in this context.

LGSon

About your comment

What I don't get is in <pre><code>something something</code></pre> The text inside code tags is translated into something which doesn't contain white spaces and line breaks then how does the pre tags preserve the white spaces and line breaks?

In addition to @Danield's great explanation, here's a sample showing the pre tag do preserve white spaces and line breaks.

<div>
  <p>PRE/CODE example:</p>
  <pre>
    <code>
    var person = {
firstName:"John2",
lastName:"Doe"
}  
    </code>
  </pre>
</div>

More to read:

Stack Overflow uses it like this for multiple lines of code:

<pre>
    <code>something</code>
</pre>

The code tag uses a fixed width font, while the pre tag additionally preserves white space and line breaks. The code tag represents computer code, while the pre tag could contain basically anything (computer code in code tags but also some nice formatted piece of text or some ASCII picture etc.).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!