React Bootstrap, Adding Hover Effects to NavItems - css

Brand new to React Bootstrap and I have been trying to add some custom styling to components.
I was able to remove the border radius from the Nav by using chrome inspector to find the classname, but I haven't been able to do the same for adding hover effects to my NavItems.
Here is my component.
<Navbar inverse collapseOnSelect className="nav-bar">
<Navbar.Header>
<Navbar.Brand>
efrt
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<NavItem eventKey={1} href="#">Features</NavItem>
<NavItem eventKey={2} href="#">Who we Are</NavItem>
</Nav>
<Navbar.Form pullRight>
<UniversalButton style="primary" name='Login' />
</Navbar.Form>
<Nav pullRight>
<NavDropdown eventKey={3} title="Signup" id="basic-nav-dropdown">
<MenuItem eventKey={3.3}>Member</MenuItem>
<MenuItem divider />
<MenuItem eventKey={3.3}>Coach</MenuItem>
</NavDropdown>
</Nav>
</Navbar.Collapse>
</Navbar>
Copied over from Chrome inspector, I get the following as the class/elements where I want to make changes
.navbar-inverse .navbar-nav>li>a {
color: #9d9d9d;
}
When I try the following nothing happens. I tried adding custom class names with no effect either.
.navbar-inverse.navbar-nav>li>a:hover {
background: grey;
}
Any help appreciated :)!

You are missing a space after .navbar-inverse as .navbar-nav is a child:
.navbar-inverse .navbar-nav > li > a:hover {
background-color: gray;
}
Running example:
const { Navbar, Nav, NavItem, NavDropdown, MenuItem } = ReactBootstrap;
class App extends React.Component {
render() {
return (
<div>
<Navbar inverse collapseOnSelect className="nav-bar">
<Navbar.Header>
<Navbar.Brand>
efrt
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<NavItem eventKey={1} href="#">
Features
</NavItem>
<NavItem eventKey={2} href="#">
Who we Are
</NavItem>
</Nav>
<Navbar.Form pullRight />
<Nav pullRight>
<NavDropdown eventKey={3} title="Signup" id="basic-nav-dropdown">
<MenuItem eventKey={3.3}>Member</MenuItem>
<MenuItem divider />
<MenuItem eventKey={3.3}>Coach</MenuItem>
</NavDropdown>
</Nav>
</Navbar.Collapse>
</Navbar>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-bootstrap/0.31.3/react-bootstrap.min.js"></script>
<style>
.navbar-inverse .navbar-nav>li>a:hover {
background-color: green;
}
</style>
<div id="root"></div>

Related

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;
}

nth-child selector doesn't work in media query

I have a project built with react and reactstrap (bootstrap 4 components). When I add the code
.col:nth-child(3) {
order: -1 !important;
}
it work great, but when I try to add in a media query it doesn't work.
#media only screen and (max-width: 700) {
.col:nth-child(3) {
order: -1 !important;
max-width: 700px
}}
what is going on here??
it is part of a nav built with reactStrap.
<Nav className='nav'>
<Col>
<Link to='/product'>
<NavItem className='nav-item'>Product</NavItem>
</Link>
</Col>
<Col>
<Link to='/brand'>
<NavItem className='nav-item'>Brands</NavItem>
</Link>
</Col>
<Col className='nav-column'>
<Link to='/'>
<NavItem className='nav-item nav-title'>
<h1 className='title'>
Makeup <span className='bomb'>Bomb</span>
</h1>
</NavItem>
</Link>
</Col>
<Col>
<Link to='/update'>
<NavItem className='nav-item'>Update</NavItem>
</Link>
</Col>
<Col>
<Link to='/about'>
<NavItem className='nav-item'>About</NavItem>
</Link>
</Col>
</Nav>
#media (max-width: 700px) {
.col:nth-child(3) {
order: -1 !important;
}
}
Try this instead, you haven't used PX and did not close the bracket
Here you go - Move screen resolution over 700px to see the color change.
Fiddle for screen visualization.
Nav :nth-child(1) {
order: -1 !important;
color: red;
}
#media only screen and (max-width: 700px) {
Nav :nth-child(1) {
order: -1 !important;
color: blue;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<Nav className='nav'>
<Col class="col">
<Link to='/product'>
<NavItem className='nav-item'>Product</NavItem>
</Link>
</Col>
<Col class="col">
<Link to='/brand'>
<NavItem className='nav-item'>Brands</NavItem>
</Link>
</Col>
<Col className='nav-column'>
<Link to='/'>
<NavItem className='nav-item nav-title'>
<h1 className='title'>
Makeup <span className='bomb'>Bomb</span>
</h1>
</NavItem>
</Link>
</Col>
<Col class="col">
<Link to='/update'>
<NavItem className='nav-item'>Update</NavItem>
</Link>
</Col>
<Col class="col">
<Link to='/about'>
<NavItem className='nav-item'>About</NavItem>
</Link>
</Col>
</Nav>
EDIT:
I decided to change the class .col to an id. This works.
#media only screen and (max-width: 700px) {
#col:nth-child(3) {
order: -1 !important;
}
}

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>
);

