保证金顶部:100%获得父宽度值...奇怪

Art*_*Art 7 html css width

在大多数浏览器(即ff,chrome,safari)上,我有一个非常奇怪的"问题".这是示例代码:

<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
        html{
            outline: 1px #0ff solid;
            background: rgba(0,255,255,0.1);
        }
        body{
            margin: 0;
            padding: 0;
            outline: 1px #00f solid;
            background: rgba(0,0,255,0.1);
        }
        #aDiv{
            width: 300px;
            outline: 1px #f00 solid;
            background: rgba(255,0,0,0.2);
        }
        #bDiv{
            margin-top: 100%;
            outline: 1px #0f0 solid;
            background: rgba(0,255,0,0.1);
        }
    </style>
</head>
<body>
    <div id="aDiv">
        <div id="bDiv">
            content
        </div>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当您更改#aDiv宽度时,#bDiv margin-top将更改为相同的值.我不知道它是如何可能的,高度是宽度.无论如何,也许你可以解释一下我发生了什么?

最好的祝福 ;)

D.

Rya*_*nal 13

这实际上是根据规范

<percentage>百分比是根据生成的框的包含块的宽度计算的.请注意,对于"margin-top"和"margin-bottom"也是如此.如果包含块的宽度取决于此元素,则在CSS 2.1中未定义结果布局.

没用,对吗?你有没有考虑使用position: absolutebottom: 0?它们可能更符合您的需求.

  • 什么样的白痴把这个放在标准中......那是......几乎令人难以置信的愚蠢,基于水平值的垂直百分比。 (2认同)

cpt*_*ing 8

您可以尝试视口相对单位(1vw =视口宽度的1%,1vh =视口高度的1%).

试过margin-top: 90vh;