Changes in #media do not appear - html

I tried to create a responsive menu for a smaller screen, but the icon fa fa-bar would not appear, even though I set display: block inside media query. I wonder what the priority in css is. I followed a tutorial on reponsive menu, yet my code in media does not reflect on the smaller screen.
nav {
display: flex;
padding: 2% 6%;
justify-content: space-between;
align-items: center;
}
.nav img {
width: 150px;
}
.nav-links {
flex: 1;
text-align: right;
}
.nav-links ul li {
list-style: none;
display: inline-block;
padding: 8px 12px;
position: relative;
}
.nav-links ul li a {
color: white;
text-decoration: none;
font-weight: 500;
}
.nav-links ul li::after {
content: "";
width: 100%;
height: 2px;
display: block;
margin: auto;
}
.nav-links ul li:hover::after {
width: 100%;
}
#media (max-width: 700px) {
.nav-links ul li {
display: block;
}
.nav-links {
position: absolute;
background: #012a5d;
height: 100vh;
width: 200px;
top: 0;
right: -200px;
text-align: left;
z-index: 2;
transition: 1s;
}
nav.fa {
display: block;
color: #fff;
margin: 10px;
font-size: 22px;
cursor: pointer;
}
.nav-links ul {
padding: 30px;
}
}
<nav>
<div class="logo">
<img src="images/Logos/University of Toronto Logos/U of T Signature_Reverse RGB (1).png">
</div>
<div class="nav-links" id="navLinks">
<i class="fa fa-times" onclick="hideMenu()"></i>
<ul>
<li>Home</li>
...
<li><a href="contact.html" class="button-header">
<div class="button-font">
Contact Us
</div>
</a></li>
</ul>
</div>
<i class="fa fa-bars" onclick="showMenu()"></i>
</nav>
<script>
var navLinks = document.getElementById("navLinks");
function showMenu()
{navlinks.style.right ="0";}
function hideMenu()
{ navlinks.style.right = "-200";}
</script>
how the menu looks like on a big screen

Related

How to make a working navigation menu that transition from top when clicked on hamburger menu?

I was trying to design a hamburger menu that lets users tap on while using phones and tablets but switch to the full nav bar on the desktop. I have a working navbar for desktop but I have not been able to make one for phones/tablets. I have made a checkbox to try implementing it. But when the checkbox(ham menu) is clicked, it doesn't do anything. Also, tried with negative values. If anyone can help me, I would appreciate it a lot.
Here's my code:
#import url("https://fonts.googleapis.com/css2?family=Montserrat&display=swap");
html,
body {
margin: 0;
padding: 0;
overflow-x: hidden;
font-family: "Montserrat", sans-serif;
}
a {
text-decoration: none;
color: aliceblue;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 8%;
background-color: #2f2e41;
top: 0;
color: aliceblue;
width: auto;
.nav-links,
li {
list-style: none;
display: inline-block;
padding: 0 10px;
}
.nav-links li a {
transition: all 0.3s ease 0s;
}
.nav-links li a:hover {
color: #6e00ff;
}
}
input[type="checkbox"] {
display: none;
}
header .navMode .menu {
display: none;
}
#media (max-width: 420px) {
header nav .nav-links {
background: #2f2e41;
position: absolute;
top: 10%;
height: 30vh;
width: 100%;
right: 0%;
display: flex;
flex-direction: column;
text-align: center;
transition: all 0.3s ease;
li {
margin-top: 5px;
padding: 10px 0;
a:hover {
color: #6e00ff;
background: none;
}
}
}
header .navMode .menu{
display: block;
}
#click:checked ~ .menuBar i:before {
content: "\f00d";
}
#click:checked ~ .menuBar .nav-links {
top: 0%;
}
header nav i {
font-size: 20px;
}
}
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
</head>
<body>
<header>
<a href="#" class="logo">
<p>Name</p>
</a>
<nav class="navMode">
<div class="menu">
<input type="checkbox" id="click">
<label for="click" class="menuBar">
<i class="fas fa-bars"></i>
</label>
</div>
<ul class="nav-links">
<li> <a class="navBar-links" href="#">Lorem
</a></li>
<li> <a class="navBar-links" href="#">Lorem
</a></li>
<li> <a class="navBar-links" href="#">Lorem
</a></li>
<li> <a class="navBar-links" href="#">lorem
</a></li>
</ul>
</nav>
</header>
</body>

How to get div to align and stay put?

