Margin-top: 100% 获取父宽度值...奇怪
我在大多数浏览器(即 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>
当您更改 #aDiv 宽度时,#bDiv margin-top 将更改为相同的值。 我不知道怎么可能,高度变成了宽度。无论如何,也许你们中的一个可以向我解释一下发生了什么事?
最好的问候;)
D.
I have very strange "issue", on most browsers (ie, ff, chrome, safari).
Here is example code:
<!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>
When You change #aDiv width, then #bDiv margin-top will change with same value.
I dont know how it is possible, that height goes to width. Anyway maybe one of You could explain me whats going on?
Best regards ;)
D.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这实际上是根据规范
很没用,对吧?您是否考虑过使用
position:absolute
和bottom:0
?它们可能更符合您的需求。This is actually according to the spec
Pretty useless, right? Have you considered using
position: absolute
andbottom: 0
? They might be more of what you're looking for.您可以尝试视口相对单位(1vw = 视口宽度的 1%,1vh = 视口高度的 1%)。
尝试过
margin-top: 90vh;
吗?You could try viewport relative units ( 1vw = 1% of viewport width, 1vh = 1% of viewport height).
Tried
margin-top: 90vh;
?这并不是一个奇怪的问题。这是因为您已将
margin-top
设置为 100%,因为aDiv
是bDiv
的父级,它采用aDiv 的宽度
为margin-top
。Its not a strange issue. It because you have set the
margin-top
to 100%, sinceaDiv
is the parent ofbDiv
it take the width ofaDiv
asmargin-top
.这完全与 bDiv 上的 CSS
margin-top: 100%;
有关,如果删除此内容,一切都会恢复正常。
It's all to do with your CSS on bDiv having
margin-top: 100%;
If you remove this, everything acts normal again.