absolute positioning not working with XHTML?

眉间皱痕 提交于 2019-12-20 02:58:11

问题



I'm trying to position a DIV with absolute positioning, however it appears that with XHTML DOCTYPE it doesn't work. In the following example, the red square appears at top-left, instead of being somewhere in the middle of the screen :

<!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> 

If appears that if I replace the DOCTYPE to this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

It will work correctly, however I would like to follow XHTML doctype.
Any idea?


回答1:


Add px to your top and left properties:

top: 520px;
left: 220px

With the XHTML doctype (or more precisely: Standards Mode), the browser will not assume that you mean px if you do not explicitly state it, in contrast to Quirks Mode.




回答2:


In XHTML absolute positioned elements with no content become "invisible" because they get width 0 and height 0. Try adding content to your div#one or try the following CSS

div#one
{
background-color: red;
position: absolute;
top:520px;
left: 220px; 
height: 100px;
width: 100px;
}


来源:https://stackoverflow.com/questions/4932480/absolute-positioning-not-working-with-xhtml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!