When I toggle the menu icon on my nav bar in mobile view, I can't get the 'work' and 'contact' links to align under the name and stay put. I'm targeting the .topnav.responsive #nav-bar ul in media queries which seems to be moving it, but can't figure out what properties to use to make it align under the 'name' when the menu icon is toggled on.
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
.topnav {
overflow: hidden;
position: fixed;
width: 100%;
z-index: 5;
padding-top: 20px;
}
.topnav a {
float: left;
display: block;
color: black;
text-align: center;
padding: 16px;
line-height: 19px;
text-decoration: none;
font-size: 18px;
}
.topnav .icon {
display: none;
}
ul {
margin:0;
padding-top: -80;
}
#nav-bar {
float: right;
}
.nav-link a {
list-style-type: none;
text-decoration: none;
color: black;
}
#nav-bar li {
display: inline-block;
margin-right: 20px;
line-height: 80px;
margin-top: -2px;
}
#work {
margin-right: -5px;
}
#media screen and (max-width: 500px) {
#nav-bar ul {display: none;}
.topnav a.icon {
float: right;
display: block;
margin-right: 10px;
margin-top: -2px;
}
}
#media screen and (max-width: 500px) {
.topnav.responsive {position: fixed;}
.topnav.responsive .icon {
position: absolute;
right: 0px;
top: 20px;
}
.topnav.responsive #nav-bar ul {
float: left;
display: block;
position: relative;
right: 300px;
top: 20px;
text-align: left;
padding-left: 20px;
}
.topnav a {
padding:20px;
}
#nav-bar li {
display:block;
}
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="topnav" id="myTopnav">
NAME
<div id="nav-bar">
<ul>
<li style="list-style-type: none;" class="nav-link"><a id="work" href="#">WORK</a></li>
<li style="list-style-type: none;" class="nav-link">CONTACT</li>
</ul>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
Don't use floats they're just a bit difficult to control, use flex instead.
I've made a few changes removed some unnecessary code to keep it simple and understandable.
I won't explain what every property is doing that'll take all night, i'm pretty sure you're familiar with most, However if you have any questions please do ask.
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.topnav {
overflow: hidden;
position: fixed;
width: 100%;
z-index: 5;
display: flex;
}
.topnav a {
display: block;
color: black;
text-align: center;
padding: 16px;
line-height: 19px;
text-decoration: none;
font-size: 18px;
}
.topnav .icon {
display: none;
}
ul {
display: flex;
margin-left: auto;
}
#media screen and (max-width: 500px) {
.topnav>ul {
display: none;
}
.topnav a.icon {
display: block;
position: absolute;
right: 0;
}
.topnav.responsive {
flex-direction: column;
align-items: flex-start;
}
.topnav.responsive>ul {
display: flex;
flex-direction: column;
margin: 0;
}
.topnav a {
text-align: left;
}
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="topnav" id="myTopnav">
NAME
<ul>
<li style="list-style-type: none;" class="nav-link"><a id="work" href="#">WORK</a></li>
<li style="list-style-type: none;" class="nav-link">CONTACT</li>
</ul>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>

Change all navigations links on hover of one

I'm trying to replica the navigation menu from this website. I've managed to it working to a certain extent, but can't make the service link when hovered to stay the same colour and all of the other to change.
.header {
display: flex;
width: 100%;
}
.nav {
width: 80%;
margin: auto;
position: relative;
}
.nav a {
color: #000;
}
.nav ul {
list-style: none;
padding: 0;
width: fit-content;
}
.nav ul:hover a{
color: #eee !important;
padding-bottom: 20px;
}
.nav ul li:hover a {
color: #333;
padding-bottom: 20px;
}
.nav li:last-child {
margin: 0;
}
.nav ul li {
display: inline-block;
margin: 0 35px 0 0;
}
.three:hover>.sub-menu {
display: flex;
opacity: 1
}
.sub-menu {
height: 200px;
display: flex;
flex-direction: row;
position: absolute;
top: 100%;
background: #333;
display: none;
opacity: 0;
left: 0;
right: 0;
}
<div class="header">
<div class="nav">
<ul>
<li>
<a class="one">Home</a>
</li>
<li>
<a class="two">About</a>
</li>
<li class="three">
<a class="">Services</a>
<div class="sub-menu">
<div class="col-1-4"></div>
</div>
</li>
<li>
<a class="four">Contact</a>
</li>
</ul>
</div>
</div>
As you can see I've changed the link colours on hover of the unordered list which is probably not the best way of trying to get this to work. Please, could someone advise me on the best method to fix this?
You're close -- you have to remove the !important from the rule affecting .nav ul:hover a as this is overriding the rule that will ensure the hovered item is a different color than the rest:
.header {
display: flex;
width: 100%;
}
.nav {
width: 80%;
margin: auto;
position: relative;
}
.nav a {
color: #000;
}
.nav ul {
list-style: none;
padding: 0;
width: fit-content;
}
.nav ul:hover a{
color: #eee;
padding-bottom: 20px;
}
.nav ul li:hover a {
color: #333;
padding-bottom: 20px;
}
.nav li:last-child {
margin: 0;
}
.nav ul li {
display: inline-block;
margin: 0 35px 0 0;
}
.three:hover>.sub-menu {
display: flex;
opacity: 1
}
.sub-menu {
height: 200px;
display: flex;
flex-direction: row;
position: absolute;
top: 100%;
background: #333;
display: none;
opacity: 0;
left: 0;
right: 0;
}
<div class="header">
<div class="nav">
<ul>
<li>
<a class="one">Home</a>
</li>
<li>
<a class="two">About</a>
</li>
<li class="three">
<a class="">Services</a>
<div class="sub-menu">
<div class="col-1-4"></div>
</div>
</li>
<li>
<a class="four">Contact</a>
</li>
</ul>
</div>
</div>
Here's a good resource on how !important affects other rules on the page.

