CSS 在页面顶部放置 DIV 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4268045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:13:17  来源:igfitidea点击:

Positioning a DIV tag in top of the page

cssweb

提问by Sudantha

I need to Positioning a DIV tag in top of the page like in the StackEchangetop menu bar

我需要像在StackEchange顶部菜单栏中一样在页面顶部定位一个 DIV 标签

i used to following code but there is the space

我曾经遵循代码但有空间

div.topbar{
    width:100%;
    background:#C0C0C0;
    border:1px;
    border-color:gray;
    border-style:solid;
    text-decoration:none;
    font-family:Arial;
    top:auto;
}

回答by mmieluch

You also need to add:

您还需要添加:

position: absolute;
top: 0px;

回答by Agos

position: absoluteis not needed if is the first child of the containing element. Just do a

position: absolute如果是包含元素的第一个子元素,则不需要。做一个

margin: 0
padding: 0

and you'll have the added benefit of the next element in the flow positioned correctly after and not under the top bar.

并且您将获得流程中的下一个元素正确定位在顶部栏之后而不是下方的额外好处。

回答by Fermin

add

添加

div.topbar{
  position:absolute;
  top:0
}

回答by Actorclavilis

Did you try adding position:absolute;and top:0;?

您是否尝试添加position:absolute;top:0;

You may need to clear:both;the next element, though.

不过,您可能需要clear:both;下一个元素。