gpt4 book ai didi

css - 为什么 IE8 会忽略子元素的边距?

转载 作者:行者123 更新时间:2023-11-28 16:42:20 26 4
gpt4 key购买 nike

当我在以下代码中将 margin 添加到 .child 时,IE8 会忽略它。在现代浏览器中,相同的代码按预期工作。这是什么原因造成的?

<html lang=“de“ xml:lang=“de“ xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv=“Content-Type“ content=“text/html“; charset=“iso-8859-1“ />
<title></title>
<style>
.parent {
margin: 5px;
border: 10px solid blue;
position: relative;
}
.child {
margin: 10px;
border: 10px solid red;
padding: 4px;
}
</style>
</head>

<body>

<div class="parent">
<p class="child" style="width:80%; position:relative; left:10px; top:10px; background-color:yellow;">I'm the CHILD!
<span id="textOutput"></span>
</p>
</div>

</body>

</html>

最佳答案

问题是您没有提供 doctype,这意味着 IE8 不知道要使用什么呈现模式,因此默认为 quirks 模式。 Quirks 模式是必不可少的一个旧的、非标准的布局引擎,在 web 还很年轻的时候使用:

There are now three modes used by the layout engines in web browsers: quirks mode, almost standards mode, and full standards mode. In quirks mode, layout emulates nonstandard behavior in Navigator 4 and Internet Explorer 5. This is essential in order to support websites that were built before the widespread adoption of web standards. In full standards mode, the behavior is (hopefully) the behavior described by the HTML and CSS specifications. In almost standards mode, there are only a very small number of quirks implemented.

怪癖模式和标准模式 ( https://developer.mozilla.org/en-US/docs/Quirks_Mode_and_Standards_Mode )

浏览器以不同的方式处理缺少 doctype 的问题,您应该始终确保在 HTML 的开头指定一个,以确保页面呈现的一致性。在撰写本文时,我会推荐 HTML5 doctype,因为它简短、清晰并且早在 IE6 就支持。

<!DOCTYPE html>
<html lang="de" xml:lang="de" xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<style>
.parent {
margin: 5px;
border: 10px solid blue;
position: relative;
}
.child {
margin: 10px;
border: 10px solid red;
padding: 4px;
}
</style>
</head>

<body>
<div class="parent">
<p class="child" style="width:80%; position:relative; left:10px; top:10px; background-color:yellow;">I'm the CHILD!
<span id="textOutput"></span>
</p>
</div>
</body>

</html>

还应注意,您需要为属性值使用普通引号而不是弯引号,并且需要结束 html 标记。

关于css - 为什么 IE8 会忽略子元素的边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33542095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com