22

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;
}
oli5679
  • 1,709
  • 1
  • 22
  • 34

4 Answers4

29
  • 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>
Vitorino fernandes
  • 15,794
  • 3
  • 20
  • 39
18

make it simple with flex

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

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

thiru murugan
  • 189
  • 1
  • 4
4

The problem is that you are not targeting the proper inline-block element. :)

.header > div{
  display: inline-block;
}
.playerOne{
  float:right;
}
Stanimir Yakimov
  • 864
  • 3
  • 14
  • 31
2

Maybe using floats?

.playerOne{
    float: left
 }

.playerTwo{
  float: right
}

http://jsfiddle.net/dfLa5nmL/

jpedroribeiro
  • 622
  • 3
  • 11