How do I fix my React Bootstrap Navbar? (Navbar Brand Collapse) Updated - html

I have created a navbar in my React.js website using bootstrap for react. The navbar currently collapses at a certain point however my menu button does not appear. Please could someone check through the code below and let me know what I'm missing/need to do?
I have tried to add in the toggle using an example on a bootstrap tutorial site however it doesn't seem to work, I'm assuming that something is missing? Or i'm doing something wrong?
EDIT: I have fixed the original issue however all the alignment has been thrown off, I have changed the code below to the new code.
I need everything on the navbar desktop view to be aligned right except the logo which should remain where it is.
In tablet view, the navbar is automatically toggled on so always viewable? I don't want this.
In the mobile view, I need the logo to remain where it is and the button to be aligned vertically central and to the right.
Thanks in advance!
Navbar.js:
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import Button from 'react-bootstrap/Button';
import {
Link
} from 'react-router-dom';
class NavBar extends React.Component {
render() {
return (
<Navbar collapseOnSelect expand="lg" className="custom-nav-bg fixed-top">
<Navbar.Brand href="#home">
<Link to="/home"><img className="custom-nav-logo"
src="logohero.png"
alt="StatHero Logo"
width="105px"
height="50px"
/></Link>
</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" className="navbar-toggler-css" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="align-nav">
<Nav.Link className="custom-nav-text" href="#about">
<Link to="/about">ABOUT</Link>
</Nav.Link>
<Nav.Link className="custom-nav-text" href="#stats">
<Link to="/stats">STATS</Link>
</Nav.Link>
<Nav.Link className="custom-nav-text" href="#faqs">
<Link to="/faq">FAQS</Link>
</Nav.Link>
<Nav.Link className="custom-nav-text" href="#contact">
<Link to="/contact">CONTACT</Link>
</Nav.Link>
<Nav.Link className="custom-nav-text" href="#signup">
<Link to="/signup"><Button className="custom-nav-button">SIGN UP</Button></Link>
</Nav.Link>
<Nav.Link className="custom-nav-text" href="#login">
<Link to="/login">LOGIN</Link>
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
)
}
}
export default NavBar;
Navbar CSS (For some context):
background-color: red ! important;
display: flex;
align-items: flex-end;
}
.align-nav {
display: flex;
align-content: flex-end;
}
.custom-nav-bg {
max-width: 100%;
padding-right: 50px;
padding-left: 60px;
border-radius: 0px;
background-color: #132A42;
}
.custom-nav-logo {
height: 100%;
}
a.custom-nav-text:hover {
color: #00DF8D ! important;
font-family: 'Assistant', sans-serif ! important;
}
a.custom-nav-button:hover {
color: #00DF8D ! important;
font-family: 'Assistant', sans-serif ! important;
}
.custom-nav-text {
max-width: 100%;
padding-top:10px ! important;
padding-right:20px ! important;
padding-bottom:10px ! important;
padding-left:20px ! important;
color: white ! important;
font-family: 'Assistant', sans-serif;
font-size: 20px;
font-weight: 600;
}
.custom-nav-button {
padding-right: 15px;
padding-left: 15px;
border-color: #00DF8D;
border-radius: 10px;
background-color: #00DF8D;
font-size: 20px;
font-weight: 600;
}
#media only screen and (max-width: 768px) {
.custom-nav-text {
max-width: 100%;
padding-top:10px ! important;
padding-right:10px ! important;
padding-bottom:10px ! important;
padding-left:10px ! important;
font-size: 15px;
}
.custom-nav-logo {
align-items: flex-start;
justify-content: flex-start;
}
.custom-nav-bg {
padding-left: 0px;
padding-right: 0px;
display: block;
}
}
#media only screen and (max-width: 768px) {
.custom-nav-button {
padding-right: 10px;
padding-left: 10px;
border-color: #00DF8D;
border-radius: 5px;
background-color: #00DF8D;
font-size: 15px;
}
}

