I am building my first Gatsby/React project and I want to create a navbar. After creating all the components, I discovered that when I reduce my screen size, my dropdown menu is not aligned with its parent (although it is perfectly aligned on big screens). Among other things, I played with @media screen but nothing seems to work as expected. I think one of the problems is that I want the dropdown menu displayed when I hover over it. Please find attached a complete copy of the Navbar component and CSS file I am using.
Gatsby/React:
import { Link } from "gatsby"
import { FiAlignJustify } from "react-icons/fi"
//import logo from "../assets/images/LIT_logo.jpg"
import { Dropdown, DropdownButton, Nav, NavDropdown } from "react-bootstrap"
const NavBar = () => {
const [show, setShow] = useState(false)
// const [logoIsVisible, setLogo] = useState(false)
const [isHovered, setIsHovered] = useState(false)
const [isClicked, setIsClicked] = useState(false)
return (
<nav className="navbar">
<div className="nav-center">
<div className="nav-header">
<button className="nav-btn" onClick={() => setShow(!show)}>
<FiAlignJustify />
</button>
</div>
<div className={show ? "nav-links show-links" : "nav-links"}>
{/*this is the toggle functionality of the nav-btn */}
<Link
to="/about"
className="nav-link"
activeClassName="active-link"
onClick={() => setShow(false)} //when you go to another page, the nav drop-down menu closes
>
about us
</Link>
<Link
to="/news"
className="nav-link"
activeClassName="active-link"
onClick={() => setShow(false)}
>
news
</Link>
<Link
to="/myforms"
className="nav-link"
activeClassName="active-link"
onClick={() => setShow(false)}
>
my forms
</Link>
<NavDropdown
id="nav-dropdown-dark-example"
title="Dropdown"
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
onToggle={() => setIsClicked(!isClicked)}
show={isClicked || isHovered}
className="nav-drop"
>
<NavDropdown.Item href="/">English</NavDropdown.Item>
<NavDropdown.Item href="/">Spanish</NavDropdown.Item>
</NavDropdown>
</div>
</div>
</nav>
)
}
export default NavBar
CSS:
*,
::after,
::before {
box-sizing: border-box;
}
:root {
/* COLORS */
/* primary */
--primary-0: #f0efff;
--primary-50: #d8d6ff;
--primary-100: #c1beff;
--primary-200: #aaa5ff;
--primary-300: #938dff;
--primary-400: #7b74ff;
/* main */
--primary-500: #645cff;
--primary-600: #554ed9;
--primary-700: #4640b3;
--primary-800: #37338c;
--primary-900: #282566;
--primary-1000: #191740;
--primary-1100: #0a0919;
--lit-0: #1e1888;
/* grey */
--grey-0: #fafbfc;
--grey-50: #f2f4f8;
--grey-100: #eaedf3;
--grey-200: #e2e7ef;
--grey-300: #dbe0ea;
--grey-400: #d3dae6;
--grey-500: #cbd3e1;
--grey-600: #adb3bf;
--grey-700: #8e949e;
--grey-800: #70747c;
--grey-900: #51545a;
--grey-1000: #333538;
--grey-1100: #141516;
/* rest */
--black: #222;
--white: #fff;
--red-light: #f8d7da;
--red-dark: #842029;
--green-light: #d1e7dd;
--green-dark: #0f5132;
/* box shadows */
--shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
--shadow-2: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
--shadow-3: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
0 4px 6px -2px rgba(0, 0, 0, 0.05);
--shadow-4: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
0 10px 10px -5px rgba(0, 0, 0, 0.04);
/* typography */
--defaultFontSize: 87.5%;
--headingFont: "Inconsolata", monospace;
--bodyFont: "Montserrat", sans-serif;
--smallText: 0.8em;
--extraSmallText: 0.7em;
/* rest */
--backgroundColor: var(--grey-0);
--textColor: var(--grey-1000);
--borderRadius: 0.25rem;
--letterSpacing: 1spx;
--transition: 0.3s ease-in-out all;
--max-width: 1120px;
--fixed-width: 600px;
}
html {
font-size: var(--defaultFontSize);
}
@media only screen and (min-width: 576px) {
:root {
--defaultFontSize: 100%;
}
}
@media only screen and (min-width: 768px) {
:root {
--defaultFontSize: 112.5%;
}
}
body {
background: var(--backgroundColor);
color: var(--textColor);
font-family: var(--bodyFont);
font-weight: 400;
line-height: 1.75;
}
p {
margin: 0;
margin-bottom: 1rem;
max-width: 40em;
}
h1,
h2,
h3,
h4,
h5 {
margin: 0;
margin-bottom: 1.38rem;
font-family: var(--headingFont);
font-weight: 400;
line-height: 1.3;
text-transform: capitalize;
letter-spacing: var(--letterSpacing);
}
h1 {
font-size: 3.052rem;
}
h2 {
font-size: 2.441rem;
}
h3 {
font-size: 1.953rem;
}
h4 {
font-size: 1.563rem;
}
h5 {
font-size: 1.25rem;
}
small,
.text-small {
font-size: var(--smallText);
}
ul {
padding: 0;
list-style-type: none;
}
a {
text-decoration: none;
}
.img {
width: 100%;
display: block;
object-fit: cover;
}
.form {
width: 100%;
background: var(--white);
border-radius: var(--borderRadius);
box-shadow: var(--shadow-2);
padding: 2rem 2.5rem;
}
label {
display: block;
font-size: var(--smallText);
margin-bottom: 0.5rem;
text-transform: capitalize;
}
input,
textarea {
width: 100%;
padding: 0.375rem 0.75rem;
border-radius: var(--borderRadius);
background: var(--grey-0);
border-color: transparent;
border: 1px solid var(--grey-300);
}
.form-row {
margin-bottom: 1rem;
}
textarea {
width: 100%;
height: 7rem;
}
::placeholder {
color: var(--grey-600);
}
.form-alert {
color: var(--red-dark);
font-size: var(--extraSmallText);
letter-spacing: var(--letterSpacing);
text-transform: capitalize;
}
hr {
border-top: var(--line);
}
/*----------------------------------------------------------------------------------------------*/
.dropdown-toggle {
white-space: nowrap;
color: #28a745;
}
/* .btn-success {
color: #fff;
background-color: #28a745;
border-color: #28a745;
} */
button.dropdown-toggle {
background-color: rgb(64, 64, 119);
}
button.dropdown-toggle:hover {
background-color: rgb(65, 79, 136);
}
.dropdown-toggle:after {
display: inline-block;
margin-left: 0.255em;
vertical-align: 0.255em;
content: "";
border-top: 0.3em solid;
border-right: 0.3em solid transparent;
border-bottom: 0;
border-left: 0.3em solid transparent;
}
/* .drop-content {
display: block;
position: relative;
background-color: var(--lit-0);
margin-left: 20vh;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
padding: 12px 16px;
z-index: 1;
} */
/* .drop-content {
margin-left: 50px;
} */
/* Shows dropdown menu */
.dropdown-menu.show {
display: block;
}
.dropdown-menu {
position: relative;
top: 100%;
left: 0;
z-index: 1000;
display: none;
float: left;
min-width: 10rem;
padding: 0.5rem 0;
font-size: 1rem;
color: #212529;
text-align: left;
list-style: none;
background-color: var(--lit-0);
background-clip: padding-box;
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 0.25rem;
}
/* @media screen and (max-width: 600px) {
.dropdown-menu {
left: 50% !important;
transform: translate(-50%) !important;
}
} */
/* items in the dropdrown */
.dropdown-item {
display: block;
width: 100%;
padding: 0.25rem 1.5rem;
clear: both;
font-weight: 400;
color: #212529;
text-align: inherit;
white-space: nowrap;
background-color: transparent;
border: 0;
color: white;
text-align: center;
}
.dropdown-item:hover {
color: var(--primary-500);
}
.nav-drop:hover {
height: 3.69vh;
}
/* This allow menu to remain open after hovering so we can click links*/
/*----------------------------------------------------------------------*/
button,
.btn {
cursor: pointer;
appearance: none;
color: var(--white);
background: var(--primary-500);
border: none;
border-radius: var(--borderRadius);
letter-spacing: var(--letterSpacing);
padding: 0.375rem 0.75rem;
box-shadow: var(--shadow-1);
transition: var(--transition);
text-transform: capitalize;
}
button:hover,
.btn:hover {
color: var(--white);
background: var(--primary-700);
box-shadow: var(--shadow-2);
}
button.small {
padding: 0.3rem 0.5rem;
font-size: var(--smallText);
}
button.hipster {
color: var(--primary-500);
background: var(--primary-50);
box-shadow: var(--shadow-1);
}
button.hipster:hover {
background: var(--primary-100);
box-shadow: var(--shadow-2);
}
button.block {
width: 100%;
}
/* Basic Classes */
.alert {
padding: 0.375rem 0.75rem;
margin-bottom: 1rem;
border-color: transparent;
border-radius: var(--borderRadius);
}
.alert-success {
color: var(--green-dark);
background-color: var(--green-light);
}
.alert-danger {
color: var(--red-dark);
background-color: var(--red-light);
}
.container {
padding: 5rem 0;
}
.container-center {
width: 90vw;
max-width: var(--max-width);
margin: 0 auto;
}
.page {
width: 90vw;
max-width: var(--max-width);
margin: 0 auto; /* auto keeps the 90% of the page specified in the width at the center of the page */
}
.page {
min-height: calc(100vh - (6rem + 4rem));
}
.title {
text-align: center;
margin-bottom: 2rem;
}
.title-underline {
height: 0.2rem;
width: 7rem;
background: var(--primary-600);
margin: 0 auto;
}
form > *:last-child {
margin-bottom: 0;
}
/* loading */
@keyframes spinner {
to {
transform: rotate(360deg);
}
}
.loading {
width: 6rem;
height: 6rem;
border-radius: 50%;
border: 3px solid #ccc;
border-top-color: var(--clr-primary-5);
animation: spinner 0.6s linear infinite;
}
.loading {
margin: 0 auto;
}
/* form loading */
.form-loading {
position: relative;
}
.form-loading::before {
background: var(--white);
position: absolute;
content: "";
height: 100%;
width: 100%;
top: 0;
left: 0;
border-radius: var(--borderRadius);
opacity: 0.8;
}
@keyframes spinner {
to {
transform: rotate(360deg);
}
}
.form-loading::after {
content: "";
position: absolute;
top: calc(50% - 3rem);
left: calc(50% - 3rem);
width: 6rem;
height: 6rem;
border-radius: 50%;
border: 3px solid #ccc;
border-top-color: var(--clr-primary-5);
animation: spinner 0.6s linear infinite;
}
/*#########*/
/* END OF GLOBALS */
/*#########*/
/*#########*/
/* Navbar */
/*#########*/
.navbar {
/* parent container */
width: 100%;
display: flex;
align-items: center; /* Both this and the line below it were center. However I liked it more when everything was aligned left */
justify-content: center;
background: #1e1888;
}
/*sits in the center of the navbar */
.nav-center {
width: 90vw; /* 90% of the screen width regardless if small or big */
max-width: 100%; /*var(--max-width); our content is never going to be bigger that max width */
/* make the nav-center span all the way accross the navbar so that contact button is all the way to the right*/
}
/* */
.nav-header {
height: 6rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-header img {
width: 80px;
margin-left: -6px;
margin-bottom: -9px;
justify-content: flex-start;
}
.nav-btn {
padding: 0.15rem 0.75rem;
}
.nav-btn svg {
font-size: 1.5rem;
}
.nav-links {
height: 0; /*hide by default */
overflow: hidden; /*when overflow, hide them */
display: flex;
flex-direction: column; /*one on top of each other */
transition: var(--transition);
}
/* change here if you add more links, this makes navbar bigger */
.show-links {
height: 29rem;
}
/* Individual links*/
.nav-link {
display: block;
text-align: center;
font-size: 1.5rem;
text-transform: capitalize;
color: white;
letter-spacing: var(--letterSpacing);
padding: 1rem 0;
border-top: 1px solid var(--grey-500);
transition: var(--transition);
}
.nav-link:hover {
color: var(--primary-500);
}
.active-link {
color: var(--primary-500);
}
.contact-link {
display: flex;
justify-content: center;
}
.contact-link a {
padding: 0.15rem 1rem;
}
/* Logo on navbar will get reduced to 50px when less than 600px screen */
@media screen and (max-width: 600px) {
.nav-header img {
width: 50px;
}
}
/* Logo on navbar will dissapear when smaller than 300px screen is used */
@media screen and (max-width: 300px) {
.nav-header img {
display: none;
}
}
/*All these will apply once the screen is BIGGER than 992px */
@media screen and (min-width: 650px) {
.navbar {
height: 6rem;
}
.nav-center {
display: flex; /* all in one line */
align-items: center; /* align vertically in the center*/
}
.nav-header {
padding: 0;
margin-right: 2rem;
height: auto; /*show them in big screen */
}
.nav-btn {
/* this hides the nav-button once we get to the bigg screen (past 992px) */
display: none;
}
.nav-btn:hover {
display: block;
}
/* margin auto allows for the links to shrink as the screen shrinks */
.nav-links {
margin-left: auto;
margin-right: auto;
height: auto !important; /* once we go to big screen we see the links*/
flex-direction: row; /* now align them as a row*/
align-items: center; /* align vertically in center*/
width: 100%; /* make the links span all across*/
justify-content: flex-end;
}
.nav-link {
padding: 0;
border-top: none;
margin-right: 1rem;
font-size: 1rem;
}
.contact-link {
width: 100%;
justify-content: flex-end;
align-items: center;
margin-right: 0;
margin-left: auto;
}
}
/*
.dropdown-btn {
display: block;
} */
/*#########*/
/* Footer */
/*#########*/
.page-footer {
text-align: center;
height: 4rem;
display: flex;
align-items: center;
justify-content: center; /* change this to move it left and right */
background: var(--black);
color: var(--white);
}
.page-footer h5 {
margin-bottom: 0;
}
.page-footer p {
margin-bottom: 0;
}
.page-footer span,
.page-footer a {
color: var(--primary-500);
}
/*#########*/
/* Error */
/*#########*/
.error-page {
min-height: calc(100vh - (6rem + 4rem));
text-align: center;
padding-top: 3rem;
}
.error-page h1 {
font-size: 9rem;
}
/*#########*/
/* Contact */
/*#########*/
.contact-page {
display: grid;
gap: 2rem 3rem;
padding-bottom: 3rem;
}
@media screen and (min-width: 992px) {
.contact-page {
grid-template-columns: 1fr 1fr;
align-items: center;
}
}
/*#########*/
/* About */
/*#########*/
.about-page h2 {
text-transform: none;
font-weight: bold;
}
.about-page {
display: grid;
gap: 2rem 4rem;
padding-bottom: 3rem;
}
.about-img {
border-radius: var(--borderRadius);
height: 500px;
} /* 500px for the SMALL screen */
@media screen and (min-width: 992px) {
.about-page {
grid-template-columns: 1fr 1fr; /*Since the parent (about-page) is a grid, once the page gets big enough, we set it to 2 columns layout*/
align-items: center;
height: 400px;
}
.about-img {
height: 100%; /* set your height based on your parent (about-page), 100% of the parent */
}
}
.featured-recipes > h5 {
text-align: center;
max-width: 100%;
}
/*#########*/
/* Hero */
/*#########*/
.hero-img {
height: 100%; /* 100% of the size of the parent (hero), using parent to control size of the image*/
border-radius: var(--borderRadius);
}
.hero {
height: 40vh; /* 40% of the size of the window*/
position: relative;
margin-bottom: 2rem;
}
.hero-container {
position: absolute; /* This positions text ON TOP of the image*/
top: 0;
left: 0;
width: 100%;
height: 100%; /* also 100% of the parent (hero same as in hero-img). That is why text and image are perfectly aligned on top of each other*/
display: flex;
align-items: center; /* we just put the text in the center and use absolute potition*/
justify-content: center;
background: rgba(0, 0, 0, 0.4); /* makes the background a bit darker*/
border-radius: var(--borderRadius);
}
.hero-text {
color: var(--white);
text-align: center;
}
@media only screen and (min-width: 768px) {
.hero-text h1 {
font-size: 4rem;
margin-bottom: 0;
}
}