Sub-menu doesn't appear on mouse hover

I am new to web design. I am trying to create a site where in some menus in menu bar have sub menus. I want on mouse hove it should display submenu which is not happening. This is my code:
#charset "UTF-8";
body {
margin: 0;
}
. wrapper {
height: 100vh;
}
nav {
height: 44px;
background: #323232;
text-align: center;
/* to center the UL in the nav */
}
nav ul {
display: flex;
max-width: 1200px;
justify-content: space-around;
align-items: center;
padding: 0;
margin: 0 auto;
/* 0 auto allows it to self-center in the nav */
list-style-type: none;
}
nav li {}
nav a {
display: inline-block;
height: 44px;
line-height: 44px;
color: white;
font-size: 15px;
font-weight: 100;
text-decoration: none;
transition: 0.3s;
}
nav a:hover {
color: #B8B8B8;
}
.dropdown ul {
position: absolute;
top: 43px;
z-index: 100;
visibility: hidden;
}
.dropdown ul li a {
background: none;
text-align: left;
display: block;
}
li li {
width: 100%;
}
.dropdown li:hover>ul {
display: block;
}
<div class="wrapper">
<nav>
<ul>
<li>Home</li>
<li class="dropdown"><a>Drinks</a>
<ul>
<li>Pan Shots</li>
<li>Tea</li>
</ul>
</li>
<li>Snacks</li>
<li>Desert</li>
<li>Special Diet</li>
<li>Contact Us</li>
</ul>
</nav>
</div>
<div class="fft">Food For Thought</div>
<br>
<br>
<img src="Indian_Spices.jpg" alt="Spices" class="main_wrapper">
<!--<div class="main_wrapper" ></div>-->
On mouse hover on 'Drinks' nothing comes up. I want when I move mouse on 'Drikns' sub menus 'Pan Shots' and 'Tea' should be visible and should hide when mouse is not on 'Drinks'.
Your example is kinda messy and there's a lot of unnecessary code, i'm gonna present you with an example that can you work from.
* {
margin: 0;
padding: 0;
}
ul {
display: flex;
list-style: none;
}
ul>li {
flex: 1;
background: dodgerblue;
height: 45px;
text-align: center;
}
ul>li>a {
text-align: center;
line-height: 45px;
text-decoration: none;
color: #fff;
}
ul>li>ul {
display: none;
}
ul>li:hover>ul {
display: block;
}
.dropdown>a:after{
content:'▿';
font-weight:bold;
}
<ul>
<li>Home</li>
<li class="dropdown">Drinks
<ul>
<li>Pan Shots</li>
<li>Tea</li>
</ul>
</li>
<li>Snacks</li>
<li>Desert</li>
<li>Contact Us</li>
</ul>
You are mixing display and visibility. Your selector is wrong as well.
.dropdown li:hover>ul
Means that CSS is looking for an li child element of .dropdown to be hovered before something is done with the > ul
Since CSS properties are inherited your text is still white in a child element. Therefor you don't see the text.
Try the following:
#charset "UTF-8";
body {
margin: 0;
}
. wrapper {
height: 100vh;
}
nav {
height: 44px;
background: #323232;
text-align: center;
/* to center the UL in the nav */
}
nav ul {
display: flex;
max-width: 1200px;
justify-content: space-around;
align-items: center;
padding: 0;
margin: 0 auto;
/* 0 auto allows it to self-center in the nav */
list-style-type: none;
}
nav li {}
nav a {
display: inline-block;
height: 44px;
line-height: 44px;
color: white;
font-size: 15px;
font-weight: 100;
text-decoration: none;
transition: 0.3s;
}
nav a:hover {
color: #B8B8B8;
}
.dropdown ul {
position: absolute;
top: 43px;
z-index: 100;
visibility: hidden;
}
.dropdown ul li a {
background: none;
text-align: left;
display: block;
}
li li {
width: 100%;
}
.dropdown:hover ul {
visibility: visible;
}
.dropdown ul a {
color: black;
}
<div class="wrapper">
<nav>
<ul>
<li>Home</li>
<li class="dropdown"><a>Drinks</a>
<ul>
<li>Pan Shots</li>
<li>Tea</li>
</ul>
</li>
<li>Snacks</li>
<li>Desert</li>
<li>Special Diet</li>
<li>Contact Us</li>
</ul>
</nav>
</div>
<div class="fft">Food For Thought</div>
<br>
<br>
<img src="Indian_Spices.jpg" alt="Spices" class="main_wrapper">
<!--<div class="main_wrapper" ></div>-->

