Sticky navbar that chnages options depending on width HTML - html

I am new to HTML and am about to complete my first website, however, I am stuck on the formatting of my navbar at different page widths.
I successfully managed to have a navbar with full options at max width, a drop down menu button below a certain width (1045px) and a sticky navbar when above 1045px with all options.
However, when the navbar is smaller than 1045px, the menu doesn't keep the same formatting and won't open to show drop down options. It also is permanently locked in the top left of the screen no matter the width.
Can someone please help fix my issue or provide guidance. The links provided are the resources I used to create this:
https://www.w3schools.com/howto/howto_css_dropdown.asp
https://www.w3schools.com/howto/howto_js_navbar_sticky.asp
These are also some minor issues I wanted to fix as well:
The options below 'Menu' option are left aligned to the button, Ii
would prefer it to be centred
When clicking the options in the full sized navbar, the back-ground colour changes even though I haven't programmed for this and would like the background colour not to change
Thank you for any help you can provide
EDIT:
I managed to fix the navbar formatting so it now says just menu when below 1045px, but when you hover or click on menu, the list for it doesn't show beneath; I instead noticed the list appeared on the right of all the other options when expanding the width beyond 1045px, with incorrect font-colour.
HTML:
<div id="navbar" class="sitcky">
<a title="Go to 'Before Rutherford' page" href="#">BEFORE RUTHERFORD</a>
<a title="Go to 'Experiment + results' page" href="exp.html">EXPERIMENT + RESULTS</a>
<a title="Go to 'What it changed' page" href="chang.html">WHAT IT CHANGED</a>
<a title="Go to 'Scattering equation' page" href="equa.html">SCATTERING EQUATION</a>
<div class="dropdown">
<button class="dropbtn">MENU</button>
<div class="dropdown-content">
<a title="Go to 'Before Rutherford' page" href="#">BEFORE RUTHERFORD</a>
<a title="Go to 'Experiment + results' page" href="exp.html">EXPERIMENT + RESULTS</a>
<a title="Go to 'What it changed' page" href="chang.html">WHAT IT CHANGED</a>
<a title="Go to 'Scattering equation' page" href="equa.html">SCATTERING EQUATION</a>
</div>
</div>
<!-- Script to keep navbar sticky when scrolled -->
<script>
window.onscroll = function() {myFunction()};
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
</script>
CSS:
#navbar {
background-color: rgb(0, 25, 50);
display: block;
width: auto;
text-align: center;
padding-top: 14px;
padding-bottom: 14px;
z-index: 10;
}
#navbar a {
color: whitesmoke;
text-decoration: none;
font-size: 16px;
padding-left: 30px;
padding-right: 30px;
}
#navbar a:hover{
color: rgb(255, 192, 0);
}
#s_navbar {
background-color: rgb(0, 25, 50);
display: block;
width: auto;
text-align: center;
padding-top: 14px;
padding-bottom: 14px;
z-index: 10;
}
#s_navbar a {
color: whitesmoke;
text-decoration: none;
font-size: 16px;
padding-left: 30px;
padding-right: 30px;
}
#s_navbar a:hover{
color: rgb(255, 192, 0);
}
#media (max-width: 1045px) {
#navbar a{display: none;}
}
#media (min-width: 1045px) {
.dropbtn {display:none;}
}
.dropdown {
position: relative;
display: inline-block;
}
.dropbtn {
background-color: rgb(0, 25, 50);
color: whitesmoke;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
color: rgb(255, 192, 0);
}
.sticky {
position: fixed;
top: 0;
left:0;
right:0;
width:max-content;
}
.sticky + .main {
padding-top: 60px;
}

Related

Navbar CSS conflicts with bootstrap CSS file?

