绝对定位不适用于XHTML?

Nir*_* O. 2 html css xhtml


我试图用绝对定位来定位DIV,但是看起来使用XHTML DOCTYPE它不起作用.在下面的示例中,红色方块显示在左上角,而不是位于屏幕中间的某个位置:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="" media="screen,projection" />
<style type="text/css">

body { background-color: silver; }
div
{
width: 100px;
height: 100px;
border: 1px solid #000;
}
div#one
{
background-color: red;
position: absolute;
top:520;
left: 220; }

</style>
</head>
<body>

<h1>
This is some text this is some text this is some.
</h1>
<div id="one"></div>

</body>
</html> 
Run Code Online (Sandbox Code Playgroud)

如果我将DOCTYPE替换为:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Run Code Online (Sandbox Code Playgroud)

它会正常工作,但我想遵循XHTML doctype.
任何的想法?

thi*_*dot 11

添加px到您的topleft属性:

top: 520px;
left: 220px
Run Code Online (Sandbox Code Playgroud)

使用XHTML doctype(或更准确地说:标准模式),px与Quirks模式相比,浏览器不会假设您的意思是您没有明确说明它.