CSS dropdown menu takes entire page width?

I have a simple nav menu at the top of a page I'm working on. I want one of the links on the nav menu to have a dropdown menu pop up underneath it when you hover over it.
I have the drop down menu appearing just fine. The only problem is that the drop down menu items are the width of the entire page instead of the width of a nav menu item. I'm not sure what's causing it...
Here is the html and css for the nav menu and drop down menu:
.dropdown {
float: left;
background-color: #FFF0F5;
}
.dropDiv {
position: relative;
display: inline-block;
width: 100%;
}
.dropdownContent {
display: none;
background-color: #FFF0F5;
z-index: 1;
width: 300px;
overflow-x: hidden;
}
.dropdownContent a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 300px;
}
.dropdownContent a:hover {
background-color: #fff8dc;
}
.dropDiv:hover .dropdownContent {
display: block;
z-index: 1;
height: 200px;
width: 300px;
overflow-x: hidden;
}
.dropDiv:hover .dropdown {
background-color: #fff8dc;
}
<nav>
<ul>
<li>Eiffel Tower</li>
<li>Fashion</li>
<li>Food</li>
<li>Museums</li>
<div class="dropDiv">
<li class="dropdown">History</li>
<div class="dropdownContent">
<a href=leaders.shtml>Leaders of Paris</a>
<a href=future.shtml>Future of Paris</a>
</div>
</div>
<li>Language</li>
<li>Works Cited</li>
</ul>
</nav>
As you can see It's a bit of a mess since I've been adding lots of things trying to think of something to fix this width issue. Any help is greatly appreciated!
Your code worked fine for me. Looks like some other CSS in your code is making it full width. Have you tried fixing it with "!important" or "max-width"?
You should create dropDiv in the li tag and make it absolute position to get the parent widht
nav ul {
padding:0;
margin:0;
}
nav ul li {
list-style: none;
float: left;
position: relative;
margin: 0 5px;
}
nav ul li a{
color: #000;
text-decoration: none;
}
.dropdown {
float: left;
background-color: #FFF0F5;
}
nav ul li:hover .dropDiv {
display: block;
z-index: 1;
height: 200px;
width: 300px;
overflow-x: hidden;
}
.dropDiv:hover .dropdown {
background-color: #fff8dc;
}
.dropDiv {
position: absolute;
width: 100%;
display: none;
background-color: #FFF0F5;
z-index: 1;
width: 300px;
overflow-x: hidden;
}
.dropDiv a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropDiv a:hover {
background-color: #fff8dc;
}
<nav>
<ul>
<li>Eiffel Tower</li>
<li>Fashion</li>
<li>Food</li>
<li>Museums</li>
<li class="dropdown">History
<div class="dropDiv">
<a href=leaders.shtml>Leaders of Paris</a>
<a href=future.shtml>Future of Paris</a>
</div>
</li>
<li>Language</li>
<li>Works Cited</li>
</ul>
</nav>
Hello just define width for nav bar as you want I've defined 700px. and add some css . check this code
.dropdown {
float: left;
background-color: #FFF0F5;
}
.dropDiv {
position: relative;
display: inline-block;
list-style: none;
padding: 9px;
float: left;
max-width: 100%;
}
.dropdownContent {
display: none;
background-color: #FFF0F5;
z-index: 1;
width: 300px;
overflow-x: hidden;
}
.dropdownContent a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdownContent a:hover {background-color: #fff8dc;}
.dropDiv:hover .dropdownContent {
display: block;
z-index: 1;
overflow-x: hidden;
top: 30px;
left: 10px;
position: absolute;
}
.dropDiv:hover .dropdown {
background-color: #fff8dc;
}
nav {width:700px;float: left;}
nav > ul > li { float: left;
padding: 9px;list-style: none}
ul{float:left;}
<nav>
<ul>
<li>Eiffel Tower</li>
<li>Fashion</li>
<li>Food</li>
<li>Museums</li>
<div class="dropDiv">
<li class="dropdown">History</li>
<div class="dropdownContent">
<a href=leaders.shtml>Leaders of Paris</a>
<a href=future.shtml>Future of Paris</a>
</div>
</div>
</li>
<li>Language</li>
<li>Works Cited</li>
</ul>
</nav>
I think this is what you want.