html - CSS 垂直向下移动 div

标签 html css

我试图将另一个 div 内的 div 向下移动一点,但是当我使用时

margin-top: 10px;

它在顶部形成一个白色间隙。 html 如下:

<div id="topb">
    <div id="selection">

    </div>
</div>

CSS 如下:

#topb {
    background: url(images/tomato-background.jpg) no-repeat fixed;
    background-size: cover;
    height: 100%;
    width: 101%;
    margin-left: -10px;
}

#selection {
    background-color: #4d4d4d;
    width: 60%;
    height: 500px;
    margin: auto;
    margin-top: 40px;
}

这是网站的屏幕截图: image

最佳答案

为此,您可以使用position:absolute。这是代码:

#topb {
    background: url(images/tomato-background.jpg) no-repeat fixed;
    background-size: cover;
    height: 100%;
    width: 101%;
    margin-left: -10px;
}

#selection {
    background-color: #4d4d4d;
    width: 60%;
    height: 500px;
    margin: auto;
    position: absolute;
    top: 40px;  /*This is where it is changed as well as the line above*/
}

希望对您有帮助!我认为填充仍然会留下背景,所以这是一个更好的主意。

关于html - CSS 垂直向下移动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40064712/

相关文章:

html - 移动下拉菜单不会消失

html - 使子 Div 整齐地适合父 Div

html - 是否可以创建仅 CSS 的 Accordion ?

html - 每侧的 CSS 图像放置

javascript - 在响应 block 内覆盖图像的最佳方式

html - 如何摆脱我的 div 的这个额外的空白区域?

html - 如何在缩放窗口时将 3 个 div 与 CSS 保持相对位置对齐

IE7 中的 HTML CSS 列表

javascript - 页面在浏览器中被杀死后如何运行任务?

javascript - jQuery - 通过单击包含 div 来提交按钮