I have a codepen of my header. How can I align the image to the right of my div?
I have tried justify-content: flex-end
and align-items: flex-end
The way I see it you have 2 options.
I prefer option 1.
With the given code sample, and to right align the image in its parent, the align-items: flex-end
is a flex container property and won't have any effect in the right
rule.
The appropriate property for a flex item would be align-self
, though as the direction is row
, this will still not work since align-*
properties affect the cross axis (vertically).
A simple solution is to remove align-items: flex-end
from the right
rule and instead make the div-container--right
a flex container, use justify-content: flex-end
to push its child, the image, to the right. That will work with the rest of the rules, and your original layout kept.
Stack snippet
.main-container{
background-color: white;
flex: 1;
display: flex;
flex-direction: row;
}
.left{
flex: 2;
background-color:white;
}
.right {
flex: 2;
/*align-items: flex-end; removed */
}
.div-container {
background-color: #90C3D4;
height: 100px;
}
.div-container--left {
border-bottom-left-radius: 10px;
padding-left: 10px;
margin-left: 10px;
}
.div-container--right {
border-bottom-right-radius: 10px;
padding-right: 10px;
margin-right: 10px;
display: flex; /* added */
justify-content: flex-end; /* changed */
align-items: flex-start; /* added, to avoid image to stretch */
}
<div class='main-container'>
<div class="left">
<div class='div-container div-container--left'>
<img src="http://www.mharrisweb.co.uk/images/calendarIcon.png" width="100" />
</div>
<div class='picker-container'>
</div>
</div>
<div class="right">
<div class='div-container div-container--right'>
<img src="http://www.mharrisweb.co.uk/images/logoGreen.png" width="100" />
</div>
<div class='picker-container'>
image above align right
</div>
</div>
</div>
Besides the above, here is a few more ways to align flex items:
An additional option to the good answer from Sten is to use absolute position. Example:
.right{
position:absolute;
right:0;
}