【问题标题】:Margin: 0 auto; not centering because there are divs to the left and right保证金:0自动;不居中,因为左右有 div
【发布时间】:2014-08-02 05:20:16
【问题描述】:

我正在尝试使中间圆圈居中,但即使设置为 margin: 0 auto; 我也无法做到。和显示:内联块;或显示表。有什么建议吗?

JSFiddle

HTML

<div id="intro">
    <p class="body">As part of Science World’s Cycle Safe Initiative we have installed sensors at each of our gates. In the past year we have had over <b>300,000</b> people ride along. Some information we gathered for June included;</p>
    <span class="blue circle">
        <h3>2 - 3PM</h3>
        <p>is the busiest hour</p>
    </span>
    <span class="green circle">
        <h3>117,295</h3>
        <p>riders this month</p>
    </span>
    <span class="navy circle">
        <h3>10%</h3>
        <p>of Vancouverites*</p>
    </span>
</div>

CSS

#intro {
    max-width: 1080px;
    margin: 0 auto;
}
#intro .circle {
    min-width: 230px;
    min-height: 230px;
    display: inline-block;
    text-align: center;
    border-radius: 1000px;
    color: white;
    margin: 60px 0;
}
#intro .circle h3 {
    margin-top: 80px;
    margin-bottom: 0;
    font-size: 2.2em;
}
#intro .circle p {
    margin-top: 0;
}
#intro .circle.blue {
    background: #0079c8;
}
#intro .circle.green {
    background: #2ecc71;
}
#intro .circle.navy {
    background: #34495e;
    float: right;
}

【问题讨论】:

    标签: html css position alignment


    【解决方案1】:

    text-align:center 添加到#intro(因为您的内部内容就像内联一样)并将float:left 添加到您的#intro .circle.blue

    Example

    【讨论】:

      【解决方案2】:

      只有块级元素可以用margin:0 auto居中。

      不过,您或许可以通过其他方式达到您想要的效果。

      请参阅this updated version of your jsfiddle,通过将以下 CSS 添加到您的中间圆圈来获得解决方案:

      position:absolute;
      left:50%;
      margin:-115px;
      

      (请注意,负数margin-left 必须等于元素宽度的一半才能使用此解决方案使其居中显示。)

      【讨论】:

        【解决方案3】:

        我已经更新了你的代码:http://jsfiddle.net/h9HGG/6/

        诀窍是将每个圆中心放在一个设置为“display:table-cell”的 div 内。然后将所有圆圈包裹在一个设置为“display:table”的容器内。

        示例:

        <div class="wrapper"> <!--display: table, width: 100%, table-layout: fixed -->
          <div class="circle-container"> <!--display: table-cell, text-align: center -->
             <!--circle 1-->
          </div>
          <div class="circle-container"> <!--display: table-cell, text-align: center -->
             <!--circle 2-->
          </div>
          <div class="circle-container"> <!--display: table-cell, text-align: center -->
             <!--circle 3-->
          </div>
        </div>
        

        【讨论】:

          【解决方案4】:

          我不确定这是否适合你 - FIDDLE

          我将圆圈放在 div 中,将它们向左浮动,对齐:居中,并为每个圆圈设置 33% 的宽度。

          它们很灵活,甚至在水平移动屏幕时会重叠(不确定这是否适合您)。

          CSS

          .centerme {
                     float: left;
                     width: 33%;
                     text-align: center;
          }
          

          【讨论】:

            猜你喜欢
            • 2017-03-19
            • 2013-10-17
            • 1970-01-01
            • 2023-03-16
            • 2013-09-01
            • 1970-01-01
            • 2014-05-15
            • 2012-05-29
            • 2013-05-01
            相关资源
            最近更新 更多