Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
like image 905
oli5679 Avatar asked Apr 11 '15 16:04

oli5679


People also ask

How do I align two divs horizontally using Flex?

When the flex-direction is column you could use justify-content for vertical alignment and align-items for horizontal alignment. Learn more: How to Center Elements Vertically and Horizontally in Flexbox.

How do I align two divs in a row?

The inherit value makes the div tag to inherit the parent element's value. Hence, in order to display 2 div tag in same line, we need to float the first div to left and second to the right using css float property.


3 Answers

  • 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>
like image 144
Vitorino fernandes Avatar answered Sep 27 '22 21:09

Vitorino fernandes


make it simple with flex

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

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

like image 33
thiru murugan Avatar answered Sep 27 '22 20:09

thiru murugan


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

.header > div{
  display: inline-block;
}
.playerOne{
  float:right;
}
like image 35
Stanimir Yakimov Avatar answered Sep 27 '22 20:09

Stanimir Yakimov