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;
}
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.
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.
display:inline-block
will not create a float
issue so there is no need to add clearfixoverflow: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>
make it simple with flex
.wrapper{
display: flex;
justify-content: space-between
}
<div class="wrapper"><span>1</span><span>2</span></div>
The problem is that you are not targeting the proper inline-block element. :)
.header > div{
display: inline-block;
}
.playerOne{
float:right;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With