How to align react bootstrap navbar items? - css

How would I set the registered as and logout button to align it to the right? I tried to use justify content on the navbar.text and set it to align the right. BTW the parent is already set to flex:
<Navbar onToggle={()=>setExpanded(!expanded)} expanded={expanded} bg="primary" variant="dark" expand="lg" fixed="top">
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav>
<Nav.Link as={Link} to="/" className='header-router-a' onClick={() => setExpanded(false)}>
Home
</Nav.Link>
<Nav.Link as={Link} to="/quiz" className='header-router-a' onClick={() => setExpanded(false)}>
Personality Quiz
</Nav.Link>
<Nav.Link as={Link} to="/shopper" className='header-router-a' onClick={() => setExpanded(false)}>
Shopper
</Nav.Link>
<Nav.Link as={Link} to="/contact" className='header-router-a' onClick={() => setExpanded(false)}>
Contact
</Nav.Link>
</Nav>
{loggedin ?
<Nav>
<Navbar.Text className="auth"as={Link} to={`/profile/${state.user}`} onClick={() => setExpanded(false)} >
Signed in as: {state.user}
</Navbar.Text >
<Button onClick = {() => {
localStorage.removeItem("jwtToken");
setExpanded(false)
setAuthToken(false);
dispatch({
type: 'SET_CURRENT_USER',
payload : {}
});
}} >
Log Out
</Button>
</Nav>
: <Button onClick={() => {history.push("/login"); setExpanded(false)}}>
LOG IN
</Button>
}
</Navbar.Collapse>
</Navbar>
.bg-primary{
background-color: #33DBFF;
opacity: 0.95;
}
/*not working*/
.auth {
margin-right: auto;
justify-content: end;
}
this is what it looks like currently:

Child was not nested properly!!!!

Related

How to make links in <NavBar> float right using react-boostrap

I would like all the elements nested inside of the first Nav tag to be on the left side of the NavBar, and all in the second Nav tag to float right. How can I achieve this using react Bootstrap?
I tried doing Nav className="float-right" on the second Nav tag but it didn't work. I also tried className="ml-auto".
<Nav>
<Nav.Link>
{" "}
<Link
to="/booklets"
className="nav-link"
onClick={() => setExpanded(false)}
>
Booklets
</Link>
</Nav.Link>
<Nav.Link>
{" "}
<Link
to="/form"
className="nav-link"
onClick={() => setExpanded(false)}
>
Upload New Booklet
</Link>
</Nav.Link>
<Nav.Link>
{" "}
<Link
to="/about"
className="nav-link"
onClick={() => setExpanded(false)}
>
About
</Link>
</Nav.Link>
</Nav>
<Nav>
<Nav.Link>
{" "}
<Link
to="/login"
className="nav-link"
onClick={() => {
setExpanded(false);
currentUser && logout();
}}
>
{currentUser ? "Logout" : "Login"}
</Link>
</Nav.Link>
<Nav.Link>
{" "}
<Link
to="/register"
className="nav-link"
onClick={() => setExpanded(false)}
>
{!currentUser ? "Register" : ""}
</Link>
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
Assuming that the direct parent of the two <Nav> is <Navbar.Collapse>, perhaps try style the parent to have it place the items each on one side, with flex display.
Example with Bootstrap classes:
<Navbar.Collapse className="d-flex justify-content-between">
Example with inline styles:
<Navbar.Collapse style={{ display: 'flex', justifyContent: 'space-between' }}>

How to place nav.link to the right react-bootstrap

I am trying to place the last Nav.Link (logOut) to the right on the nav bar (see picture) without affecting the other nav.links. I have tried with d-flex and so on but it does not work. What to do in the specific nav.link for getting it to stay right, and still included in the Navbar.collapse?
import "./NavBar.css"
import '#fortawesome/fontawesome-free/css/all.min.css';
export const NavBar = () =>{
return (
<Navbar className="nav-bar" expand="lg" sticky="top">
<Container>
<Navbar.Brand href="#home">Scancar</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="ml-auto">
<Nav.Link href="find">Find booking</Nav.Link>
<Nav.Link href="book">Book</Nav.Link>
<Nav.Link href="pricing">Overview</Nav.Link>
<Nav.Link href="pricing">Release</Nav.Link>
<Nav.Link href="pricing">Request</Nav.Link>
<Nav.Link href="logOut"> <i className="fas fa-sign-out-alt"></i> </Nav.Link>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
);
}
See the navbar
You can add ms-auto
<Nav.Link href="logOut" className="ms-auto"> <i className="fas fa-sign-out-alt"></i> </Nav.Link>

Not able to change color of NavLink (React Bootstrap)

