【问题标题】:What is the difference between this 2 margins? [duplicate]这两个边距有什么区别? [复制]
【发布时间】:2013-12-17 15:15:22
【问题描述】:

我正在为大学做的作业是制作一个网站。 所以我有一个名为 container 的 div id,它是我的 div,它将定义我所有页面的宽度。 所以我的问题是老师说放

#container {
    width:Anyvaluepx;
     margin:auto;

}

所以我一直在寻找使容器居中的方法,我看到人们说要放这样的东西:

#container {
  width:valuepx;
   margin 0 auto;

}

我认为我的 html 文件不是必需的,但我也会发布:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Eagle's Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
</head>

<body>
<div id="container">
<div id="header">
</div>
<div id="nav">
</div>
<div id="content">
</div>
<div id="footer">
 </div>






</div> <!--Finish Container div-->
</body>
</html>

所以我的问题很简单 use margin:auto; 之间有什么区别?和边距:0 自动;?

感谢所有问题,所以边距适用于使用屏幕分辨率重新调整网站大小,对吗?所以最好让所有自动?还是 0 自动;?

感谢您的宝贵时间。

【问题讨论】:

    标签: html css


    【解决方案1】:

    两者都会让你的 div 水平居中。添加 0 确保顶部和底部边距为零。它不必与对齐中心做任何事情。更多infi参考以下帖子。

    What does auto do in margin:0 auto?

    【讨论】:

      【解决方案2】:

      margin: 0 auto居中 容器的正确方法。不同之处在于margin: auto 混淆了垂直轴和水平轴,而后者侧重于水平轴。

      【讨论】:

        【解决方案3】:

        区别在于margin: auto;将所有边距设置为auto;而margin: 0 automargin-topmargin-bottom 都设置为0margin-leftmargin-right 都设置为auto

        margin: auto;
        

        相当于:

        margin-top: auto;
        margin-right: auto;
        margin-bottom: auto;
        margin-left: auto;
        

        margin: 0 auto;
        

        相当于:

        margin-top: 0;
        margin-right: auto;
        margin-bottom: 0;
        margin-left: auto;
        

        同样,

        margin: 0 auto 10px;
        

        相当于:

        margin-top: 0;
        margin-right: auto;
        margin-bottom: 10px;
        margin-left: auto;
        

        参考资料:

        【讨论】:

        • 嗯所以我的老师错了?所以正确的方法是总是使用 margin:0 auto;?谢谢。
        • 不,上边距和下边距可以auto,对于没有绝对定位的框,它们只会默认为零。
        • @Carlo:同意,只是删除了那个断言(经过片刻的思考)。
        猜你喜欢
        • 1970-01-01
        • 2016-05-08
        • 2019-11-15
        • 2012-03-21
        • 2011-01-14
        • 2013-02-19
        • 2016-06-07
        • 2020-07-22
        • 2014-02-08
        相关资源
        最近更新 更多