0

Top Menu

Hello, i have a basic top bar navigation with 2 items , i want to fix the first item to the left and the second to right , for that i tried to use Flexbox , here is my css code :

.ant-menu{
display: flex ;
flex-direction: row ;
background-color: red;
}

.ant-menu .logo {
align-self: flex-start;
float: left ;
margin-right: 400px;
}

.ant-menu .account{
align-self: flex-end;
float: right ;
}

And here my component :

class AntdTopMenu extends React.Component {
state = {
    current: 'mail',
};

handleClick = e => {
    console.log('click ', e);
    this.setState({
        current: e.key,
    });
};

render() {
    return (
        <Menu onClick={this.handleClick} selectedKeys={[this.state.current]} mode="horizontal">

                <Menu.Item className='logo' key="logo">
                    Home
                </Menu.Item>
                <Menu.Item  className='account' key="account" >
                    My Account
                </Menu.Item>

        </Menu>
    );
 }
}

export default AntdTopMenu;
isherwood
  • 58,414
  • 16
  • 114
  • 157
Mickael Zana
  • 261
  • 2
  • 6
  • 22

2 Answers2

4

If you're in flex-direction: row, then use justify-content. The align-* properties would apply to the vertical axis (the cross axis).

.ant-menu{
   display: flex;
   flex-direction: row ;
   background-color: red;
   justify-content: space-between; /* or space-around */
}

You can also use auto margins on the items (full explanation).

Also, floats don't work in a flex container.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

Use justify-content:space-between. You can also give an item inside a flexbox a margin-left:auto to push it all the way to the right:

.flex {
  display: flex;
  background: red;
  justify-content: space-between;
  padding: 5px;
}

a {
  text-decoration: none;
  color: #fff;
  font-family: Arial;
}
<div class="flex">
  <a href="#">Home</a>
  <a href="#">My Account</a>
</div>
symlink
  • 11,984
  • 7
  • 29
  • 50