【问题标题】:How to set a margin-top of the size of the windows?如何设置窗口大小的边距顶部?
【发布时间】:2014-04-11 17:04:54
【问题描述】:

我想在当前窗口之后放置一个带有 CSS 的 div(即当您开始滚动时会看到它)。

似乎微不足道,我试过了

#content {
    margin-top: 100%;
}

JSFiddle

但它不起作用,并且边距不占用当前窗口的高度。

【问题讨论】:

  • 我检查了 jsfiddle,它非常适合我。一旦我开始滚动,我就会看到 lorem ipsum。你从哪里开始看到文字?
  • 增加margin 的大小怎么样? margin-top: 120%;jsfiddle.net/Z74dx
  • @Alex 您解决了您的问题还是需要更多帮助?我的回答是否满足您的要求?如果是,请勾选接受:)

标签: html css margin


【解决方案1】:

解决方案:

  1. 你可以使用position:absolute;top:100%;实现你的目标
    FIDDLE
  2. 第二个选项是添加一个带有height:100%; 的元素以“推”.content 向下
    FIDDLE

说明:

问题在于百分比margin-top(如margin-bottompadding-top/bottom)是根据父母的宽度计算的。见here

百分比是指包含块的宽度

CSS:

body,html {
    background-color: lightblue;
    height:100%;
    width:100%;
    margin:0;
}

#content {
    position:absolute;
    top: 100%;
    background-color: lightgrey;
}

代码选项 2:

HTML:

<div id="push"></div>
<div id="content">
    <p>... content ...</p>
</div>

CSS:

body,html {
    background-color: lightblue;
    height:100%;
    width:100%;
    margin:0;
}
#push{
    height:100%;
}

#content {
    background-color: lightgrey;
}

【讨论】:

  • +1 我想知道为什么他们决定将它基于容器的宽度而不是高度,这对于顶部和底部边距是合乎逻辑的。
  • @Alex 用第二个选项更新了我的答案:jsfiddle.net/webtiki/Z74dx/9
猜你喜欢
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
相关资源
最近更新 更多