I am having a hard time changing the color of NavLink inside the NavBar when it is inactive and when you hover over it.
<Navbar className="navbar-spacing justify-content-center" expand="lg">
<Navbar.Brand href="#home">
<img
src={logo}
width="270"
className="d-inline-block align-top"
alt="Overland NInja Logo"
/>
</Navbar.Brand>
<Navbar.Toggle aria-controls="on-nav" />
<Navbar.Collapse id="on-nav">
<Nav className="mr-auto" >
<Nav.Link className="inactive" href="#">Home</Nav.Link>
<Nav.Link className="inactive" href="#">Contributors</Nav.Link>
<Nav.Link className="inactive" href="#">Newsletter</Nav.Link>
<Nav.Link className="inactive" href="#">Contact Us</Nav.Link>
</Nav>
<Nav>
<Nav.Link className="inactive" href="#">Browse Overlanding Routes</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
This is my CSS code:
.inactive {
color: #000;
}
.inactive:hover {
color: #1D8F32;
}
The style doesn't seem to be applied and the color is not changing at all (Even when it's active and inactive).
You can pass in a bsPrefix prop to replace the generated css class name of the react bootstrap component.
import { Navbar, Nav } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
import "./styles.css";
const customClass = "myCustomNavLink";
export default function App() {
return (
<Navbar className="navbar-spacing justify-content-center" expand="lg">
<Navbar.Toggle aria-controls="on-nav" />
<Navbar.Collapse id="on-nav">
<Nav className="mr-auto">
<Nav.Link href="#" bsPrefix={customClass}>
Home
</Nav.Link>
<Nav.Link href="#" bsPrefix={customClass}>
Contributors
</Nav.Link>
<Nav.Link href="#" bsPrefix={customClass}>
Newsletter
</Nav.Link>
<Nav.Link href="#" bsPrefix={customClass}>
Contact Us
</Nav.Link>
</Nav>
<Nav>
<Nav.Link className="inactive" href="#">
Browse Overlanding Routes
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
// styles.css
.myCustomNavLink {
color: #000;
}
.myCustomNavLink:hover {
color: #1d8f32;
}
Note that by doing this, the default class of nav-link is no longer applied. As such, you might need to consider copying other nav-link styles to your custom stylesheet (padding, display, etc).
The other option is just matching or increasing the specificity of the bootstrap selector used for Nav.Link
// styles.css
.navbar-light .navbar-nav .nav-link.inactive {
color: #000;
}
.navbar-light .navbar-nav .nav-link.inactive:focus,
.navbar-light .navbar-nav .nav-link.inactive:hover {
color: #1d8f32;
}

Adding custom css to React-Bootstrap Component

How to add some custom CSS styles to the Nav component to manage the padding between the nav items and make it float right of the page?
const navbar = props => (
<Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">
<Navbar.Brand href="#home">React-Bootstrap</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="#features">Home</Nav.Link>
<Nav.Link href="#pricing">About Us</Nav.Link>
<NavDropdown title="Facilities" id="collasible-nav-dropdown">
<NavDropdown.Item href="#action/3.1">Library</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.2">Laboratories</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.3">Transportation</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.3">Hostel</NavDropdown.Item>
</NavDropdown>
<Nav.Link href="#pricing">Gallery</Nav.Link>
<Nav.Link href="#pricing">Event</Nav.Link>
<Nav.Link href="#pricing">Contact Us</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
you would need to override the default CSS of bootstrap, you could check the class names using the inspector of your browser and modify those classes since the react-bootstrap compiles to bootstrap anyways
create a css file called
Navbar.css
.navbar-nav {
float: right !important;
}
.navbar-expand-lg .navbar-collapse {
display: inline !important;
}
import it into navbar.js component
import "./Navbar.css";
const navbar = props => (
<Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">
<Navbar.Brand href="#home">React-Bootstrap</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="#features">Home</Nav.Link>
<Nav.Link href="#pricing">About Us</Nav.Link>
<NavDropdown title="Facilities" id="collasible-nav-dropdown">
<NavDropdown.Item href="#action/3.1">Library</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.2">Laboratories</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.3">Transportation</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.3">Hostel</NavDropdown.Item>
</NavDropdown>
<Nav.Link href="#pricing">Gallery</Nav.Link>
<Nav.Link href="#pricing">Event</Nav.Link>
<Nav.Link href="#pricing">Contact Us</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);

React Bootstrap - position sticky not working

I am using React Bootstrap and the class position-sticky doesn't work at all.
There is not overflow at all levels.
Any hints?
I found out the Container needs height specified.
Adding height: 100% to the parent and top: 0 to the child fixes the issue.
The reason is that the sticky rule uses the height of the parent to calculate its own position.
Using React Bootstrap the className h-100 can be used to set height: 100%
I hope this helps also others :)
<>
<Navbar
sticky="top"
collapseOnSelect
expand="lg"
bg="primary"
variant="dark"
>
<Container className="">
<Navbar.Brand as={Link} to="/">
<img height={"30px"} src={logo} alt="" />
</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="me-auto">
<Nav.Link href="home#services">Services</Nav.Link>
<Nav.Link href="home#experts">Experts</Nav.Link>
<NavDropdown title="Dropdown" id="collasible-nav-dropdown">
<NavDropdown.Item href="#action/3.1">Action</NavDropdown.Item>
<NavDropdown.Item href="#action/3.2">
Another action
</NavDropdown.Item>
<NavDropdown.Item href="#action/3.3">
Something
</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.4">
Separated link
</NavDropdown.Item>
</NavDropdown>
</Nav>
<Nav>
<Nav.Link as={Link} to="/about">
About
</Nav.Link>
<Nav.Link as={Link} to="/login">
Log In
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
</>

Resources