Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:17:00+00:00 2026-06-15T12:17:00+00:00

You can see my problem in this jsFiddle . I tried using code tags

  • 0

You can see my problem in this jsFiddle.

I tried usingcode tags to distinguish special content, but this quickly backfired on me (as you can see in the above link). When I use Firebug to look at the content, this:

<p>
    This is a sample paragraph with a code block:
    <code>
        <p> Some line of code </p>
        <p> Another line of code </p>
    </code>
</p>

has turned into this:

<p>
    This is a sample paragraph with a code block:
    <code> </code>
</p>
<p>
    <code> Some line of code </code>
</p>
<code>
    <p> Another line of code </p>
</code>

Now, this can be solved by changing <code> to <div class="code"> (as seen in this jsFiddle), but why did the browser do this in the first place, and why did it do it only to the first section in each paragraph?

Firefox, Opera, Chrome, Internet Explorer, Safari – all of them do this, but I’d really like to know why. Does it happen with code only, or will it do this with other tags? And why would browsers move tags around like that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:17:00+00:00Added an answer on June 15, 2026 at 12:17 pm

    HTML places certain restrictions on which elements can be nested in which other elements. Sometimes browsers will happily construct a nonsensical DOM out of certain nesting scenarios, such as a <div> directly in a <ul>. Other times, they absolutely can’t because of other written or unwritten parsing rules, such as <p> elements never containing any other block elements, not even other <p> elements (this is implied by the spec), so they have to work around it by changing the DOM to something that they can work with, resulting in the behavior you observe.

    Because you cannot nest <p> elements within one another at all, what’s happening here is that this element:

        <p> Some line of code </p>
    

    is causing this other element to be terminated:

    <p>
        This is a sample paragraph with a code block:
        <code>
    

    Since there’s an empty <code> tag in there, it’s closed, and the containing <p> closed as well, because a subsequent <p> start tag will automatically close a preceding <p> start tag:

    <p>
        This is a sample paragraph with a code block:
        <code> </code>
    </p>
    

    At this point a browser has to deal with the fact that the <code> and <p> tags are now effectively in the wrong order, but still nested. To compensate for the restructuring of the first “outer” <p> element, and the fact that there was going to be a <code> tag before the second “inner” <p>, it inserts <code> tags into the second <p>, turning its contents into code:

    <p>
        <code> Some line of code </code>
    </p>
    

    Since browsers do seem to allow <p> within <code> for whatever reason (note that at this point the <code> is still not yet explicitly terminated with a </code>), the browser builds the rest of the DOM as follows, before continuing on its way:

    <code>
        <p> Another line of code </p>
    </code>
    

    This is probably consistent across browsers for legacy and cross-browser compatibility reasons; some of these legacy parsing rules have been retconned into sections of the HTML5 spec as well. Unfortunately, I’m not a browser implementer so I can’t list out all possible scenarios; on the other hand, it’s unwise to rely on such details considering the markup you’re writing is invalid in the first place.

    And, finally, today’s highly relevant xkcd (of course):

    Tags

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can see the example here: http://jsfiddle.net/8EHED/8/ This is a tricky problem because I
I can't see this in the docs, but I presume it's a solved problem.
I have a problem when using paperclip. I can see the files uploaded but
I've looked at this over and over again and I can't see the problem.
I have the following problem. I am making this website you can see in
I have a few thoughts on this but I can see problems with both.
I can't see where is problem, but .topbar isn't centered as it should be...
Take a look at this fiddle: JsFiddle As you can see I have put
I have this small testcase: http://jsfiddle.net/sV8js/ You can see that in Chrome (tested on
You can see the issue here: http://jsfiddle.net/qQ6ws/36/ I am using shift to toggle my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.