1

http://jsfiddle.net/yr15y98e/

How would I go about centering the "CENTER"(yellow) div in the fiddle.

<div id="container">
<div id="leftdiv">left</div>
<div id="middlediv">middle</div>
<div id="rightdiv">right</div>
</div>

3 Answers3

1

add a float:left to your left div, then center by applying text-align:center to your container:

#container {
    height: 100px;
    width: 200px;
    background-color: grey;
    text-align:center; /* ADD THIS */
}
#container div {
    display: inline-block;
}
#rightdiv {
    background-color: blue;
    float: right;
}
#middlediv {   
    background-color: yellow;
}
#leftdiv {
    background-color: red;
    float:left;  /* ADD THIS */
}

http://jsfiddle.net/yr15y98e/1/

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
1

You can also use display: flex and justify-content: space-between;

*{
    padding: 0;
    margin: 0;
}
#container {
    height: 100px;
    width: 100%;
    background-color: grey;
    display: flex;
    justify-content: space-between;
}
#rightdiv {background-color: blue;}
#middlediv {background-color: yellow;}
#leftdiv {background-color: red;}
<div id="container">
    <div id="leftdiv">left</div>
    <div id="middlediv">middle</div>
    <div id="rightdiv">right</div>
</div>
Dmitriy
  • 4,475
  • 3
  • 29
  • 37
0

You need to change the order of html like below:

<div id="container">
<div id="leftdiv">left</div>
<div id="rightdiv">right</div>
<div id="middlediv">middle</div>
</div>

And apply margin auto on middlediv like this:

#container {
    height: 100px;
    width: 200px;
    background-color: grey;
}
#container div {
    display: inline-block;
}
#rightdiv {
    background-color: blue;
    float: right;
}
#middlediv {
    background-color: yellow;
    margin: 0 auto;/*center the div*/
}
#leftdiv {
    background-color: red;
    float: left;
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231