29

I have a problem on a site that I am making. There is this line of white space at the top of my page. I can not find out where it is coming from. It might be because of extra line breaks in my page thanks to php include() or maybe it is just some faulty css.

Daniel Node.js
  • 6,734
  • 9
  • 35
  • 57

15 Answers15

41

I got it! I must admit, this was a very strange one.

This was my exact problem, and his solution fixed it perfectly.


The problem was because of the include().

When you save a page as UTF-8, a special signature called a byte-order mark (or BOM) is included at the beginning of the file, indicating that it is a UTF-8 file. You can only see BOMs with low level text editors (such as DOS edit). You need to remove this signature from the included file in order to get rid of the white space at the top of the page.

DOS edit

Daniel Node.js
  • 6,734
  • 9
  • 35
  • 57
  • This signature is known as a byte-order mark, or BOM. It's a PHP developer's worst nightmare but also potentially affects just about any other UTF-8 file. – BoltClock Apr 17 '12 at 23:43
  • in mac just create a utf8 file and copy paste the text. The new file will be free from this BOM. – iluvatar_GR May 31 '15 at 09:49
  • The Notepad++ saved my life when it came to this - just select UTF-8 from the encoding menu. They have bom UTF-8 types but the UTF-8 from the encoding menu doesnt include bom. – Michael Cantrall Aug 22 '18 at 18:49
14

In Notepad++ you can change the encoding of the file to "UTF-8 without BOM" from the "Encoding" menu. This fixes the problem for me.

Bruno
  • 1,213
  • 13
  • 14
  • Not for me, I changed to Latin1 with Unix EOL and the *only* thing that worked was removing the closing ?> from the included file. In fact judicious use of printf() showed the spaces inserted at the interface of the included file and the main body. – mckenzm Dec 10 '14 at 19:04
10
  1. Open file in Notepadd++

  2. From top bar: Encoding->Encode in UTF8 without BOM

  3. Save the file

In Dreamweaver

Press CTRL + J or go to Change->Page Settings->Tile/Encoding and unchek UNICODE BOM if it is cheked.

sashkello
  • 17,306
  • 24
  • 81
  • 109
Victor Sitnic
  • 380
  • 2
  • 6
4

Well you're missing a DOCTYPE for starters... You also don't have a character encoding specified.

Aside from that, make sure that if you've saved the files as UTF-8, they must not have a BOM at the start. Also ensure that you didn't leave any empty lines (although whitespace is probably ignored, otherwise every layout I've ever written would break)

0b10011
  • 18,397
  • 4
  • 65
  • 86
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
3

For some reason after your body tag before the first div you have some funky character... just delete them and re-indent as needed. the character  to be precise... (you can see it in Firebug if you inspect the HTML for the page...but you can't see it in the view-source because it renders as whitespace).

unrelated to the alignment issue... you also have a miss-matched closing </li> tag after your login/signup link.

scunliffe
  • 62,582
  • 25
  • 126
  • 161
3

you can solve this problem doing the following...

based in what scunliffe said... you can avoid this character... example

$file = file_get_contents('header.php');
$file = substr($file, 3, strlen($file));
echo $file;

so, you're skipping this bug...

regards

Philippe Boissonneault
  • 3,949
  • 3
  • 26
  • 33
cebasso
  • 31
  • 1
  • verrrrrrrrrrrrrrrrrrrrrrrrrryyyyyyyyyyyyyyyyyyyyyyyyyyyyy tanX ... Goooooood way ... solved for me ... ^_____________________________^ taaaaaaaaaaaaaaaaaaaaanX – Erfan Safarpoor Dec 17 '14 at 19:42
3

Important: If you have multiple php pages as I have in my website using utf-8, you need to make sure you change the encoding of every single page to be "Encode in utf-8 without a BOM".

hagai shaul
  • 35
  • 1
  • 5
1

Yep, there's extra whitespace included before anything else.

May I recommend the use of output buffering to you? You will gain a fine grained control of what is pushed to the client.

aefxx
  • 24,835
  • 6
  • 45
  • 55
0

After you declare a DOCTYPE make sure to add CSS reset into the first line of your CSS code to make it consistent across all browsers :

*{margin:0;
 padding:0;
 }
Dejo Dekic
  • 2,088
  • 4
  • 27
  • 50
0

While reviewing your website output on firebug, i found small issue on top of html rendering. view in screen shot. firebug report

It may be due to missing doctype on top of page. Add the following doctype on top of page and see output again.

<!doctype html>
<html itemtype="http://schema.org/WebPage" xmlns="http://www.w3.org/1999/xhtml"   xmlns:og='http://opengraphprotocol.org/schema/'
xmlns:fb='http://www.facebook.com/2008/fbml'>
irfanmcsd
  • 6,533
  • 7
  • 38
  • 53
0

Tested all suggestion online, solved with notepad, procedure :

  1. Copy all content of the file to include, from php without <?
  2. Open Notepad and type <? then paste all code
  3. Enjoy
Leonardo Ciaccio
  • 2,846
  • 1
  • 15
  • 17
0

This problem has also bothered me for a long time, until I found a very simple solution.

This problem is caused by a BOM character, which is used to tell code page and which endian is of a text file. When you include a PHP file, this character will be included as well. Although you will not see the BOM character, but it might cause gap on your page.

My solution is here. You don't have to remove this character from files. What you have to do is like this:

<!-- <?php
include_once (dirname(__FILE__) . "/_include.php");
?> -->

Using HTML comment mark to wrap php code which includes other php files. And the BOM character will be considered as comment and will not appear on page.

0

BOM Character creates that problem. Sometimes it does not create such visual character that one see. Just check the file editor settings and set to "no BOM" to prevent such blank line.

Sometimes images won't work because of this character, since the character set of image is corrupted by such character.

To avoid such situations, i). check if your Code Editor creates new file with BOM. If so, change this setting. ii). Open all the files of your code and check BOM character at start (

gSeaner
  • 1
  • 1
0

Try this this is the best idea that I know, cheers

<div style="position:absolute;">
<?php include("conn.php"); ?>
</div>
<!DOCTYPE html>
deibu
  • 1
  • 3
0

I just downloaded Notepad++ and should advise UTF-8 without BOM is not an encoding option. Choose only "UTF-8" not the "UTF-8 BOM". That worked.

giusti
  • 3,156
  • 3
  • 29
  • 44
Mauro S
  • 46
  • 5