Skip to content Skip to sidebar Skip to footer

Two Divs On The Same Row And Center Align Both Of Them

I have two divs like this
Div 1
Div 2
I want them

Solution 1:

You could do this

<divstyle="text-align:center;"><divstyle="border:1px solid #000; display:inline-block;">Div 1</div><divstyle="border:1px solid red; display:inline-block;">Div 2</div></div>

http://jsfiddle.net/jasongennaro/MZrym/

  1. wrap it in a div with text-align:center;
  2. give the innder divs a display:inline-block; instead of a float

Best also to put that css in a stylesheet.

Solution 2:

Could this do for you? Check my JSFiddle

And the code:

HTML

<divclass="container"><divclass="div1">Div 1</div><divclass="div2">Div 2</div></div>

CSS

div.container {
    background-color: #FF0000;
    margin: auto;   
    width: 304px;
}

div.div1 {
    border: 1px solid #000;
    float: left;
    width: 150px;
}

div.div2 {
    border: 1px solid red;
    float: left;
    width: 150px;
}

Solution 3:

both floated divs need to have a width!

set 50% of width to both and it works.

BTW, the outer div, with its margin: 0 auto will only center itself not the ones inside.

Solution 4:

Align to the center, using display: inline-block and text-align: center.

.outerdiv
{
    height:100px;
    width:500px;
    background: red;
    margin: 0 auto;
    text-align: center;
}

.innerdiv
{
    height:40px;
    width: 100px;
    margin: 2px;
    box-sizing: border-box;
    background: green;
    display: inline-block;
}
<divclass="outerdiv"><divclass="innerdiv"></div><divclass="innerdiv"></div></div>

Align to the center using display: flex and justify-content: center

.outerdiv
{
    height:100px;
    width:500px;
    background: red;
    display: flex;
    flex-direction: row;
    justify-content: center;
}

.innerdiv
{
    height:40px;
    width: 100px;
    margin: 2px;
    box-sizing: border-box;
    background: green;
}
<divclass="outerdiv"><divclass="innerdiv"></div><divclass="innerdiv"></div></div>

Align to the center vertically and horizontally using display: flex, justify-content: center and align-items:center.

.outerdiv
{
    height:100px;
    width:500px;
    background: red;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items:center;
}

.innerdiv
{
    height:40px;
    width: 100px;
    margin: 2px;
    box-sizing: border-box;
    background: green;
}
<divclass="outerdiv"><divclass="innerdiv"></div><divclass="innerdiv"></div></div>

Solution 5:

I would vote against display: inline-block since its not supported across browsers, IE < 8 specifically.

.wrapper {
    width:500px; /* Adjust to a total width of both .left and .right */
    margin: 0 auto;
}
.left {
    float: left;
    width: 49%; /* Not 50% because of 1px border. */
    border: 1px solid #000;
}
.right {
    float: right;
    width: 49%; /* Not 50% because of 1px border. */
    border: 1px solid #F00;
}

<div class="wrapper">
    <div class="left">Div 1</div>
    <div class="right">Div 2</div>
</div>

EDIT: If no spacing between the cells is desired just change both .left and .right to use float: left;

Post a Comment for "Two Divs On The Same Row And Center Align Both Of Them"