Reactstrap - Align navbar items to left and right

I currently have a navbar that has site navigation to the left and login/register/search to the right. When I add a button with an image, the layout of the navbar goes wrong and I cannot solve it. The elements that should be to the right, are aligning more the the middle.
This is my JSX:
<Navbar className="custNav navbar-inverse bg-primary" expand="md">
<NavbarBrand className='custNav' href="/">Brand</NavbarBrand>
<NavbarToggler onClick={this.toggle} />
<Collapse isOpen={this.state.isOpen} navbar>
<Nav className='mr-auto' navbar>
<NavItem className='custNav'>
<NavLink className='custNav' href="/comments">Home</NavLink>
</NavItem>
<NavItem className='custNav'>
<NavLink className='custNav' href="/report">About Us</NavLink>
</NavItem>
<NavItem>
<NavLink className='custNav' href="/report">Contact Us</NavLink>
</NavItem>
</Nav>
<Nav className='rightNav ml-auto' navbar>
<NavItem className=' custNav'>
<NavLink className='custNav' href="/comments">Login</NavLink>
</NavItem>
<NavItem className='custNav'>
<NavLink className='custNav' href="/report">Signup</NavLink>
</NavItem>
<FormGroup className='m-auto'>
<Input type="text" name="search" id="search" placeholder="Search" />
</FormGroup>
<Button className='searchBTN'><img className='btnIMG' src={searchIMG} alt=''/></Button>
</Nav>
</Collapse>
</Navbar>
My CSS:
body{
flex: 1;
}
.custNav{
background-color: #d4452d !important;
color: #f1f2ed !important;
}
.searchNav{
justify-content: center !important;
align-items: flex-start !important;
}
.searchBTN{
background-color: #d4452d !important;
color: #f1f2ed !important;
border-color: #d4452d;
width: 5%;
height: 5%;
}
.btnIMG{
position: relative;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
When I inspected the element, it appears to be the formgroup that is taking up all the room between the search button and the signup button.

React Bootstrap NavBar Container, set width to auto

I am trying to use a NavBar from react bootstrap. However there seems to be a <div class="container"> that adds width. How do I set this width to auto?
I tried the following:
.navbar > .container {
width:auto;
}
But this margin still remains. I also tried to
<Navbar style={{ width: "auto" }}>
<Navbar.Header>
...
But no success. My complete code:
<Row>
<Navbar>
<Navbar.Header>
<Navbar.Brand className="lang-de">
<a href="/" />
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav pullRight>
<NavItem eventKey={1} href="#">
Impressum
</NavItem>
</Nav>
</Navbar.Collapse>
</Navbar>
</Row>
you can use only React Bootstrap Fixed top
<Navbar fixed="top" />
It will auto create the CSS below:
.fixed-top {
position: fixed;
top: 0;
right: 0;
left: 0;
z-index: 1030;
}
You can see the this and another positions in documentation here
In your css, try the following:
.navbar > .container {
width:auto!important;
}
Just add "fluid" next to Navbar on your code
<Row>
<Navbar fluid>
<Navbar.Header>
<Navbar.Brand className="lang-de">
<a href="/" />
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav pullRight>
<NavItem eventKey={1} href="#">
Impressum
</NavItem>
</Nav>
</Navbar.Collapse>
</Navbar>
If you are using the grid system you can set the Container to fluid
<Container fluid>
<NavBar />
<Row>
<div>Personal Digital Assistants</div>
<ProfileCard text="Hello" info='unknown' imageTag={AlexaImg}/>
<ProfileCard text="Sir" info='unknown' imageTag={CortanaImg}/>
<ProfileCard text="Google thing" info='known' imageTag={SiriImg}/>
</Row>
</Container>

Resources