html - Div - margin-top 作为 div 内的百分比

标签 html css

这没有按预期工作。我基本上希望 red div 位于距离 div 顶部 20% 的位置。这是行不通的。我提供了 JS Fiddle 和下面的代码。

HTML

<div id="container">


    <div id="inside"</div>

</div>

CSS

body, html {
    margin: 0 0;
    height: 100%;
}

#container {
    background: yellow;
    height: 60%;
    width: 100%;
}

#inside {
    background: red;
    height: 50%;
    margin-top: 20%;
    width: 100%;
}

JSFiddle - 实时预览 http://jsfiddle.net/ye947tjh/

最佳答案

如果您为边距设置了一个百分比值,它是根据元素的宽度计算的。

查看此处了解更多信息: Why are margin/padding percentages in CSS always calculated against width?

关于html - Div - margin-top 作为 div 内的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002260/

相关文章:

javascript - 将 img.src 设置为 dataUrl 会泄漏内存

html - 如何对齐div右侧的3个按钮

html - 有没有比重复 ../更简单的 HTML 路径方法?

CSS :Before image is knocking text down

html - 在不留空白的情况下使用图层(z-index)

css - 如何更改我的代码以使布局像 css 中的图片

php - php中用于电子邮件内容的样式变量

html - ie8 - 当宽度为 :100% 时边框添加到最大宽度

javascript - Windows Mobile 上的媒体查询检测

css - 为什么内层div没有展开到设置的height/min-height?