I have header that is moving when I scroll my page down. I have added several buttons to it, and they are moving with it.
My problem is that my drop-button is showing its content when I am not hovering over the button itself.
My code:
/*------------------------------------dropdown menu start*/
.dropbtn {
background-color: #B9B9B9;
color: white;
font-size: 30px;
font-weight:bold;
border: none;
cursor: pointer;
position: relative;
left: 300px;
top: -18px;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
left: 300px;
background-color: #ffffff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
top: 18px;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #ffffff}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
color: #4d4d4d;
}
/*------------------------------------dropdown menu end*/
And a picture(black dot is a mouse location) :
What can I do to fix this?
Your problems run deep. I don't even really want to fix the entire menu because I'd basically be writing one from scratch and you can do that yourself, but what I will do is point out some issues with this to help you find your way:
The core of your design:
<div class="header-cont">
<div class="header">
<img src="">
<logotext>MyCompanyName</logotext>
<button>Home</button>
<div class="dropdown">
<button class="dropbtn">Products</button>
<div class="dropdown-content">
Product 1
Product 2
Product 3
</div>
</div>
<button>Locations</button>
<button>Contacts</button>
<button>History</button>
<div class="dropdown">
<button class="dropbtn">Language</button>
<div class="dropdown-content">
Language 1
Language 2
</div>
</div>
</div>
</div>
There are a number of things here I would never do:
<logotext> is not a valid HTML markup. You probably want a <span class="logotext"> or something along those lines.
Your navigation menu is comprised of <div>s with <button>s and other <div>s with <a> tags in them. This is a bizarre and confusing way to organize a menu. You should consider using <ul> tags and order your sub menu with <li> instead.
The problem you are directly running into is caused by the fact that you have your home <button> element with a left: 300px on it that your <div class="dropdown"> doesn't have.
A much easier and more logical way to organize a nav menu:
<ul id='menu'>
<li><a href='#'>Planets</a>
<ul>
<li><a href='#'>Mercury</a></li>
<li><a href='#'>Venus</a></li>
<li><a href='#'>Earth</a></li>
</ul>
</li>
<li><a href='#'>Stars</a>
<ul>
<li><a href='#'>Sun </a></li>
<li><a href='#'>Betelgeuse</a></li>
<li><a href='#'>Bellatrix</a></li>
</ul>
</li>
<li><a href='#'>Galaxies</a>
<ul>
<li><a href='#'>Milky Way </a></li>
<li><a href='#'>Andromeda</a></li>
<li><a href='#'>Antennae</a></li>
</ul>
</li>
</ul>
I just got this from google and here's the JSFiddle for it.
Here is a CodePen Example
Your HTML should have a mark similar to this as per your CSS
<div class="navbar">
<div class="navItem">Home</div>
<div class="navItem product-dropdown">
<span>Products</span>
<div class="dropdown-content">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>
</div>
</div>
Then as for styling something similar to this:
.dropdown-content{
display:none;
}
.product-dropdown:hover .dropdown-content {
display: block;
}
.navItem {
float: left;
padding: 10px;
}
li {
list-style-type: none;
}
Related
I am trying to fix the behaviour of a nested navbar I have on my page. The main idea is that I have two levels of <ul>-tags, where on both levels the <li>-tags can contain links (i.e. <a>), so both levels have the ability to redirect the page. Upon hovering the top level <li>, I have the lower level display, otherwise it is hidden. This looks like this:
* {
font-family: Roboto;
}
.menu-container {
height: 29px;
background-color: #dcb400;
}
.menu {
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
}
.menu-item {
position: relative;
}
.menu-item:hover {}
.menu-item-div {
display: inline-block;
margin-left: 0;
line-height: 1.5rem;
padding: 3.5px 9.5px;
color: black;
font-weight: 700;
font-size: 13px;
cursor: pointer;
height: 22px;
}
.menu-item:hover .menu-item-div {
background-color: black;
color: white;
}
.submenu {
position: absolute;
top: 29px;
width: auto;
background-color: white;
color: black;
z-index: 100;
padding: 0px;
display: none;
-webkit-box-shadow: 3px 3px 10px rgb(0 0 0 / 50%);
box-shadow: 3px 3px 10px rgb(0 0 0 / 50%);
}
.menu-item:hover .submenu {
display: block;
}
.submenu-item {
display: block;
cursor: pointer;
}
.submenu-item:hover {
background-color: #ccc;
}
<body>
<div class='menu-container'>
<ul class='menu'>
<li class='menu-item'>
<a href='#'>
<div class='menu-item-div'>Menu item 1</div>
</a>
<ul class='submenu'>
<li class='submenu-item'>Submenu item 1</li>
<li class='submenu-item'>Submenu item 2</li>
<li class='submenu-item'>Submenu item 3</li>
</ul>
</li>
<li class='menu-item'>
<a href='#'>
<div class='menu-item-div'>Menu item 2</div>
</a>
<ul class='submenu'>
<li class='submenu-item'>Submenu item 4</li>
</ul>
</li>
<li class='menu-item'>
<a href='#'>
<div class='menu-item-div'>Menu item 3</div>
</a>
</li>
</ul>
</div>
</body>
Now, this works like a charm on desktop, as my submenus show up as soon as I hover a top level menu item. However, on mobile this doesn't work anymore, as the only (reasonable) way to hover a top level menu item is by clicking it, which results in the page redirecting to the top level <a> target. Is there a way to expand this code to also make it work on mobile, i.e. when tapping the top level menu item on mobile, the submenu should show up instead of the page redirecting?
The solutions I have found so far online are either concerning libraries like bootstrap or are way too clumsy. I am wondering if there is a way to do this with only HTML and CSS, or do we also have to involve some js?
Since you are not using your top level menu items for redirecting, you could abandon your references altogether and use divs instead:
<ul>
<li>
<div>Menu item 1</div>
<ul> <!-- display on hover of top level li -->
<li><a>Submenu item 1</a></li>
...
</ul>
</li>
...
</ul>
Or for your given example: https://jsfiddle.net/0fmbojqk/3/
I'm trying to build a drop-down menu with the template of this menu: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_navbar
However, in my browser, the dropdown menu is slow to respond at hover, if it does at all, what is the any reason for this?
Note: The code (functionality and design of drop-down menu) is working but it's very flickery when it catches the hovering, as opposed to the w3Schools example.
A very specific example of the problem is that when I move the mouse slightly away from hovering and back into hovering, the hovering doesn't activate drop-down feature at all.
SCSS (Partials contain only mixin & variable declaration for simple design stuff, not expected to influence performance very much.) :
#import 'color';
#import 'box-shadow';
#import 'border';
#mixin dropdown-template() {
ul {
list-style-type: none;
margin: -10px 150px 0 0;
padding: 0;
overflow: hidden;
}
li {
float: right;
}
li a, .dropbtn {
display: inline-block;
color: $netflix-black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: $netflix-red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
max-width: 200;
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;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
}
& the SCSS later directly compiled to CSS-file linked in CSS:
#ULTRAHEADER {
background: $linear-gradient-facebook-dark;
& h1 {
text-align: left;
margin-left: 40px;
}
#include dropdown-template;
}
& relevant HTML:
<header id="ULTRAHEADER">
<h1>Principles</h1> <!-- The Meta-Title above 4 AoL -->
<ul>
<li class="dropdown"><a class="dropbtn">Spiritual</a>
<div class="dropdown-content">
<a href="markups/IntPrinHTML/BoostOdds.html">
Boost your odds of success through de-centralized principle-guided decision-making.
</a>
<a href="markups/IntPrinHTML/ApplyPrin.html">
Apply Principles in Comple Sub-systems of Reality
</a>
<a href="markups/IntPrinHTML/BoostOdds.html">
Learn, Grow, Evolve. Be Anti-Fragile.
</a>
</div>
</li>
<li class="dropdown"><a class="dropbtn">Relationship</a>
<div class="dropdown-content">
<a href="markups/IntPrinHTML/BoostOdds.html">
Boost your odds of success through de-centralized principle-guided decision-making.
</a>
<a href="markups/IntPrinHTML/ApplyPrin.html">
Apply Principles in Comple Sub-systems of Reality
</a>
<a href="markups/IntPrinHTML/BoostOdds.html">
Learn, Grow, Evolve. Be Anti-Fragile.
</a>
</div>
</li>
<li class="dropdown"><a class="dropbtn">Physical</a>
<div class="dropdown-content">
<a href="markups/IntPrinHTML/BoostOdds.html">
Boost your odds of success through de-centralized principle-guided decision-making.
</a>
<a href="markups/IntPrinHTML/ApplyPrin.html">
Apply Principles in Comple Sub-systems of Reality
</a>
<a href="markups/IntPrinHTML/BoostOdds.html">
Learn, Grow, Evolve. Be Anti-Fragile.
</a>
</div>
</li>
<li class="dropdown"><a class="dropbtn">Intellectual</a>
<div class="dropdown-content">
<a href="markups/IntPrinHTML/BoostOdds.html">
Boost your odds of success through de-centralized principle-guided decision-making.
</a>
<a href="markups/IntPrinHTML/ApplyPrin.html">
Apply Principles in Comple Sub-systems of Reality
</a>
<a href="markups/IntPrinHTML/BoostOdds.html">
Learn, Grow, Evolve. Be Anti-Fragile.
</a>
</div>
</li>
</ul>
</header>
Please ask for whatever other info you need, thanks
I may seem really silly or outright wrong in the way I code. However, when I create a drop down menu in CSS the new li elements get pushed to the other side of the page and not in the container box. How would I fix this?
Here is the code:
<nav>
<div class="wrapper">
<div class="brand">
<img class="UKLogo" src="images/logo.png" alt="">
</div> <!-- brand -->
<div class="navigation">
<ul class="nav-ul">
<li> HOME </li>
<li> ABOUT </li>
<a href="#">
<li class="course-li">
COURSES
<ul class="drop-down">
<li class="list-item"> Driver CPC </li>
<li> First Aid </li>
<li> Other </li>
</ul>
</li>
<li> CONTACT </li>
<!-- <li> TESTOMONIALS </li> -->
<!-- <li> FAQs </li> -->
</ul>
</div> <!-- Navigation -->
</div> <!-- Wrapper -->
</nav>
nav {
margin: 0px;
padding: 0px;
height: 75px;
background-color: #FFF;
}
.brand {
margin: auto;
width: 960px;
}
.company-name {
padding: 0px;
margin: 0px;
}
.UKLogo {
padding: 0px;
margin: 0px;
position: relative;
top: 11px;
}
.navigation ul li {
display: inline-block;
margin: 10px;
position: relative;
left: 380px;
top: -46px;
}
.navigation ul a {
color: black;
margin-left: 40px;
text-decoration: none;
font-family: Lato;
font-weight: 300;
}
.navigation ul a:hover {
color: #169ec5;
font-weight: 300;
}
.course-li:hover .drop-down {
left: 0px;
}
.drop-down {
position: absolute;
left: -5px;
z-index: 1;
background-color: white;
left: -9999px;
}
Thank you ever so much for looking and helping. Always open to criticism whether its the way I code or anything else.
Here is a JSFiddle https://jsfiddle.net/vj41qLts/
Many Thanks!
You need to declare a position in the parent, for the child to reside in. An element with position: absolute; will position itself to the first parent with position: relative;. If there is no parent with position: relative;, it will use the browser window instead.
See fix example here: https://jsfiddle.net/vj41qLts/1/
I think there are two thing you need to change:
ul li will select everything li in the navigation even the dropdown, ul>li will only select the immediate child, instead of running down the nested elements.
you need to add position:relative; in your dropdown's parent.
One of the first issues I see is the fact that your markup for your main links isn't setup correctly. Following a structure more link the below should give make it work the way you want it to:
<nav>
<ul>
<li><a href="#">Home<a></li>
<li><a href="#">About<a></li>
<li>
<a href="#">Courses<a>
<div>
<ul>
<li>A link</li>
<li>A link</li>
</ul>
</div>
</li>
</ul>
</nav>
Then use CSS or JS to control showing and hiding the dropdown of links.
I have a weird problem where my links work fine on one page or fail to do so on another. Here is my code for the non-working page:
<div id="wrapper">
<a href="frontPage.html"><header>
<img src="img/MOBILAX-LOGO.png" height="100" alt="logo">
<h1>MOBI & LAX</h1>
<p>CELLULAR REPAIR CENTER</p>
</header></a>
<nav>
<ul>
<li>
ABOUT US
</li>
<li>
SERVICES
</li>
<li>
IPHONE REPAIR
</li>
<li>
BLOG
</li>
<li>
CONTACTS
</li>
</ul>
</nav>
And the code for the working page:
<div id="wrapper">
<a href="frontPage.html"><header>
<img src="img/MOBILAX-LOGO.png" height="100" alt="logo">
<h1>MOBI & LAX</h1>
<p>CELLULAR REPAIR CENTER</p>
</header></a>
<nav>
<ul>
<li>
<a class="activeLink" href="side2.html">ABOUT US</a>
</li>
<li>
SERVICES
</li>
<li>
IPHONE REPAIR
</li>
<li>
BLOG
</li>
<li>
CONTACTS
</li>
</ul>
</nav>
I am able to see the links fine, but they are not clickable.
Here is the CSS for the nav, ul and wrapper:
nav {
background-color: #2a2a2a;
width: 50%;
height: 200px;
float: left;
}
nav ul {
list-style: none;
height: 200px;
}
nav ul li {
float: left;
margin-top: 86px;
margin-left: 25px;
}
nav a {
text-decoration: none;
color: white;
font-weight: bold;
}
nav a:hover {
color: #f25e44;
}
.activeLink {
color: #f25e44;
}
#wrapper {
width: 1400px;
/*border: 1px solid black;*/
margin-left: auto;
margin-right: auto;
box-shadow: 10px 5px 5px 10px #888888;
}
EDIT: I figured out the issue. I had a div overlapping my ul.
FIDDLE
The # would normally reference an anchor on your page and scroll there. Since you are just using the # it links to itself, so the page wouldn't reload, and would stay in the same place.
Your CSS also specifies not to decorate (underline) the hyperlinks, giving the impression that the link does nothing.
Edit: http://jsfiddle.net/2L3hL7w6/
I've added some CSS to highlight in red if a link has been visited - you'll see if you click on one of your links on the page it changes to red, showing the link does in fact work.
nav a:visited {
color: #ff0000;
}
All the links are the same in your example code. So when you once clicked one link....nothing more will happen since you are already there.
I am trying to make a navigation bar with a four columns submenus. I coded most of things, but when I creating the submenu I found the problem.
This is my HTML:
<div id="navigation">
<ul>
<li class="current">
Home
</li>
<li class="sub-menu">
Our Products
<div class="subnav product">
<div class="content">
<div class="col">
<ul>
<li class="one">
Main Menu Item
</li>
<li class="one">
Main Menu Item
</li>
<li class="one">
Main Menu Item
</li>
</ul>
</div>
<div class="col">
<ul>
<li class="two">
<img src="" />
Promoting Peace in the Niger Delta
</li>
<li class="three">
<img src="" />
Promoting Peace in the Niger Delta
</li>
<li class="four">
<img src="" />
Promoting Peace in the Niger Delta
</li>
<li class="five">
<img src="" />
Promoting Peace in the Niger Delta
</li>
</ul>
</div>
</div>
</div>
</li>
<li class="">
Service Maintenance
</li>
<li class="sub-menu">
Frequently Ask Questions
<li class="sub-menu">
Our Products
<div class="subnav product">
<div class="content">
<div class="col">
<ul>
<li class="one">
Main Sub Item
</li>
<li class="one">
Main Sub Item
</li>
<li class="one">
Main Sub Item
</li>
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
Hope somebody will help me out.
Thank you.
The problem is the container width is defined at 300px
#navigation ul li > div.product {
width: 300px;
}
And its child elements are taking up 100% of that space. So you need to make sure they have room to float left.
#navigation div.col {
float: left;
height:200px;
width: 25%;
}
Hopefully that helps with your question.
Fiddle
Check this http://jsfiddle.net/qtvVK/11/embedded/result/.
I made some changes to your markup and used display:inline-block; instead of floating elements
Relevant CSS syles
/* Dropdown styles */
#navigation ul > li > ul.sub-menu {
display: none;
position:absolute;
padding:10px 0;
background:#fff;
border: 1px solid #DDDCDC;
top: 24px;
z-index: 1;
}
/* Show dropdown when hover */
#navigation ul > li:hover > ul.sub-menu {
display:block;
}
.row {
width:auto;
white-space: nowrap;
}
.col {
display: inline-block;
vertical-align: top;
padding: 0 10px;
}
i suggest using jQuery.
it has simple function called slideDown().
Here is a link to a good tutorial.
You should do like so: First hide your menu when script starts:
$("#idOfDropDownMenu").hide();
And command to drop menu down when mouse enters button and slide up when it leaves it:
$("#idOfButton").hover(function(){ //function that fires when mouse enters
$("#idOfDropDownMenu").slideDown();
}, function() { //function that fires when mouse leaves
$("#idOfDropDownMenu").slideUp();
}
Instead of using IDs you can use any CSS selector.
I hope this helps with your question.
css
ul li ul
{
display: none;
position: fixed;
margin-left: 191px;
margin-top: -37px;
}
ul li:hover ul
{
display: block;
}
ul li a:hover
{
color: #fff;
background: #939393;
border-radius:20px;
}
ul li a
{
display: block;
padding: 10px 10px;
color: #333;
background: #f2f2f2;
text-decoration: none;
}
ul
{
background: #f2f2f2;
list-style:none;
padding-left: 1px;
width: 194px;
text-align: center;
}
html
<ul>
<li>Home</li>
<li>
About
<ul>
<li>About Me
<li>About Site
</ul>
</li>
<li>Contact</li>
</ul>