Issue
I’m looking to design a dropdown menu similar to the one on the OpenStax website, specifically the sections that include “Subjects”, “Technology”, and “What We Do”. Here’s the link for reference: https://openstax.org.
I’ve made some progress in creating the anchors and other elements, but I’m having trouble with the actual implementation. Could someone provide some guidance on this matter?
html
<nav class="navbar1">
<img src=".\Logo.png" alt="OpenStax logo" width="120px" height="30px" class="logo">
<p class="navpara">Access. The future of education.</p>
<div class="dropdownmenu">
<button onclick="myFunction()" class="dropdownbtn">Subjects</button>
<div id="mydropdown" class="dropdown-content">
<a href="">All</a>
<a href="">Business</a>
<a href="">College Success</a>
<a href="">Humanities</a>
<a href="">Science</a>
<a href="">Social Sciences</a>
</div>
</div>
<div class="dropdownmenu">
<button onclick="myFunction()" class="dropdownbtn">Technology</button>
<div id="mydropdown" class="dropdown-content">
<a href="">OpenStax Assignable</a>
<a href="">OpenStax Kinetic</a>
<a href="">OpenStax Technology Partners</a>
</div>
</div>
<div class="dropdownmenu">
<button onclick="myFunction()" class="dropdownbtn">What We do</button>
<div id="mydropdown" class="dropdown-content">
<a href="">About us</a>
<a href="">Team</a>
<a href="">Research</a>
<a href="">K12 Books & Resources</a>
<a href="">Institutional Partnership</a>
<a href="">Technology Partnership</a>
<a href="">Webinars</a>
</div>
</div>
<Button class="givebtn">Give</Button>
<button class="loginbtn">Login</button>
</nav>
Css
.navbar1 {
width: 100%;
padding: 5px 0 5px 0;
display: flex;
align-items: center;
justify-content: flex-start;
background-color: white;
}
.logo {
margin-right: 10px;
padding-left: 60px;
}
.navpara {
margin-right: 14rem;
}
.dropdownbtn {
padding: 13px 16px;
font-size: 16px;
border: none;
cursor: pointer;
margin: 0 10px;
}
/* The container <div> - needed to position the dropdown content */
.dropdownmenu {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.show {display:block;}
.givebtn {
padding: 13px 30px;
font-size: 16px;
border: none;
cursor: pointer;
margin: 0 10px;
}
.loginbtn {
padding: 13px 30px;
font-size: 16px;
border: none;
cursor: pointer;
margin: 0 10px;
}
Javascript
function myFunction() {
document.getElementById("mydropdown").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropdownbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
Solution
Here you go brother, hope this is what you need. A fully responsive and working navigation bar that looks exactly like the OpenStax one<3
The code isn't the cleanest as I used your code and then started further fixing/making it. But you'll be able to understand it surely.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>OpenStax</title>
<script src="script.js" defer></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<nav class="navbar">
<section class="nav left">
<img
src="https://openstax.org/dist/images/logo.svg"
alt="OpenStax logo"
width="120px"
height="30px"
class="logo"
/>
<p class="navpara">Access. The future of education.</p>
</section>
<section class="nav right">
<div class="dropdown">
<button class="dropdownbtn">Subjects</button>
<div id="mydropdown" class="dropdown-content">
<a href="">All</a>
<a href="">Business</a>
<a href="">College Success</a>
<a href="">Humanities</a>
<a href="">Science</a>
<a href="">Social Sciences</a>
</div>
</div>
<div class="dropdown">
<button class="dropdownbtn">Technology</button>
<div id="mydropdown" class="dropdown-content">
<a href="">OpenStax Assignable</a>
<a href="">OpenStax Kinetic</a>
<a href="">OpenStax Technology Partners</a>
</div>
</div>
<div class="dropdown">
<button class="dropdownbtn">What We do</button>
<div id="mydropdown" class="dropdown-content">
<a href="">About us</a>
<a href="">Team</a>
<a href="">Research</a>
<a href="">K12 Books & Resources</a>
<a href="">Institutional Partnership</a>
<a href="">Technology Partnership</a>
<a href="">Webinars</a>
</div>
</div>
<button class="givebtn">Give</button>
<button class="loginbtn dropdownbtn">Login</button>
</section>
</nav>
</body>
</html>
CSS:
body,
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: Helvetica, sans-serif;
}
body {
background-color: #f1f1f1;
}
.navbar {
background-color: #fff;
display: flex;
align-items: center;
justify-content: center;
margin: 50px 0;
box-shadow: 0 0 25px 5px #00000025;
max-height: 60px;
}
.nav {
display: flex;
align-items: center;
}
.nav.left {
gap: 1em;
}
.navpara {
font-size: 14px;
margin-right: 14rem;
}
.dropdownbtn {
position: relative;
}
.dropdownbtn {
position: relative;
padding: 20px 2em;
font-size: 16px;
border: none;
cursor: pointer;
margin: 0 0px;
font-weight: 600;
background-color: transparent;
color: #5e6162;
}
.dropdown:last-of-type .dropdownbtn {
width: fit-content;
padding: 20px 1em;
width: 160px;
}
.dropdownbtn::after {
content: " \25bc";
position: absolute;
transform: scale(0.8, 0.6);
margin-left: 7px;
}
/* The container <div> - needed to position the dropdown content */
.dropdownmenu {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
visibility: hidden;
opacity: 0;
background-color: #fff;
position: absolute;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
border-top: 3px solid #72a648;
}
.dropdown.active .dropdown-content {
opacity: 1;
visibility: visible;
}
/* Dropdown Links */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.givebtn {
justify-content: center;
text-align: center;
font-size: 20px;
font-weight: 600;
border-radius: 0.5rem;
box-shadow: 0 0.2rem 0.4rem rgba(0, 0, 0, 0.2);
padding: 12px 2rem;
color: #424242;
background-color: #fdbd3e;
box-shadow: none;
border-radius: 0;
border: 0;
cursor: pointer;
}
@media (max-width: 70.0625em) {
.navbar {
background-color: #fff;
display: flex;
align-items: stretch;
justify-content: space-between;
box-shadow: 0 0 25px 5px #00000025;
min-height: 60px;
margin: 0;
padding: 0 10px;
gap: 0;
}
.navpara {
display: none;
}
}
/* I could've had made the hamburger in a better way, but it is just a placeholder */
@media (max-width: 45.0625em) {
.navpara,
.nav.right {
display: none;
}
.nav.left::after {
--clr: #afafaf;
content: "";
height: 20px;
width: 25px;
background: rgb(255, 255, 255);
background: linear-gradient(
0deg,
var(--clr) 0%,
var(--clr) 20%,
transparent 20%,
transparent 40%,
var(--clr) 40%,
var(--clr) 60%,
transparent 60%,
transparent 80%,
var(--clr) 80%,
var(--clr) 100%
);
position: absolute;
right: 20px;
cursor: pointer;
}
.nav.left:hover::after {
--clr: #010101;
}
}
JavaScript:
let dropdown = document.querySelectorAll(".dropdown");
dropdown.forEach((el) => {
el.addEventListener("mouseenter", () => {
el.classList.add("active");
});
el.addEventListener("mouseleave", () => {
el.classList.remove("active");
});
});
Here's are screenshots of the navigation bar:
Answered By - Envy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.