gpt4 book ai didi

html - 如何让div在页面的同一时间中心停留在顶部?

转载 作者:太空宇宙 更新时间:2023-11-03 21:40:10 25 4
gpt4 key购买 nike

我有一个高度和宽度固定的 div,我使用左右边距 auto 居中。但我希望这个栏在我的整个页面中都可见,所以我使用 position:absolute 将其粘贴。但随后条形图向左移动,不再居中。如何将栏固定到中心顶部位置?

<div class="bar">bar</div>

.bar{
width: 400px;
height: 40px;
margin-left:auto;
margin-right:auto;
background-color:orange;
}

Here是 fiddle 。

最佳答案

尝试使用 100% 宽度的容器 div,将其固定在顶部,然后将栏置于其中居中。CSS:

.bar{
width: 400px;
height: 40px;
margin:0 auto;
background-color:orange;
}
.container
{
width:100%;
position:fixed;
top:0px;
left:0px;
}

Updated Fiddle

关于html - 如何让div在页面的同一时间中心停留在顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24125751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com