This is the example from react-boostrap
import React from "react";
import{ Navbar, Nav, Button, NavDropdown} from "react-bootstrap";
class NavBar extends React.Component {
render() {
return (
<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">Features</Nav.Link>
<Nav.Link href="#pricing">Pricing</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 href="#deets">More deets</Nav.Link>
<Nav.Link eventKey={2} href="#memes">
Dank memes
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
}
export default NavBar;
check also the doc of react-boostrap for the responsive behaviours
https://react-bootstrap.github.io/components/navbar/#navbars-mobile-friendly
I would recommend you to keep align with bootstrap classes for alignment, without touching directly the css (you can do that, but try to use as much as possible their alignments). Have a look in the docs on how to position things.

I'm new to the web , but maybe so like this for the desktop version
import {Navbar,Nav} from 'react-bootstrap';
import '../styless/topBar.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import logo from '../assets/logo.svg';
const Header = () => {
return(
<>
<Navbar collapseOnSelect variant='dark' expand="lg" >
<Navbar.Brand className='logo-app' >
<div class="container-fluid">
<img
src={logo}
width='30'
height='30'
className='d-inline-block align-top'
alt='logo'
/>
</div>
</Navbar.Brand>
<Navbar.Brand className='brand ' href='/'>
Header
</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" className="navbar-toggler-css"/>
<Navbar.Collapse id="responsive-navbar-nav" className = "justify-content-end">
<Nav >
<Nav.Link href='/'>Home</Nav.Link>
<Nav.Link href='/articles'>Article</Nav.Link>
<Nav.Link href ='/about'>About Us</Nav.Link>
<span >
<a className="btn btn_nav btn-dark " href="/login">login</a>
</span>
</Nav>
</Navbar.Collapse>
</Navbar>
</>
)}
export default Header;
'''

Related

How to style navbar logo and navbar title so they don't overlap?

I am trying to make a navbar which kind of looks like this site: https://med.stanford.edu/ultrasound.html.
So far I have done my own styling but one thing I am stuck at is that, when I shorten the width of the browser, the title overlaps my logo. I do not want that. I want to keep the navbar title beside the logo always like the site I provided. How to achieve that?
My navbar component
import React from "react";
import NavbarItems from "./NavbarItems";
import "./navbar.css";
import logo from "../../logo/logo.png";
import { Image } from "#chakra-ui/image";
import { Text } from "#chakra-ui/react";
function Navbar(props) {
return (
<>
<nav className='NavbarItems'>
<div className='navbar-logo'>
<a href='#'>
<Image src={logo} alt='logo' />
</a>
</div>
<div className='navbar-title'>
<Text>Some text beside the logo</Text>
</div>
<div
className='menu-icon'
onClick={() => props.setIsMenuOpen(!props.isMenuOpen)}
>
<i className={props.isMenuOpen ? "fas fa-times" : "fas fa-bars"}></i>
</div>
<ul className={props.isMenuOpen ? "nav-menu active" : "nav-menu"}>
{NavbarItems.map((item, index) => {
return (
<li key={index}>
<a className={item.cname} href={item.url}>
{item.title}
</a>
</li>
);
})}
</ul>
</nav>
</>
);
}
export default Navbar;
my navbar CSS file
.NavbarItems {
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1rem;
font-weight: 600;
position: -webkit-sticky;
position: fixed;
top: 0;
width: 100%;
z-index: 1;
background: white;
}
.navbar-logo {
position: relative;
justify-self: start;
margin-left: 8px;
margin-right: 8px;
cursor: pointer;
width: 250px;
}
.navbar-title {
position: relative;
justify-self: start;
}
.nav-menu {
display: grid;
grid-template-columns: repeat(5, auto);
grid-gap: 10px;
list-style: none;
text-align: center;
width: 70vw;
justify-content: end;
margin-right: 2rem;
}
There is no need position: relative to this style my opinion.
.navbar-title {
//position: relative;
justify-self: start;
}
It should work for you on your case. Just comment this line and check.
<ul class="nav nav-pills">
...
<li role="presentation" class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
Dropdown <span class="caret"></span>
</a>
<ul class="dropdown-menu">
...
</ul>
</li>
...
</ul>
use bootstrap navbar.
clarify your question. your image files such us logos do not found

CSS - Align element to the right of the page

I have a problem with aligning elements to the right of the page as I did use several methods such as float, flex, but none of them work. For easier to understand, I included the picture so that it can describe what I mean. I don't know is there any wrong with my code so can someone help me to solve the problem? Thank you for your help
Home.js:
import React from 'react';
import "./homeStyle.css";
import { Navbar, NavItem, NavDropdown, MenuItem, Nav, Form, FormControl, Button } from 'react-bootstrap';
import "./components/containers/Menu.css";
import "./homeScript";
import { Widget } from '#typeform/embed-react'
import logo from "./components/img/logo.png";
export default function Home() {
return(
<div>
<div className='App tc f3'>
<Navbar bg='dark' expand='lg'>
<Navbar.Brand href="">
<img src={logo}/>
</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className='ml-auto'>
<Nav.Link href="#home"><button type="button">Login</button></Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
</div>
<section id="home">
<div className="inner-width">
<div className="content">
<h1 />
<div className="buttons">
<button>Not Prepare to Part Yet? Click to Learn More</button>
</div>
</div>
</div>
</section>
</div>
)
}
CSS:
*{
margin: 0;
padding:0;
text-decoration: none;
font-family: 'Montserrat', sans-serif;
box-sizing: border-box;
}
#home .inner-width {
display: flex;
align-items: center;
justify-content: right;
height: 100%;
text-align: center;
}
#home .content {
width: 100%;
color: white;
float: right;
}
#home .content h1 {
font-size: 60px;
margin-bottom: 60px;
}
#home .content h1::after {
content: " Family ";
animation: textAnim 10s linear infinite;
}
#keyframes textAnim {
25% {
content: " If not Now, When ? ";
}
50% {
content: " Just in case. ";
}
75% {
content: " For your loved one.";
}
}
Try using css properties position: relative and position: absolute for aligning item.
Short explanation. Position relative is assigned to parent, in this case you can assign to section home. Then, position absolute is assigned to child that want to be aligned. After that, for child element you can use positioning properties like right, top, bottom and left to adjust the positioning.
For example
#home {
position: relative;
width: 100%;
height: 90vh;
}
.content {
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
}

word-wrap not working in react-bootstrap navbar

I am trying to make a responsive navbar in react-bootstrap. Once I get down to h4, i want to start normal word wrapping, but no matter what, the h4 text goes outside the div container on the right when I try to test it's responsiveness in chrome. The nav-bar hovers above a dynamic map which is why the z-value is 2.
Here is the js file:
import React from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import { Navbar, Nav, NavItem, NavDropdown, ToggleButton, ToggleButtonGroup } from 'react-bootstrap';
export default function Routing(props) {
return (
<>
<Router>
<Navbar collapseOnSelect className="navbar">
<div className="h-box">
<Navbar.Brand>
<h4>Portland Watershed Data Dashboard</h4>
</Navbar.Brand>
</div>
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="ml-auto align-items-center">
<Nav.Link>
<Link to="/"><h5 className="tiny link">Home</h5></Link>
</Nav.Link>
<Nav.Link>
<Link to="/page"><h5 className="tiny link">Gradient</h5></Link>
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
<Switch>
<>
<Route exact path="/">
<Home />
</Route>
<Route exact path="/page">
<Page />
</Route>
</>
</Switch>
</Map>
</Router>
</>
)
And here is the css for these elements, I have removed all the text color, background color, and shadowing for readability.
body {
margin: 0px;
padding: 0px;
}
.header {
height: 45px;
position: fixed;
top: 0;
}
.navbar {
left: 0;
position: fixed;
top: 0;
z-index: 2;
width: 100%;
display: block;
}
.h-box {
border: solid black;
padding-top: 0.5vh;
padding-bottom: 0.5vh;
padding-left: 2vh;
padding-right: 1vh;
text-align: center;
}
.tiny, .h-box h4 {
display: block;
}
.link {
margin-right: 2vh;
}
add this className to h4:
<h4 className="text-wrap">Example Headline Here</h4>
should take care of your problem

CSS unecessary space in main when in mobile mode

This is what the site is like when in desktop mode:
Now in mobile mode:
You can see that there is now extra space on the bottom. I make sure to erase margins everywhere but the space still remained there.
Pertinent css code:
.grid-container{
display: grid;
grid-template-areas:
"header"
"main"
"footer";
grid-template-columns: 1fr;
grid-template-rows: 5rem 1fr 3rem;
height: 100%;
}
.main {
grid-area: main;
background-image: url("res/background_min.jpg");
overflow: hidden
}
/*Home Screen*/
.home{
display: flex;
}
.menu-list{
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
height: 100%;
}
.menu-list a {
padding: 0rem;
flex: 0 1 50rem;
margin: 1.5rem;
height: 100%;
width: 100%;
display: flex;
text-decoration: none;
color: white;
}
.menu-list li
{
display: flex;
list-style-type: none;
text-decoration: none;
align-items: center;
justify-content: center;
width: 100%;
height: 10%;
}
.menu-list p {
font-size: 2vw;
}
.section a {
color: #ffffff;
font-weight: bold;
font-size: 2vw;
text-decoration: none;
}
.about {
background-image: linear-gradient(to right, cyan, magenta);
}
.contacts {
background-image: linear-gradient(to right, fuchsia, turquoise);
}
.projects{
background-image: linear-gradient(to right, turquoise, fuchsia);
}
.question-box {
background-image: linear-gradient(to right, magenta, aqua);
}
.home-main-image{
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
max-width: 45%;
}
.home-main-image img{
max-width: 99%;
}
.fade-in {
animation: fadeIn ease 2s;
}
.button{
background-image: linear-gradient(to right, cyan, magenta);
border: none;
text-align: center;
color: white;
text-decoration: none;
font-size: 1vw;
padding: .5rem;
}
Menu list is the vertical column of buttons that should occupy most of the screen on the left, and home-main-image is the giant terminal logo plastered on the right.
Note that the issue happens with a real mobile device too.
Edit: For clarification, this is the extra space:
Edit: added react html:
App.js
function App() {
const [lang, setLang] = useLanguage();
const handleLanguageChange = () => {
if (lang.lang === 'eng'){
setLang('fr')
} else {
setLang('en')
}
}
return (
<BrowserRouter>
<div className="grid-container">
<header className="header">
<div className="brand">
<Link to="/" >
Eduardo Breton
</Link>
</div>
<div className="header-side">
{lang.subtitle}
</div>
<div className="header-right">
<button className="button" onClick={() => handleLanguageChange()}>{lang.traduction} </button>
</div>
<div>
</div>
</header>
<main className="main">
<div className="content">
<Route path="/" exact={true} component={HomeScreen} />
<Route path="/about" component={AboutScreen}/>
<Route path="/contact" component={ContactScreen}/>
<Route path="/project" component={ProjectScreen}/>
<Route path="/question" component={QuestionScreen}/>
</div>
</main>
<footer className="footer">
© 2020 Eduardo Breton
</footer>
</div>
</BrowserRouter>
);
}
HomeScreen.js:
const { Link } = require("react-router-dom");
function HomeScreen() {
const [lang, setLang] = useLanguage();
return <div className="home">
<ul className="menu-list">
<Link to="/about">
<li className="fade-in section about" >
{lang.about}
</li>
</Link>
<Link to="/contact">
<li className="fade-in section about" >
{lang.contact}
</li>
</Link>
<Link to="/project">
<li className="fade-in section about" >
{lang.projects}
</li>
</Link>
<Link to="/question">
<li className="fade-in section about" >
{lang.question}
</li>
</Link>
</ul>
<div className="home-main-image fade-in">
<img src={terminalImage} />
</div>
</div>
}
export default HomeScreen;
Checked answer with the discussion has fixed the issue, changing flex-direction to column on a media screen on main has allowed me to center correctly the contents:
the first thing you need to do is add a meta description for mobile view in the head. These in particular
<meta content="width=device-width, initial-scale=1" name="viewport" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
Next you need to make your site responsive by adding media queries so that the home main image and your buttons become responsive. You should increase the size of the menu buttons and set them to
#media screen and (max-width:650px) {
.menu-list li {
flex-direction:column;
}
}
That way your buttons will be bigger, aligned in the center, AND in a column (this will look better on mobile). You should also change the flex direction of the main image. Also, try adding some padding between the buttons in mobile view, this should also help in taking up the space.

can't get logo to display in navbar html

I am making a simple app, and I can't get a logo to appear in my navbar. It is located in the same folder as my component, and its name is "logo.jpg". Can someone please help. I have been fiddling with it for some time now and am unable to get the logo to display. Thanks!
Here is the code for my component:
import React, {Component} from 'react';
import {Navbar, Nav,NavDropdown, Form, FormControl, Button} from 'react-bootstrap';
import './Topbar.css'
class Topbar extends Component {
render() {
return (
<div>
<div class="topnav">
<a class="active" href="#home">Home</a>
<a class="navbar-brand" href="/">
<div class="logo-image">
<img src="logo" alt ="a" class="img-fluid"/>
</div>
</a>
News
Contact
About
</div>
</div>
)
}
}
export default Topbar;
//and my css
.topnav {
background-color: #FFFF00;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #808080;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.logo-image{
width: 46px;
height: 46px;
border-radius: 50%;
overflow: hidden;
margin-top: -6px;
}
Thanks! Really appreciate it!
For development purposes, just import the image from the current location with a relative path and then use the url provided in the variable img and set the src of the image tag with it.
import React, {Component} from 'react';
import {Navbar, Nav,NavDropdown, Form, FormControl, Button} from 'react-bootstrap';
import img from './logo.png'
import './Topbar.css'
class Topbar extends Component {
render() {
return (
<div>
<div class="topnav">
<a class="active" href="#home">Home</a>
<a class="navbar-brand" href="/">
<div class="logo-image">
<img src={img} alt ="a" class="img-fluid"/>
</div>
</a>
News
Contact
About
</div>
</div>
)
}
}
Insert the logo‘s path into the src attribute like e.g. <img src="/logo.jpg" alt ="a" class="img-fluid"/>
Note that the logo path is relative to the location of the underlying file.