I copied the "mega menu" navbar from W3Schools to a blank page and attached custom CSS, but as soon as I attach the bootstrap CSS file, the header layout is changed completely and the dropdown content doesn't show up anymore. I added two images below the snippet to show what exactly happens. I tried finding the CSS conflict with element inspector without success. I also tried different versions of bootstrap.css but all versions are causing the same navbar to become misplaced.
/* HEADER CSS */
/* Custom */
.navbar {
height:100px!important;
border-bottom:5px solid rgba(55,175,75,1.00)!important;
background-color:rgba(255,255,255,1.00)!important;
font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, "sans-serif"!important;
}
.brand {
margin-top:9px!important;
width:175px!important;
}
.brand-link {
background-color:rgba(255,255,255,0.00)!important;
}
.brand-link:hover {
background-color:rgba(255,255,255,0.00)!important;
}
.navlink {
margin-top:45px!important;
background-color:rgba(255,255,255,1.00)!important;
color:rgba(55,175,75,1.00)!important;
font-size:1.10em!important;
font-weight:300!important;
}
.navlink:hover {
background-color:rgba(255,255,255,1.00)!important;
color:rgba(55,175,75,0.35)!important;
}
.droplink {
}
.droplink:hover {
}
.dropbtn {
margin-top:45px!important;
background-color:rgba(255,255,255,1.00)!important;
color:rgba(55,175,75,1.00)!important;
cursor:pointer!important;
font-size:1.10em!important;
font-weight:300!important;
}
.dropdown-content {
margin-top:-2px!important;
background-color:rgba(55,175,75,1.00)!important;
}
.dropdown-content .header {
background-color:rgba(55,175,75,1.00)!important;
}
.dropdown-content .header-title {
margin-top:0!important;
margin-bottom:8px!important;
color:rgba(255,255,255,1.00)!important;
font-size:1.35em!important;
}
.dropdown-content .droprow {
background-color:rgba(55,175,75,1.00)!important;
}
.dropdown-content .dropcol {
background-color:rgba(55,175,75,1.00)!important;
}
/* Default */
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial, Helvetica, sans-serif;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
width: 100%;
left: 0;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content .header {
background: red;
padding: 16px;
color: white;
}
.dropdown:hover .dropdown-content {
display: block;
}
/* Create four equal columns that floats next to each other */
.column {
float: left;
width: 25%;
padding: 10px;
background-color: #ccc;
height: 250px;
}
.column a {
float: none;
color: black;
padding: 16px;
text-decoration: none;
display: block;
text-align: left;
}
.column a:hover {
background-color: #ddd;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
#media screen and (max-width: 600px) {
.column {
width: 100%;
height: auto;
}
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Naamloos document</title><h3></h3>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="navbar">
<a href="/index.htm" class="brand-link">
<img src="/img/brand.png" class="responsive brand" alt="navbar brand icon">
</a>
Home
News
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<div class="header">
<h2 class="header-title">Mega Menu</h2>
</div>
<div class="row droprow">
<div class="column dropcol">
<h3 class="header-title">Category 1</h3>
Link 1
Link 2
Link 3
</div>
<div class="column dropcol">
<h3 class="header-title">Category 2</h3>
Link 1
Link 2
Link 3
</div>
<div class="column dropcol">
<h3 class="header-title">Category 3</h3>
Link 1
Link 2
Link 3
</div>
<div class="column dropcol">
<h3 class="header-title">Category 4</h3>
Link 1
Link 2
Link 3
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Without any bootstrap CSS file attached (blue area is image), while mouse over dropdown button!
With bootstrap CSS file attached (blue area is image), also while mouse over dropdown button!
https://getbootstrap.com/docs/3.3/
Try Bootstrap 3 and check if that still happens to you.
Found the solution!
I completely removed the .navbar class and all corresponding CSS classes from the bootstrap.css file and replaced it with the W3Schools navbar CSS.
As this is a testing site I can do this, but I would not recommend it as a definitive solution. It may cause problems later (I haven't experienced any so far).

Drop down submenu opening overlapping the main menu with back button

I kindly ask you for help, I would like the submenus to open in slide in the same screen overlapping in the drop vertical menu, because I have many rows and submenus to insert and use the back button to go back in the submenus when they are overlapped.
They do not have to open in cascade like it is now, or sideways, but when I click car the submenu must completely overlap the main menu, and then use the back buttons to go back in the menu go, is it possible?
the back button does not work for me..
I would like the submenus to open to pages, then overlapping each other and using the back button to go back through the menus
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function() {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
});
}
.sidenav {
height: 250px;
width: 300px;
z-index: 1;
top: 200px;
left: 0;
background-color: #444;
overflow-x: hidden;
padding-top: 20px;
}
.sidenav a,
.dropdown-btn {
padding: 14px 8px 6px 16px;
text-decoration: none;
font-size: 17px;
color: #8c8c8c;
display: block;
border: none;
background: none;
width: 100%;
text-align: left;
cursor: pointer;
outline: none;
margin-left: 2px;
}
.sidenav a:hover,
.dropdown-btn:hover {
color: #f1f1f1;
background: #555;
border-left: 3px solid #f1d120;
margin-left: -1px;
}
.main {
margin-top: -460px;
margin-left: 310px;
font-size: 20px;
padding: 10px 50px;
}
.active {
background-color: grey;
color: white;
}
.dropdown-container {
display: none;
background-color: #4c4c4c;
padding-left: 8px;
}
<div class="sidenav">Brand <button class="dropdown-btn">Car <strong>
</strong> </button>
<div class="dropdown-container">° renault ° citoren</div>
<br /><br /><br />
<button>back</button>

How to fix hover dropdown menus on the navbar?

