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

oli5679 picture oli5679 · Apr 11, 2015 · Viewed 21.6k times · Source

I'm working on a game website and want to place two divs inside a 'header' div such that they are horizontally aligned and to the left and right of this container div. See below for an example:

Oli                                                                             Matt

Here is my attempt. What is my error?

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;
}

Answer

Vitorino fernandes picture Vitorino fernandes · Apr 11, 2015
  • display:inline-block will not create a float issue so there is no need to add clearfix
  • you can also use overflow:hidden instead of display: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>