偏离起始标签头,元素样式不允许作为元素主体的子元素.(抑制来自此子树的进一步错误)


Stray start tag head, Element style not allowed as child of element body in this context. (Suppressing further errors from this subtree.)

这些验证错误是在我纠正我的代码(把链接到图标在头部)后出现的。我相信这个问题结合在header。php和home。php中,从我尝试解决它的过程来看。验证器还说我的<doctype! html>由于某种原因偏离了。

header。php代码:

<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" href="style/stylesheet.css" title="stylesheet">
<link rel="shortcut icon" property='icon' href="img/favicon.ico">
<?php if(isset($pageStyle)) : ?> 
<style type="text/css">
<?php echo $pageStyle; ?>
</style>
<?php endif; ?>
</head>

home。php代码:

<?php 
include("incl/config.php"); 
$title = "Hem";
$pageId = "hem";
$pageStyle = null;
?>
<?php include("incl/header.php"); ?>
<head>
<style>
html {
    background: url("img/church.jpg") no-repeat center center fixed;
    background-size:cover;
}
</style>
</head>

可以看到,<head>和doctype似乎都没有偏离。你可以看到,<style>也在<head>里面,而不在<body>里面。<style>与背景的原因是设置在home.php,而不是在我的样式表是因为我有不同的图像作为背景在我的网站上的每个页面。这可能是代码中的结构错误吗?

最好是在包含文件中打开<head>,而不是在包含文件中关闭它。然后,您可以选择添加到<head>。然后在主代码中关闭它。

试试这个

header。php代码:

<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" href="style/stylesheet.css" title="stylesheet">
<link rel="shortcut icon" property='icon' href="img/favicon.ico">
<?php if(isset($pageStyle)) : ?> 
<style type="text/css">
<?php echo $pageStyle; ?>
</style>
<?php endif; ?>

home。php代码:

<?php 
include("incl/config.php"); 
$title = "Hem";
$pageId = "hem";
$pageStyle = null;
?>
<?php include("incl/header.php"); ?>
<style>
html {
    background: url("img/church.jpg") no-repeat center center fixed;
    background-size:cover;
}
</style>
</head>