I'm doing some work to set up a new website for a county, and these dropdown menus are giving me quite a bit of trouble. I'm using the code I have to make a custom theme for the county to use with Wordpress, though the issue I'm having pertains to the header specifically.
Issue: I have created three dropdown menus stemming from "Departments", "Services", and "How Do I". However, upon hovering over one of them, all three menus seem to conjoin into one and overlap each other. Also, the menu does not extend down past the navbar into the body, but creates a scroll option inside the navbar, which I do not want.
I've uploaded working chunks of HTML and CSS code to jsfiddle.net, so I'll share a link here: https://jsfiddle.net/cameoux/nbe18zav/
Feel free to take a look here though as well.
<html>
<body>
<div class="topnav" style="color: #FFFFFF">
<img src="<?php echo get_template_directory_uri(); ?>" alt="Image not found!" style="max-width: 100%; display: block;">
About
Contact
<div class="dropdown">
How Do I
<div class="dropdown-content">
Lorem ipsum domat queri?
Lorem ipsum sumer dokar?
Lorem escat que poner populusque?
</div>
Services
<div class="dropdown-content">
Pay Taxes Online
Voter Registration
Traffic Map
</div>
Departments
<div class="dropdown-content">
Chamber of Commerce
Health Department
Clerk's Office
</div>
</div>
</div>
<div class="icon-bar">
<i class="fa fa-facebook"></i>
<i class="fa fa-google"></i>
<i class="fa fa-youtube"></i>
<i class="fa fa-phone"></i>
</div>
<div class="container">
.topnav {
padding-top: 3rem;
padding-left: 7rem;
padding-bottom: 3rem;
top: 0px;
left: 0px;
height: auto;
position: relative;
background-color: rgba(0, 15, 93, 0.85);
font-size: 24pt;
font-family: 'lato', sans-serif;
font-weight: normal;
overflow: auto;
z-index: 1;
}
.topnav a,
.dropbtn {
color: white;
display: inline-block;
padding-left: 20px;
padding-right: 20px;
position: relative;
float: right;
text-align: center;
text-decoration: none;
margin-right: 50px;
z-index: 1;
}
.topnav a:hover,
.dropdown:hover,
.dropbtn {
color: rgba(213, 159, 15, 1.0);
z-index: 1;
}
.dropdown {
position: relative;
display: inline-block;
float: right;
z-index: 1;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f4f4f4;
text-align: center;
font-size: 12pt;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
position: relative;
color: black;
padding: 12px 16px;
text-decoration: none;
display: inline-block;
text-align: center;
z-index: 1;
}
.dropdown-content a:hover {
background-color: #ddd;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
z-index: 1;
Ideally, I'd like the menu items to line up vertically, for the dropdowns to extend past the navbar and into the body of the page, and for the menus to remain separate and not overlap. Any help or advice would be appreciated, thank you!

topnav place form item divs in the correct order

Background
I am trying to make a template for a git repo web viewer so it better matches with the rest of my website. I am starting with the top navigation bar. I have been able to put the items I want: logo, menu item links, select menu for branch, and search box.
Problem
I placed the div container for the select menu first and the div container for the search box second. Yet, the two appear in reverse order. I want them back in the correct order as I placed them in the code.
Code
Note: for best results, use a wide window. I will work on responsiveness once this is working.
/*
* Add a black background color to the top navigation
*/
.topnav {
overflow: hidden;
background-color: #444;
}
/*
* Make a style for fixing the menu to the top.
*/
.fixed {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
/*
* Style the links inside the navigation bar
*/
.topnav a {
float: left;
display: block;
color: #eee;
text-align: center;
padding: 12px 12px;
text-decoration: none;
font-size: 16pt;
}
/*
* Add an active class to highlight the current page
*/
.active {
background-color: #6666;
color: #eee;
}
/*
* Style image in topnav.
*/
.logo {
width: 20px;
height: 20px;
}
/*
* Style image in topnav.
*/
.topnav-image a {
float: left;
display: block;
color: #eee;
text-align: center;
text-decoration: none;
font-size: 16pt;
}
/*
* Right-aligned section inside the top navigation
*/
.topnav-right {
float: right;
}
/*
* Style the right aligned links inside the navigation bar
*/
.topnav-right a {
font-size: 16pt;
}
/*
* Hide the link that should open and close the topnav on
* small screens
*/
.topnav .icon {
display: none;
}
/*
* Change the color of links on hover
*/
/*
* Add a dark background on topnav links and the dropdown button on hover
*/
.topnav a:hover {
background-color: #555;
color: #fff;
}
.topnav .branch-container {
float: right;
}
.topnav .branch-container select {
padding: 5px;
margin: 4px;
border: none;
border-radius: 8px;
font-size: 16pt;
color: #eeeeee;
background-color: #666666;
background-image: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.topnav .search-container {
float: right;
}
.topnav input[type=text] {
padding: 6px;
margin: 4px;
font-size: 16pt;
border: none;
border-radius: 8px;
background: #666666;
color: #eeeeee;
}
.topnav .search-container button {
/*float: right;*/
/*padding: 10px 10px;*/
display: none;
margin-top: 4px;
margin-right: 8px;
background: #ddd;
font-size: 16px;
border: none;
cursor: pointer;
background: #444;
color: #eee;
}
.topnav .search-container button:hover {
background: #666;
color: #eee;
}
::placeholder { /* Chrome, Firefox, Opera, Safari 10.1+ */
color: #eee;
opacity: 1; /* Firefox */
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: #eee;
}
::-ms-input-placeholder { /* Microsoft Edge */
color: #eee;
}
/*
* Format the footer.
*/
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #cccccc;
color: black;
}
/*
* Style the links inside the footer.
*/
.footer a {
float: left;
display: block;
color: black;
text-align: center;
padding: 8px 8px;
text-decoration: none;
font-size: 12pt;
}
/*
* Right-aligned section inside the footer.
*/
.footer-right {
float: right;
}
/*
* Hover effect of footer items.
*/
.footer a:hover {
background-color: #dddddd;
color: black;
}
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
/*
* Format the main content.
*/
.main {
/*padding: 16px;*/
margin-top: 70px;
margin-left: 16px;
margin-right: 16px;
margin-bottom: 50px;
}
h1.title {
font-weight: normal;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Repos: Ali Kakakhel</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
</head>
<body>
<div class="fixed">
<nav class="topnav" id="mytopnav">
<div class="topnav-image">
<img src="https://git-scm.com/images/logos/downloads/Git-Icon-1788C.png" class="logo">
</div>
RepoName
<div class="topnav-right">
About
Summary
Refs
Log
Tree
Commit
Diff
Stats
<div class="branch-container">
<select>
<option value="Master">Master</option>
<option value="Branch1">Branch1</option>
<option value="Branch2">Branch2</option>
<option value="Branch3">Branch3</option>
</select>
</div>
<div class="search-container">
<form action="/action_page.php">
<input type="text" id="SearchInput" placeholder="Search...">
<button type="submit" id="SearchButton" onclick="javascript:alert('Hello World!')"><i class="fa fa-search"></i></button>
</form>
</div>
</div>
</nav>
</div>
<div class="main">
<h1 class="title">Title</h1>
<p>
Far far away, behind the word mountains, far from the countries
Vokalia and Consonantia, there live the blind texts. Separated they
live in Bookmarksgrove right at the coast of the Semantics, a large
language ocean. A small river named Duden flows by their place and
supplies it with the necessary regelialia. It is a paradisematic
country, in which roasted parts of sentences fly into your mouth.
</p>
</div>
<div class="footer" id="myfooter">
<a>Description</a>
<div class="footer-right">
<a>Owner</a>
</div>
</div>
<script>
var input = document.getElementById("SearchInput");
input.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("SearchButton").click();
}
});
</script>
</body>
</html>

