我已经制作了一个菜单条,我希望它固定在页面的底部中心.我已经尝试了一切.有没有办法在CSS中这样做?我已经将菜单固定在页面底部
底部:0px位置:固定
但使用
保证金:自动自动0px自动或保证金左:自动
似乎并没有使菜单居中.它只是粘在页面的左侧.任何想法将不胜感激!
您可以使用50%的左侧属性和相等于页脚宽度一半的负左边距.
#footer {
width: 600px;
position: fixed;
bottom: 0;
left: 50%;
margin-left: -300px;
}
Run Code Online (Sandbox Code Playgroud)
要使元素居中,您需要指定宽度并将左右边距设置为自动。然后要将这样的元素粘贴到页面底部,您需要将其包装在另一个具有固定宽度的元素中,例如 100% 并位于底部。是的,CSS 可以。
<section style="position: fixed; bottom: 0px; width: 100%;">
<p style="margin: 0 auto; width: 300px;">Blah blah</p>
</section>
Run Code Online (Sandbox Code Playgroud)
display: flex现在使这变得非常容易!见下文:
.footer {
position: fixed;
bottom: 0;
width: 100%;
display: flex;
justify-content: center;
}
.content {
background: grey;
}Run Code Online (Sandbox Code Playgroud)
<div class="footer">
<div class="content">My bottom-fixed content</div>
</div>Run Code Online (Sandbox Code Playgroud)
使用此方案,无需设置固定宽度,可以更加灵活。