简体   繁体   中英

Having Trouble Centering a Div

I am trying to have my "boxes" div centered on my page so that everything inside of that div acts as it is acting now but centered on my page instead of pushed to the left.

Unfortunately, the way I visioned my CSS isn't the way it turned out.

If someone wouldn't mind looking over my jsbin http://jsbin.com/ijifah/1 I'd appreciate it.

The end-goal is to have the boxes centered on the page.

  • Initially I thought I would put all three boxes in a div.
  • After, I would make the boxes.
  • Lastly, I would center the div "boxes" that contains all three of my boxes. Unfortunately this isn't working.

I have been using the

 {display:block;
  margin-right: auto;
  margin-left: auto;
  }

To center a lot of the objects in my CSS.

CSS:

.boxes  {
    display: inline-block;
    margin-right: auto;
    margin-left: auto;
    position: absolute;
}
.basecamp {
    width: 160px;
    height: 217px;
    border: 1px solid #E8E8E8;
    padding: 20px 10px 10px 10px;
    font-family: Lucida Grande;
    background: linear-gradient(#FFFFFF,#F0F0F0);
    border-radius: 5px;
    position: relative;
    display: inline-block;
    margin-right: auto;
    margin: 0 20px;
}

.logos {
    position: relative;
    display: block;
    margin-right: auto;
    margin-left: auto;
    margin-bottom: 10px;
}

.basecamp a {
    position: relative;
    display: block;
    margin-right: auto;
    margin-left: auto;
    font-size: 21px;
    color: #3366A7;
    text-align: center;
    font-weight: bold;
    font-family: Lucida Grande;
    margin-bottom: -10px;
}

.logo-headers {
    color:#CC0000;
    text-align: center;
    position: relative;
    font-size: 14px;
    font-family: Lucida Grande;
}

  .logo-p {
position: relative;
text-align: center;
font-family: Lucida Grande;
font-size: 14px;
margin-top: -10px;
 }

HTML:

<body>
  <div class="boxes">
    <div class="basecamp">
        <img src="images/logo-bc.png" alt="logo" class="logos">
        <a href="#">Basecamp</a>
        <h5 class="logo-headers">Manage Projects</h5>
        <p class="logo-p">Used by millions for project management.</p>
    </div>
    <div class="basecamp">
        <img src="images/logo-bc.png" alt="logo" class="logos">
        <a href="#">Basecamp</a>
        <h5 class="logo-headers">Manage Projects</h5>
        <p class="logo-p">Used by millions for project management.</p>
    </div>
    <div class="basecamp">
        <img src="images/logo-bc.png" alt="logo" class="logos">
        <a href="#">Basecamp</a>
        <h5 class="logo-headers">Manage Projects</h5>
        <p class="logo-p">Used by millions for project management.</p>
    </div>
</div>


</body>

Thanks for the Help!

Is this good for you: http://jsbin.com/upuwil/1 ?
These two new css properties do the trick:

.boxes  {
  text-align: center;
  width: 100%;
}

To center a div in CSS I use:

margin:0 auto; 

try that

.boxes  {
   position: relative;
   display:inline-block;
}
<center>
<div class="boxes">
    <div class="basecamp">
        <img src="images/logo-bc.png" alt="logo" class="logos">
        <a href="#">Basecamp</a>
        <h5 class="logo-headers">Manage Projects</h5>
        <p class="logo-p">Used by millions for project management.</p>
    </div>
    <div class="basecamp">
        <img src="images/logo-bc.png" alt="logo" class="logos">
        <a href="#">Basecamp</a>
        <h5 class="logo-headers">Manage Projects</h5>
        <p class="logo-p">Used by millions for project management.</p>
    </div>
    <div class="basecamp">
        <img src="images/logo-bc.png" alt="logo" class="logos">
        <a href="#">Basecamp</a>
        <h5 class="logo-headers">Manage Projects</h5>
        <p class="logo-p">Used by millions for project management.</p>
    </div>
  </div>
</center>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM