user1896446
user1896446

Reputation:

Margin: 0 auto; not centering because there are divs to the left and right

I am trying to centre the middle circle but I am unable even when set to margin: 0 auto; and display: inline-block; or display table. Any suggestions?

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;
}

Upvotes: 0

Views: 75

Answers (4)

potashin
potashin

Reputation: 44581

Add text-align:center to the #intro (as your inner content acts like inline) and add float:left to your #intro .circle.blue.

Example

Upvotes: 1

TimSPQR
TimSPQR

Reputation: 2984

I'm not sure this is going to work for you - FIDDLE

I put the circles in divs, floated them left, align: center, and gave each one a width of 33%.

They are flexible, and even overlap when the screen is moved horizontally (not sure if that works for you).

CSS

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

Upvotes: 0

Anish Beetun
Anish Beetun

Reputation: 22

I have updated your code: http://jsfiddle.net/h9HGG/6/

The trick is to put each circle centered inside a div which is set to "display:table-cell". Then wrap all the circle inside a container which is set to "display:table".

example:

<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>

Upvotes: 0

Kylok
Kylok

Reputation: 769

Only block-level elements can be centered with margin:0 auto.

You can probably achieve the effect you want in other ways, though.

See this updated version of your jsfiddle for a solution via adding the following CSS to your middle circle:

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

(Note that the negative margin-left must be equal to half the element's width to make it appear centered using this solution.)

Upvotes: 0

Related Questions