cosmichero2025
cosmichero2025

Reputation: 1029

How to center a div with two rows contained in it horizontally?

I can't seem to get these two rows centered in the middle of the container for my project. I want the two rows to be in the center of the div with a little bit of margin between them but I can't get it to work.

Picture

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/

Upvotes: 0

Views: 1713

Answers (4)

G-Cyrillus
G-Cyrillus

Reputation: 106008

You can also use the display:table/table-cell properties :

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

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

Upvotes: 0

Michael Coker
Michael Coker

Reputation: 53709

You can use display: flex; justify-content: center; to .card-row to center the contents, and change margin-right on .card to .card:not(:last-child) { margin-right: 10%; } so that it isn't applied to the last element in the row.

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

Upvotes: 0

Tusko Trush
Tusko Trush

Reputation: 430

the better way is .card + .card {margin-left: 10%}

and for parent row add text-align:center

Here is code: https://jsfiddle.net/v2vn0wza/1/

Upvotes: 1

Rick Hitchcock
Rick Hitchcock

Reputation: 35670

Add this style:

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

Updated Fiddle

Upvotes: 1

Related Questions