views:

1494

answers:

12

I don't know why every time I try to include my header using PHP’s include there's a top margin. I checked it using Firebug and it says there's a 22px offset margin on the top. Is anybody experiencing this problem? I think it's a CSS propiety: top: 22px. But nothing can change it even if I write h1 style="top: 0px; margin-top: 0px;". I think it's a php-CSS mystery that will never be solved.

edit: The only way to get rid of that top margin offset or whatever it is, is to add the follow properties to the H1: top: 0px; position: absolute;

Will those properties generate more problems in the future?

is there a better way to solve this top margin-offset problem?

edit2: I think there's a problem with the encoding. Is there a conflict between the encoding of the included file (header.html) and the index file?

My index goes like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    <link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
    <div id="page-wrap">
    <?php include_once("header2.html"); ?>
    </div>
</body>
</html>

With this CSS:

* {
    padding: 0px;
    margin: 0px;
}

My header.html (the one that’s being included):

<h1>Header 2</h1>

And that’s the output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    <link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
    <div id="page-wrap">
    <h1>Header 2</h1> </div>
</body>
</html>
  • God its so simple that I really dont know where the top margin is coming from (in all browsers).
  • It only happens when I use php includes.
  • The source code looks the same as when I dont use php include.
+1  A: 

well, without knowing what's in your php include, it's hard to tell, but make sure there's no CSS inside of it. also make sure that CSS you're loading is getting found and loaded. i debug by changing the background color or something else visual so u can be sure it's loading.

Roy Rico
A: 

Do you have the option to give us a link? I just tried locally a couple of things and you may get this "unwanted" new line for apparently "no reason" at all between your content and the "doctype" declaration, if there is a nonprintable character. Try to delete all characters after the ">" and take a look at it with a hexeditor to make certain of it. Please provide us more info.

merkuro
+1  A: 

You ought to post your generated HTML, like by copying what you see when you select "View Source" from your web browser. Oftentimes, I've seen mysterious blank lines in PHP-generated HTML because PHP is configured to output errors and warnings. Even if this is not caused by that, we can more-easily diagnose the problem if we see the outputted HTML.

Jacob
+1  A: 

i'd bet anything that it has nothing to do with the php include it's self. like previously mentioned, there may be some css in the included file that is doing it, but i've never seen php anything like what you are describing.

i would look at the doctype declaration [temporarily remove it to see what happens], and remove the current html declaration tag and replace it with a plain <html> tag [again just temporarily, to debug]

I think you are right ... The problem should be in the CSS in the include file (header.html). I deleted the doctype declaration and the"xmlns="w3.org/1999/xhtml" but it didn't work. :(
janoChen
+1  A: 

A lot of times an extra new line will be at the end of a PHP script file you are including after the ?> tag. This will cause an extra character to be included in the generated output. You can remedy this problem by not having a closing ?> tag at the very end of your file. PHP knows to "assume" that it's closed.

Nolte Burke
thanks but the file I'm including is just like this: <h1>header 2</h1>it doesn't have any php opening or closing tag.
janoChen
A: 

Thats very interesting, because I just used your markup and your css. I also put it into an php file and included a html file. But there is no margin. Nowhere.

So make sure your browser is able to find the css file with * { margin: 0; padding: 0 }

And I suggest you to use a reset sheet like this one - http://meyerweb.com/eric/tools/css/reset/index.html.

p.s. if you own some webspace, put it online and send us a link.

Tammo
thanks! but the rest didn't work. I tried the code on Safari, Firefox, Chrome ...in the only browser I don't see the top-offset(margin?) is in Opera.
janoChen
+3  A: 

This is pretty weird, but I copied & pasted your output HTML into Notepad++, and there was a strange character just prior to the h1. When pasting it into Notepad, the h in the h1 was subscripted.

Therefore, it looks to me like you may have an erroneous (or unexpected for PHP) character in your included HTML (or in the PHP including it). My suspicion? The dreaded UTF-8 BOM. Try to change the encoding of your included HTML file to eliminate the BOM (which I've always had problems with when dealing with PHP).

Edit: Yep. Just verified with a hex editor. There's a byte-order mark at the beginning of your included HTML. I don't know which editor you use, but you should have the option to change your text encoding settings.

See this for more info about PHP & the BOM.

Jacob
Pretty weird when I leave it utf-8 I don't see anything. But if I change the encoding to big5 or ASCII I see 2 weird characters before "Header 2". When I delete all the encoding (rendering none I guess) I see the same problem of top margin. I starting to think that there's a ghost haunting my html.
janoChen
All UTF-8 is not the same. See if you have the option for UTF-8 with the BOM or UTF-8 without. Which editor are you using?
Jacob
notepad++ and Expression Web because this last one have an option to change and select encoding.
janoChen
Expression web has an option to include BOM but its not checked
janoChen
I use notepad++ also (version 5.1.1), and it has the option under the "Format" menu to "Encode in UTF-8 without BOM". Try changing the encoding, saving, and re-running the PHP.
Jacob
(for header2.html, of course)
Jacob
A: 

I copied your code that you posted for the index.php, style2.css, and header2.html, and am not getting the problem you described. Double check you are not missing anything in what you posted.

(I used UltraEdit)

Joe Bubna
A: 

THANK Jacob it;s working now! I selected encoding: UTF without BOM. I never heard about BOM before. Where is this BOM stored?

janoChen
A: 

Thanks! I have the same problem, and resolve with the answer of Jacob! and save my files in UTF8 whitout BOM

A: 

Dreamweaver drives me nuts with it's automatic BOM additions. Here's the fix:

http://www.adobe.com/support/documentation/en/dreamweaver/mx2004/dwusing_errata/dwusing_errata2.html

Christopher
A: 

I had the same problem. Tried also converting to asci, utf-8, ...still the same. Then, assuming that at the very top of all my CSS div's and classes stays the CSS body property, i whas curious to change the:

"font-size:12px;" to "font-size:0px" ... and voila'!

Am I doin'it wrong? YES? how to fix that?

regards

roXon