Align the two divs horizontally (one on the far left and the other on the far right of the container)

I am working on a game site and I want to place two divs inside the div 'header' so that they are horizontally aligned, and a div to the left and right of this container. The following is an example:

Oli                                                                             Matt

Here is my attempt. What is my mistake?

HTML:

<div class="header">
     <div class="playerOne">
     Oli
     </div>
     <div class="playerTwo">
     Matt
     </div>
</div>

CSS

.header{
  display: inline-block;
}
.playerOne{
    margin-left: 0;
 }

.playerTwo{
  margin-right: 0;
}
+11
source share
4 answers
  • display:inline-blockwon't create a problem floatso there is no need to add clearfix
  • You can also use overflow:hiddeninsteaddisplay:inline-block

.header {
  display: inline-block; 
  width: 100%;
  border: 1px solid red;
}
.playerOne {
  float: right;
}
.playerTwo {
  float: left;
}
<div class="header">
  <div class="playerOne">
    Oli
  </div>
  <div class="playerTwo">
    Matt
  </div>
</div>
Run code

+14
source

, .:)

.header > div{
  display: inline-block;
}
.playerOne{
  float:right;
}
+3

Maybe using float?

.playerOne{
    float: left
 }

.playerTwo{
  float: right
}

http://jsfiddle.net/dfLa5nmL/

+2
source

simplify with flex

.wrapper{ display: flex; justify-content: space-between }

<div class="wrapper"><span>1</span><span>2</span></div>

+2
source

All Articles