如何将div中包含两行的div居中?

时间:2017-03-19 20:17:40

标签: html css

我似乎无法将这两行放在容器中间为我的项目。我希望这两行位于div的中心,它们之间有一点距离,但是我无法让它工作。

照片

enter image description here

HTML

<div class="page-wrapper">
  <section id="process_section">
            <h1 class="section-header">Our Process</h1>
            <h4 class="text-center">A quick rundown of how we handle things</h4>
            <div id="card_holder">
                <div class="card-row">
                    <div class="card"> <img src="icons/iconmonstr-payment-14-120.png" alt="Avatar">
                        <div class="container">
                            <h4><b>Payment</b></h4>
                            <p>Payment with Paypal</p>
                        </div>
                    </div>
                    <div class="card"> <img src="icons/iconmonstr-shipping-box-8-120.png" alt="Avatar">
                        <div class="container">
                            <h4><b>Ship</b></h4>
                            <p>Send Us Your Item</p>
                        </div>
                    </div>
                </div>
                <div class="card-row">
                    <div class="card"> <img src="icons/iconmonstr-gear-10-120.png" alt="Avatar">
                        <div class="container">
                            <h4><b>Repair</b></h4>
                            <p>Device is repaired</p>
                        </div>
                    </div>
                    <div class="card"> <img src="icons/iconmonstr-shipping-box-9-120.png" alt="Avatar">
                        <div class="container">
                            <h4><b>Return</b></h4>
                            <p>Return in 4-5 days</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>
</div>

CSS

.page-wrapper {
    width: 1024px;
    margin: 0 auto;
}

#process_section {
    background: #D3D3D3;
}

#card_holder {
    width: 100%;
    display: inline;
    height: 20%;
    margin-left: auto;
    margin-right: auto;
}

#card-row {
    display: block;
}

.card {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    transition: 0.3s;
    width: 200px;
    height: 300px;
    display: inline-block;
    text-align: center;
    margin-right: 10%;
}

.card:hover {
    box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}

.container {
    padding: 2px 16px;
}

的jsfiddle

https://jsfiddle.net/v2vn0wza/

4 个答案:

答案 0 :(得分:1)

添加此样式:

.page-wrapper {
    text-align: center;
}

Updated Fiddle

答案 1 :(得分:1)

更好的方法是.card + .card {margin-left: 10%}

并且对于父行添加text-align:center

以下是代码:https://jsfiddle.net/v2vn0wza/1/

答案 2 :(得分:0)

您可以使用display: flex; justify-content: center;.card-row将内容置于中心位置,并将margin-right上的.card更改为.card:not(:last-child) { margin-right: 10%; },以便不将其应用于.page-wrapper { width: 1024px; margin: 0 auto; } #process_section { background: #D3D3D3; } #card_holder { width: 100%; display: inline; height: 20%; margin-left: auto; margin-right: auto; } .card-row { display: block; display: flex; justify-content: center; } .card { box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); transition: 0.3s; width: 200px; height: 300px; display: inline-block; text-align: center; } .card:not(:last-child) { margin-right: 10%; } .card:hover { box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2); } .container { padding: 2px 16px; }行中的最后一个元素。

<div class="page-wrapper">
  <section id="process_section">
    <h1 class="section-header">Our Process</h1>
    <h4 class="text-center">A quick rundown of how we handle things</h4>
    <div id="card_holder">
      <div class="card-row">
        <div class="card"> <img src="icons/iconmonstr-payment-14-120.png" alt="Avatar">
          <div class="container">
            <h4><b>Payment</b></h4>
            <p>Payment with Paypal</p>
          </div>
        </div>
        <div class="card"> <img src="icons/iconmonstr-shipping-box-8-120.png" alt="Avatar">
          <div class="container">
            <h4><b>Ship</b></h4>
            <p>Send Us Your Item</p>
          </div>
        </div>
      </div>
      <div class="card-row">
        <div class="card"> <img src="icons/iconmonstr-gear-10-120.png" alt="Avatar">
          <div class="container">
            <h4><b>Repair</b></h4>
            <p>Device is repaired</p>
          </div>
        </div>
        <div class="card"> <img src="icons/iconmonstr-shipping-box-9-120.png" alt="Avatar">
          <div class="container">
            <h4><b>Return</b></h4>
            <p>Return in 4-5 days</p>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>
$('#usp-content').text(pOnly);

答案 3 :(得分:0)

您还可以使用display:table / table-cell properties:

https://jsfiddle.net/v2vn0wza/3/

&#13;
&#13;
/* update */

#card_holder {
  display:table;
  margin-left: auto;
  margin-right: auto;
}

.card-row {
  display: table-cell;
}
.page-wrapper {
  max-width: 1024px;
  margin: 0 auto;
}

/* end update */

#process_section {
  background: #D3D3D3;
}


.card {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  transition: 0.3s;
  width: 200px;
  height: 300px;
  text-align: center;
  margin-right: 10%;
}

.card:hover {
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}

.container {
  padding: 2px 16px;
}
&#13;
<div class="page-wrapper">
  <section id="process_section">
    <h1 class="section-header">Our Process</h1>
    <h4 class="text-center">A quick rundown of how we handle things</h4>
    <div id="card_holder">
      <div class="card-row">
        <div class="card"> <img src="https://cdn2.iconfinder.com/data/icons/oxygen/128x128/categories/preferences-desktop-personal.png" alt="Avatar">
          <div class="container">
            <h4><b>Payment</b></h4>
            <p>Payment with Paypal</p>
          </div>
        </div>
        <div class="card"> <img src="https://cdn2.iconfinder.com/data/icons/oxygen/128x128/categories/preferences-desktop-personal.png" alt="Avatar">
          <div class="container">
            <h4><b>Ship</b></h4>
            <p>Send Us Your Item</p>
          </div>
        </div>
      </div>
      <div class="card-row">
        <div class="card"> <img src="https://cdn2.iconfinder.com/data/icons/oxygen/128x128/categories/preferences-desktop-personal.png" alt="Avatar">
          <div class="container">
            <h4><b>Repair</b></h4>
            <p>Device is repaired</p>
          </div>
        </div>
        <div class="card"> <img src="https://cdn2.iconfinder.com/data/icons/oxygen/128x128/categories/preferences-desktop-personal.png" alt="Avatar">
          <div class="container">
            <h4><b>Return</b></h4>
            <p>Return in 4-5 days</p>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>
&#13;
&#13;
&#13;