Michelle Dimwaite
Michelle Dimwaite

Reputation: 115

Trying to center 2 groups of 2 divs each at the middle of a wrapper

I have four divs(all same sizes) i have grouped them into to blocks of 2 with classes blocka both contained in an inner container body-content float all the blocks to the left so when my page width changes,these the blocks(of 2) aligned under each other. I am trying to get now is that those divs sit right at the middle of the main container.

Everything response well. All i need it the class body content sit at the middle of main while blocka all center themselves in the middle of body-content

.main {
  max-width: 100%;
  margin-left: 240px;
  background-color: ;
}

.body-content {
  background-color: white;
  margin: 0 auto;
  width: 95%;
  padding: 15px;
  border: 1px solid grey;
  height: 100px;
}

.blocka {
  float: left;
  margin: 0 auto;
}
<div class="main">

  <div class="body-content">
    <div class="blocka">
      <div class="body-content-items">Add All</div>
      <div class="body-content-items">Add All</div>
    </div>
    <div class="blocka">
      <div class="body-content-items">Add All</div>
      <div class="body-content-items">Add All</div>
    </div>
  </div>
</div>

Upvotes: 0

Views: 40

Answers (4)

Carl Binalla
Carl Binalla

Reputation: 5401

I don't really understand so please clarify.

Here you go, the body-content is sitting in the middle of main, while blocka is sitting in the middle of body-content. You didn't said where will the main be position so I left it in the right-side.

Colors are for visualization and I reduced the body-content's width to see the main behind it

.main {
  max-width: 80%;
  margin: 0 auto;
  background-color: red;
}

.body-content {
  background-color: blue;
  margin: 0 auto;
  width: 50%;
  padding: 15px;
  border: 1px solid grey;
  height: 100px;
  text-align:center;
}
<div class="main">

  <div class="body-content">
    <div class="blocka">
      <div class="body-content-items">Add All</div>
      <div class="body-content-items">Add All</div>
    </div>
    <div class="blocka">
      <div class="body-content-items">Add All</div>
      <div class="body-content-items">Add All</div>
    </div>
  </div>
</div>

Upvotes: 1

johnashu
johnashu

Reputation: 2211

The body-content in this instance is the container.. Anytihng inside body will be flexed as defined..

.body-content{    
    display: flex;
    justify-content: center;
    align-content: center;
    align-items: center;
    flex-direction: column
}

.blocka {
    flex:1
}

Upvotes: 2

tao
tao

Reputation: 90068

Have you considered flexbox?

body {
  margin: 0;
}

.body-content {
  display: flex;
  min-height: 100vh;
  justify-content: center;
  align-items: center;
}

.blocka {
  display: flex;
  flex-direction: column;
}

.body-content-items {
  min-width: 35vw;
  min-height: 35vh;
  margin: 5px;
  background-color: #f5f5f5;
  padding: 1rem;
  box-sizing: border-box;
}
<div class="main">
  <div class="body-content">
    <div class="blocka">
      <div class="body-content-items">Add All</div>
      <div class="body-content-items">Add All</div>
    </div>
    <div class="blocka">
      <div class="body-content-items">Add All</div>
      <div class="body-content-items">Add All</div>
    </div>
  </div>

Feel free to set fixed sizes for items (instead of 35vw/35vh). I was just showing you it's possible. Changing their size will keep them centered.

Upvotes: 2

Gerard
Gerard

Reputation: 15786

Try the following:

.main {
  max-width: 100%;
  background-color: ;
  text-align: center;
}

.body-content {
  background-color: white;
  margin: 0 auto;
  width: 95%;
  padding: 15px;
  border: 1px solid grey;
  height: 100px;
}

.blocka {
  margin: 0 auto;
  display: inline-block;
}
<div class="main">
  <div class="body-content">
    <div class="blocka">
      <div class="body-content-items">Add All</div>
      <div class="body-content-items">Add All</div>
    </div>
    <div class="blocka">
      <div class="body-content-items">Add All</div>
      <div class="body-content-items">Add All</div>
    </div>
  </div>
</div>

Upvotes: 2

Related Questions