我想div是水平居中,css代码是这样的:
<style type="text/css">
#footer{
position: fixed;
bottom: 20px;
background-color: red;
width:500px;
margin: auto;/*left:auto; right:auto;*/
}
</style>
Run Code Online (Sandbox Code Playgroud)
和HTML代码:
<body>
<div id="footer">hello world</div>
</body>
Run Code Online (Sandbox Code Playgroud)
我认为没有必要解释我的CSS代码,它几乎是不言自明的,但div不是水平居中,有没有办法做到这一点?提前致谢.
Sac*_*hin 20
试试这个
#footer{
position: fixed;
bottom: 20px;
background-color: red;
width:80%;
margin: 0 0 0 -40%;
left:50%;
}
Run Code Online (Sandbox Code Playgroud)
这里需要注意的是,margin-left正值的一半为负,width并设定left了身体的50%
这应该适合你.它通过添加容器div来工作.
<style>
#footer-container{
position: fixed;
bottom: 0;
left: 0;
width: 100%;
text-align: center;
}
#footer
{
width:500px;
margin:0 auto;
margin-bottom:20px;
background-color:red;
}
</style>
<div id="footer-container">
<div id="footer">hello world</div>
</div>
Run Code Online (Sandbox Code Playgroud)
小智 6
在其中放置另一个div与相对位置,margin:auto.给固定的100%宽度.
否则你可以用负边距'技巧'破解它
div {
position: fixed;
left: 50%;
width: 500px;
margin-left: -250px;
}
Run Code Online (Sandbox Code Playgroud)