I have a banner a follows:
I need to display Title on left most side and icon on the right most side.
Here is my code:
<div className={classNames.root} role="banner">
<div className={classNames.left}> Title </div>
<div className={classNames.right}><SettingsIcon /></div>
</div>
root: [
{
backgroundColor: theme.palette.themePrimary,
height: 40,
color: 'white',
maxWidth: "100%",
margin: '0 auto',
borderBottom: '1px solid transparent',
boxSizing: 'border-box',
display: 'flex',
paddingLeft: 20,
paddingTop: 10
},
className
],
left: {
float: 'left'
},
right: {
fontSize: 20,
float: 'right'
}
How do I fix this?
3
Answers
You mixed 2 approaches for element placement: flex and floating.
The example with floating:
The example with flexbox:
I can make a suggestion like this; This way, by using justifyContent: ‘space-between’, the items inside will be evenly distributed horizontally between the left and right sides. And with alignItems: ‘center’, the items will be vertically aligned at the center.
If you’re using flex box then set the flex-grow of your left most div to 1. It’ll push the icon all the way to the right. Flex items automatically shrink to the size of their content unless you tell them otherwise.
CSS tricks has a good article on flexbox and here’s a video by Scrimba on flex-grow and flex-shrink
Marked up code below: