React bootstrap Navbar: How to right align a navbar item

后端 未结 7 1380
独厮守ぢ
独厮守ぢ 2021-01-05 01:51

I\'m trying to right align a navbar item (Contribute) within a navbar.js but I can\'t seem to figure it out. The navbar is a React component and looks like the

相关标签:
7条回答
  • 2021-01-05 02:25

    If you want to make your navigation look something like as shown in below screen shot:

    Then you would need to apply the class container-fluid on Nav and class ml-auto on the Nav.Item on the navigation item which you wish to right align.

    Below is the code:

    <Navbar bg="dark" variant="dark">
      <Nav className="container-fluid">
        <Nav.Item>
          <Navbar.Brand as={Link} to="/">Demo App</Navbar.Brand>
        </Nav.Item>
        <Nav.Item>
          <Nav.Link as={Link} to="/user-list">User List</Nav.Link>
        </Nav.Item>
        <Nav.Item>
          <Nav.Link onClick={handleClickUserLogOut}>Log Out</Nav.Link>
        </Nav.Item>
        <Nav.Item className="ml-auto">
          <Nav.Link>Hi fname lname!</Nav.Link>
        </Nav.Item>
      </Nav>
    </Navbar>
    
    0 讨论(0)
  • 2021-01-05 02:35

    This one works for me

    <Navbar>
        <Navbar.Brand href="/">MyBrand</Navbar.Brand>
        <Navbar.Toggle />
        <Navbar.Collapse>
            <Nav className="justify-content-end" style={{ width: "100%" }}>
                ...
            </Nav>
        </Navbar.Collapse>
    </Navbar>
    
    0 讨论(0)
  • 2021-01-05 02:37

    You can target the dropdown menu with a dropdown-menu-right to align the Contribute nav item right. bootstrap dropdown alignment docs

    0 讨论(0)
  • 2021-01-05 02:41

    The best and easiest approach which works is to add following class to the NAV node like following:

    <Nav className="ml-auto">
    

    Unfortunately adding "pullRight" wasn't the solution and it won't work.

    0 讨论(0)
  • 2021-01-05 02:43

    use the class navbar-right the reach what you want

    0 讨论(0)
  • 2021-01-05 02:43

    The below code solved my issue of alignment.

    var NavMenu = React.createClass({
      render: function(){
        var links = this.props.links.reduce(function(acc, current){      
          current.dropdown ? acc.rightNav.push(current) : acc.leftNav.push(current);
          return acc;
        }, { leftNav: [], rightNav: [] });
        return (
          <div>
            <ul className="nav navbar-nav">
              {links.leftNav.map( function(link) {
                return <NavLink key={link.text} linkTo={link.linkTo} text={link.text} active={link.active} />
              })}
            </ul>
            {
              links.rightNav.length > 0 ?
                <ul className="nav navbar-nav navbar-right">
                  {
                    links.rightNav.map( function(link) {
                      return <NavLinkDropdown key={link.text} links={link.links} text={link.text} active={link.active} />
                    })
                  }
                </ul> : false
            }
          </div>
        );
      }
    });
    
    0 讨论(0)
提交回复
热议问题