How to align a dropdown menu in CSS

I'm having some trouble aligning my dropdown menu in my angular app.
Please see the screenshot attached. The menu extends too far right of the screen. I need it to align to the "top right" of the ellipses instead of the "top left" as it is currently. The menu is currently functioning in a table.
Any assistance in getting the menu to shift to the left would be greatly appreciated.
Here is the HTML/CSS:
<td class="dropup">
<div class="dropup">
<button style="background-color: white;" class="dropbtn">
<img class="icon" src="assets/ellipsis.png">
</button>
<div class="dropup-content">
Admin Functionality
Download Project Snapshot
<a [routerLink]="['/project/'+j._id+'/project-activity']">View Project Activity</a>
Archive Project
<a *ngIf="j.creatorid != user._id" (click)="onLeaveProjectClick(j._id, j.projectname, n)">Leave Project</a>
</div>
</div>
</td>
.dropbtn {
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropup {
position: relative;
display: inline-block;
float:right;
}
.dropup-content {
display: none;
position: absolute;
transform-origin: 60% 40%;
top: 20px;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
border-radius: 10px;
}
.dropup-content :hover {
border-radius: 10px;
}
.dropup-content a {
color: black;
padding: 8px 16px;
text-decoration: none;
display: block;
}
.dropup-content a:hover {
background-color: #ddd;
color: #00aca8 !important;
}
.dropup:hover .dropup-content {
display: block;
border-radius: 10px;
}
As you set .dropup-content to absolute, it will be left aligned relative to .dropup as .dropup-content is within .dropup. But as your class is already absolute, simply add:
right: 0;
left: auto; // Unsure if you really need this though
to .dropup-content. This will take care that the class is right aligned relative to .dropup.
add to class .dropup-content this:
right:0;
https://jsfiddle.net/b1stpfr4/2/