the blank space below the <body> tag & script tag and link tag goes under body tag from head tag
-
27-09-2019 - |
Question
hey guys, i wrote a php app using php+smarty.
when i view web source code in firebug, i find that link tag and script tag get under the body tag. but i should be under head tag.
and there are some space below body tag.
and there's blank space on my top of my web page.
so , what's the problem?
Solution
You've got some stray text content inside the <head>
, before the <link>
tag. The browser sees the text and decides this means you're starting the main document body but have forgotten to include the <body>
tag.
This is actually valid—if inadvisable—in HTML4: the <head>
end-tag and <body>
start-tag are both optional. This is how you can have just <html><head><title>x</title>Hello!
as a valid HTML document. But it's not permissible in XHTML, so if you validate your document you should get a “character data is not allowed here” error at the point the stray text occurs.
The browser then parses the rest of the document as body content, putting the <link>
inside the body (which is not valid, but which is nonetheless commonplace). It ignores the real <body>
when that comes along because it already has a body.
If you can't see the stray text, perhaps it's an invisible character like U+00A0 No-break space
or—most likely for Chinese documents—U+3000 Ideographic space
, which you may get when you press space in some input method modes. These characters won't be visible, but they're not ‘ignorable whitespace’ like a normal U+0020 Space or newline, so they trigger ‘text content’ processing and force the <body>
.
OTHER TIPS
Blank spaces, specially at the start of the webpage are usually caused because the file is saved in the format UTF-8 (which contains BOM). If you are using an editor like Notepad++ or Vim, save the file in the format UTF-8 without BOM.
Add the following CSS:
html, body
{
padding: 0px;
margin: 0px;
}
Send your HTML through a validator ( http://validator.w3.org/ ) - it'll tell you what kind of error you've got in there (missing closing tag or something).