gpt4 book ai didi

css - 为什么我不能使用边距 :auto? 将此 div 居中

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

google 拉出来的第一件事是

margin:auto

我试过了,但没有用。

使用检查器,子元素之一似乎占据了包含 div 的整个宽度。

我认为这可能是问题所在。这让我问我如何才能阻止这个子元素占用它没有使用的所有宽度。

下面是一些可能相关的代码:

<style>
#pub_fm{
margin: auto;
}
#pub_fm_tags{
position: relative;
width: 370px;
}
#pub_fm_foo{
margin-top:10px;
position: relative;
clear: both;
}
#pub_fm_space{
height: 10px;
clear: both;
}
</style>

<div id='body_public'>
<div id = 'pub_fm'>
<div id = 'pub_fm_tags'></div>
<div id = 'pub_fm_space'></div>
<div id = 'pub_fm_arcmarks'></div>
</div>
</div>

最佳答案

您还需要在元素上设置一个 width 属性,以便 margin: 0 auto; 起作用。

所以添加:

#pub_fm{
width: 1000px; // Set the width you want
margin: 0 auto;
}

它甚至适用于 width: auto; 和百分比宽度 width: 75%;:

#pub_fm{
width: auto;
margin: 0 auto;
}

#pub_fm{
width: 75%;
margin: 0 auto;
}

关于css - 为什么我不能使用边距 :auto? 将此 div 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31455780/

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