PHP 包含烦人的上边距

发布于 2024-07-24 10:32:14 字数 1901 浏览 6 评论 0原文

我不知道为什么每次我尝试使用 PHP 的 include 包含我的标头时都会出现顶部边距。 我使用 Firebug 检查了它,它说顶部有 22px 的偏移边距。 有人遇到这个问题吗? 我认为这是CSS属性:top:22px。 但即使我写 h1 style="top: 0px; margin-top: 0px;" 也无法改变它。 我认为这是一个永远无法解开的 php-CSS 之谜。

编辑:摆脱上边距偏移或其他任何内容的唯一方法是将以下属性添加到 H1:top:0px; 位置:绝对;

这些属性将来会产生更多问题吗?

有更好的方法来解决这个顶部边距偏移问题吗?

edit2:我认为编码有问题。 包含文件(header.html)和索引文件的编码是否有冲突?

我的索引是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>

使用这个CSS:

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

我的header.html(正在包含的那个):

<h1>Header 2</h1> 

这就是输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
  • 上帝,它是如此简单,我真的不知道上边距来自哪里(在所有浏览器中)。
  • 仅当我使用 php include 时才会发生这种情况。
  • 源代码看起来与我不使用 php include 时相同。

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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

情栀口红 2024-07-31 10:32:14

这很奇怪,但我复制了 & 将输出的 HTML 粘贴到 Notepad++ 中,在 h1 之前有一个奇怪的字符。 粘贴到记事本时,h1中的h带有下标。

因此,在我看来,您所包含的 HTML(或包含它的 PHP)中可能存在错误(或 PHP 意外的)字符。 我的怀疑? 可怕的 UTF-8 BOM。 尝试更改包含的 HTML 文件的编码以消除 BOM(我在处理 PHP 时总是遇到问题)。

编辑:是的。 刚刚用十六进制编辑器验证。 包含的 HTML 的开头有一个字节顺序标记。 我不知道您使用哪个编辑器,但您应该可以选择更改文本编码设置。

有关 PHP 和 PHP 的更多信息,请参阅。 物料清单。

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.

没有你我更好 2024-07-31 10:32:14

您应该发布生成的 HTML,例如复制从网络浏览器中选择“查看源代码”时看到的内容。 我经常在 PHP 生成的 HTML 中看到神秘的空行,因为 PHP 被配置为输出错误和警告。 即使这不是由此引起的,如果我们看到输出的 HTML,我们也可以更轻松地诊断问题。

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.

画离情绘悲伤 2024-07-31 10:32:14

我遇到了同样的问题...所以我解决这个问题的方法是在第一个文件的开头添加一个 html 开始注释,在第二个文件的开头添加一个 html 结束注释。 这会将 BOM(或文件之间出现的任何内容)包含在注释中,因此不会以 html 形式输出。

例如:

#header.php
<?php ?>
<html>
<!--

#body.php
<?php ?>
-->
<body>
...

我尝试修复notepad++中编码格式之间转换的问题,但无济于事。 所以这只是一个临时修复,直到我找到更好的解决方案。

I experienced the same problem...so what I did to fix it was to add an html opening comment at the beggining of the first file and an html closing comment at the beginning of the second file. This encloses the BOM (or whatever is appearing in between the files) inside a comment so it's not outputted in html.

For example:

#header.php
<?php ?>
<html>
<!--

#body.php
<?php ?>
-->
<body>
...

I tried to fix the problem converting between encoding formats in notepad++ to no avail. So this is only a temporary fix til I find a better solution to the problem.

扬花落满肩 2024-07-31 10:32:14

好吧,如果不知道你的 php include 里有什么,就很难说,但要确保里面没有 CSS。 还要确保您正在加载的 CSS 已被找到并加载。 我通过更改背景颜色或其他视觉内容进行调试,以便您可以确定它正在加载。

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.

﹏雨一样淡蓝的深情 2024-07-31 10:32:14

我敢打赌,它与 php 无关,包括它的 self. 就像前面提到的,包含的文件中可能有一些 css 正在执行此操作,但我从未见过 php 类似您所描述的内容。

我会查看 doctype 声明 [暂时删除它以查看会发生什么],然后删除当前的 html 声明标记并将其替换为普通的 标记[再次只是暂时的,用于调试]

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]

寂寞笑我太脆弱 2024-07-31 10:32:14

很多时候,您在 ?> 后包含的 PHP 脚本文件的末尾会出现额外的新行。 标签。 这将导致生成的输出中包含额外的字符。 您可以通过不关闭来解决这个问题吗?> 标记在文件的最后。 PHP 知道“假设”它是关闭的。

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.

尘曦 2024-07-31 10:32:14

您可以选择给我们一个链接吗? 我只是在本地尝试了一些事情,如果存在不可打印的字符,您可能会在您的内容和“doctype”声明之间明显“毫无理由”地得到这个“不需要的”新行。 尝试删除“>”之后的所有字符 并用十六进制编辑器查看它以确保它。 请向我们提供更多信息。

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.

2024-07-31 10:32:14

这非常有趣,因为我刚刚使用了你的标记和 CSS。 我还将它放入一个 php 文件并包含一个 html 文件。 但没有余量。 无处。

因此,请确保您的浏览器能够找到 css 文件
<代码>* { 边距:0; padding: 0 }

我建议您使用像这样的重置表 - http://meyerweb.com/eric/tools/css/reset/index.html

PS 如果您拥有一些网络空间,请将其放在网上并向我们发送链接。

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.

蓝梦月影 2024-07-31 10:32:14

Dreamweaver 的自动 BOM 添加功能让我抓狂。 修复方法如下:

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

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

勿忘初心 2024-07-31 10:32:14

我正在使用记事本,我从菜单中选择 -> 编码->使用无 BOM 的 UTF-8 编码,效果很好。 应该对每个包含的文件执行此操作。 我认为这就是您问题的解决方案。 祝你今晚愉快。

I am using notepad and I selected from the menu -> Encoding->Encoding with UTF-8 without BOM and it worked just fine. This should be done for every included file. I think this is the solution to your problem. Have a nice evening.

风渺 2024-07-31 10:32:14

为了解决问题,我差点毁了我的键盘。
然后我尝试了 PSPad 编辑器,显式更改 .inc.php< /code> 到 UTF-8 并且成功了!

I almost killed my keyobard trying to solve to problem.
Then I tried PSPad editor, change explicitly .inc or .php to UTF-8 and IT WORKED !!

懒猫 2024-07-31 10:32:14

我曾经遇到过这个问题,这里是解决它的方法 - 它确实是字符集中的问题。我花了 10 个小时才找到它。 在我的情况下,我将所有php文件(所有php文件,所有tpl(如果有)文件)字符集从UTF-8更改为不带bom的UTF-8,然后保存并尝试。 之后,您还可以在 .htaccess 中创建字符串“AddDefaultCharset UTF-8”。

I had one time this problem and here is the way to fix it - its really problem in charset.I have spent 10 hours to find it. In my situation I change at all php files(all php files, all tpl(if you have) files) charset from UTF-8 to UTF-8 without bom and save it and try then. After that you can also make at .htaccess string "AddDefaultCharset UTF-8".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文