html - Div 附加到 chrome 中的 h1

标签 html css

我正在尝试像 stackoverflow 那样在我的页面顶部创建一个栏,顶部的黑色栏。

我正在尝试在其下方创建一个标题。

<html>
<style>
.top{
    background-color: black;
    position:fixed;
    height: 50px;
    width: 110%;
    float: top;
    margin-left: -10px;
    margin-top: -10px;
}

h1{
    text-align: center;
    margin-top: 100px;
}
</style>
<body>
    <div class = "top">
        asdfad
        fasdfasdf
    </div>
    <h1> lol </h1>
</body>
</html>

但是,当我在 Chrome 中创建它时,div 顶部会附加到 H1,就像占据其一半大小一样。怎么了?

最佳答案

我假设您希望您的菜单栏具有固定位置,所以只需将top:0添加到此类:

.top{
    background-color: black;
    position:fixed;
    height: 50px;
    width: 110%;
    /*float: top;   remove this*/
    margin-left: -10px;
    margin-top: -10px;
    top:0 /*add this*/
}

fiddle

另外没有float:top。看这里css float property

关于html - Div 附加到 chrome 中的 h1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24725350/

相关文章:

javascript - 如果选项值可见

html - 重用 CSS 样式并应用于其他 HTML 元素

html - 火狐渲染背景图

html - 为什么我的 CSS 边框会到达页面末尾?

切换到 HTML 5 时,CSS Focus 代码不起作用

javascript - jquery:将样式属性添加到现有样式

javascript - $(*[attr]) 和 $([attr]) 之间的区别

javascript - HTML 数据和一个看起来像数字的字符串

animation - 每个子元素都有延迟的 CSS 动画

php - 使用 ACF 输出单个页眉样式和页脚脚本