-1

I tried centering a div within another div. I saw Manoj Kumar's response to this question. But it's not working.

<div class="row">
  <div class="col-sm-9">
    <div class="contAin">
        <div class="tile">Some Content</div>
    </div>
</div>
</div>

and the css

.tile {
  margin-bottom: 30px;  
  width: 80%;
}

.contAin {
  display: flex;
  justify-content: center;
}

The inner div is just aligning itself to the left.

Community
  • 1
  • 1
Rockstar5645
  • 4,376
  • 8
  • 37
  • 62

4 Answers4

3

Remove the display: flex and instead use this CSS rule for .tile:

.tile {
  width: 80%;
  margin: 0 auto 30px;
}

Snippet

.tile {
  margin: 0 auto 30px;  
  width: 80%;
}
<div class="contAin">
    <div class="tile">Some Content</div>
</div>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

You can add the margin-left:auto and margin-right:auto to the same css class and check.

So, the CSS becomes:

.tile {
  margin-bottom: 30px;  
  width: 80%;
  margin-left:auto;
  margin-right:auto;
}
Sravan
  • 18,467
  • 3
  • 30
  • 54
code.rider
  • 1,891
  • 18
  • 21
0

I tried it in jsfiddle, and that's working http://jsfiddle.net/say2vnxb/

.tile {
  margin-bottom: 30px;  
  width: 80%;
  background: yellow;
}

.contAin {
  display: flex;
  justify-content: center;
  border: 1px solid red;
}

What browser do you use?

0

try this in CSS code:

.tile  {margin-right: auto;margin-left: auto;}
Prajwal Shrestha
  • 524
  • 3
  • 12