:hover usage in html and css - html

Can anyone tell me where I can use :hover?
In the last CSS line it's .dropdown:hover .dropdown-content which gets me a dropdown menu when I hover on dropdown element.
However, if I use .dropbtn:hover .dropdown-content instead, no dropdown menu is displayed. Why is that?
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: red;
}
.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;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>

As per your code, The dropdown menu is a part of a list element.
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
Your class "dropbtn" is a class which is applied for an anchor link you've placed. So that's why when you use the code
.dropbtn:hover .dropdown-content {
display: block;
}
It does not make sense, because the div with class "dropdown-content" is not a child of the element which contains the class "dropbtn" - which is a simple anchor element.
however, The code:
.dropdown:hover .dropdown-content {
display: block;
}
works becausue the class "dropdown" is the class of the parent element, which is the list element and then under it, we have the div with class "dropdown-content".
That's how CSS works and hope it makes sense.

Related

second column in drop down menu list in navigation bar

Hi I have made a web page with a navigation bar. There is mouse hover drop down menu on a navigation bar. I have take the code for this navigation bar from w3school web site. The code is
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.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;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div style="display:flex">
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
<div class="dropdown-content">
List 4
List 5
List 6
</div>
</div>
</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
Now the problem is that I want to add second column in front of first column in the drop down list.For this I add second div but all the stuff of the second div appears in place of first div and hides the content of first div. Please tell me that how can I place the second div in next to first div so that they appear parallel to each other.Please answer this question. I will be very thank full to you.
You can place it by wrapping the .dropdown-content div with a flexbox.
I have added .wrapper a class you can change it to whatever you like.
Here is the code:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.wrapper {
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;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .wrapper{
display: flex;
justify-content: center;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="wrapper">
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
<div class="dropdown-content">
demo
demo
demo
</div>
</div>
</li>
</ul>
P.S. In the same way you can create multiple, side by side divs using flexbox. You can learn about flex here

Drop down menu doesn't work how I want it to

I am trying to make a menu which contains one menu-item with a dropdown, however I can't seem to get it quite right.
I started off with combining my existing menu bar to a piece of code for a dropdown, I have managed to get de drop down item in the menu bar, but I don't know how to get the menu bar back to the top again and it still has a blank on the left.
There is also an extra empty item in my drop down menu and I don't know how to get rid off it?
Can someone please help me I feel like I am overlooking something
.menu-bar {
position: fixed;
width: 100vw;
background-color: #134c95;
color: white;
display: flex;
flex-direction: row;
justify-content: center;
opacity: 0.8;
}
.menu-item {
padding: 10px;
cursor: default;
font-size: 15pt;
margin-left: 15px;
margin-right: 15px;
}
.menu-item:hover,
a:hover {
background-color: white;
cursor: pointer;
color: 234c95;
}
.menu-bar a {
color: white;
}
.menu-bar a:visited {
color: white;
}
.menu-bar a:link,
.menu-bar a:visited {
text-decoration: none;
}
/*
.menu-bar a:hover {
color: #134c95;
}
*/
/* Dropdown Button */
.dropbtn {
background-color: inherit;
color: white;
/* padding: 16px;*/
/* font-size: 16px;*/
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
/*
.dropdown {
position: fixed;
display: inline-block;
}
*/
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: white;
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: #234c95;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: 234c95;
color: white;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: white;
color: #234c95;
}
<div class="menu-bar">
<a href="instructiestest.html">
<div class="menu-item">Welkom</div>
</a>
<a href="instructie2.html">
<div class="menu-item">Aanmelden Portbase</div>
</a>
<a href="instructie3.html">
<div class="menu-item">De App</div>
</a>
<a href="testing.html">
<div class="dropdown">
<button class="dropbtn">
<div class="menu-item">Het team</div>
<div class="dropdown-content">
Google
Facebook
YouTube
</div>
</button>
</div>
</a>
</div>
Remove the a tag from your dropdown item, like below:
<div class="menu-bar">
<a href="instructiestest.html">
<div class="menu-item">Welkom</div>
</a>
<a href="instructie2.html">
<div class="menu-item">Aanmelden Portbase</div>
</a>
<a href="instructie3.html">
<div class="menu-item">De App</div>
</a>
<div class="dropdown">
<button class="dropbtn">
<div class="menu-item">Het team</div>
<div class="dropdown-content">
Google
Facebook
YouTube
</div>
</button>
</div>
</div>
Also, your .dropdown-content a:hover rule is missing a # in your background-color attribute:
.dropdown-content a:hover {
background-color: #234c95;
color: white;
}
Here is a working Fiddle
Something seems to have an extra margin or similar. Try to load the code, and check with the inspector. It'll show you the different spacing types in nice colours: https://developer.mozilla.org/en-US/docs/Tools/Page_Inspector/How_to/Examine_and_edit_the_box_model
In all honesty, you might be better re structuring this menu completely. I don't mean to be harsh but the code isn't structured well enough and is a little confusing to work with.
Ive just tweaked your code a little to make it easier to work with. I'm not exactly sure what it is your trying to achieve but here is something for you to take a look at:
CSS
#menu-bar {
position: fixed;
width: 100vw;
background-color: #134c95;
padding: 15px;
}
#menu-bar ul {
list-style: none;
padding: 0;
margin: 0;
font-size: 22px;
font-family:sans-serif;
}
#menu-bar ul li {
margin-bottom: 10px;
width: 100%;
}
#menu-bar ul li a {
display: block;
padding: 10px;
color: #fff;
text-decoration: none;
}
#menu-bar ul li a:hover {
color: #134c95;
background-color: #fff;
}
#menu-bar ul li ul {
display: none;
}
#menu-bar ul li:hover ul {
display: block;
}
HTML
<nav id="menu-bar">
<ul>
<li>Welkom</li>
<li>Aanmelden Portbase</li>
<li>De App</li>
<li class="">Het team
<ul>
Google
Facebook
YouTube
</ul>
</li>
</ul>
</nav>
As you can see this is much easier to understand and follow. The ID of the nav element is all you need to style the child elements.
Hope that helps as a starting point!

Dropdown Menu Somehow Not Working

I have went through W3Schools to attempt understanding the coding structure of dropdown menus. When opening the page and hovering your cursor to the 'Merch' text it is supposed to display the dropdown menu. For some reason, however, it is not showing. Please amplify for me and show me where I went wrong.
<!DOCTYPE html>
<html>
<head>
<style>
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
li a:hover {
background-color: gray;
}
#dropdown {
position: absolute;
display: none;
background-color: darkgray;
min-width: 140px;
}
#dropdown a {
color: white;
text-align: left;
padding: 10px;
display: block;
text-align: left;
}
#dropdown a:hover {
background-color: gray;
}
#dropbtn:hover #dropdown {
display: block;
}
</style>
</head>
<body>
<ul>
<li>Home</li>
<li>Merch
<div id="dropdown">
Shirts
Pants
</div>
</li>
<li>About Us</li>
</ul>
</body>
</html>
You need this change in CSS
#dropbtn:hover + #dropdown {
display: block;
}
Dropdown is not a child, it is next element in your current html setup, so, this selector will find it.
Or, better, place id on li element (parent):
<ul>
<li>Home</li>
<li id="dropbtn"><a href="#" >Merch</a>
<div id="dropdown">
Shirts
Pants
</div>
</li>
<li>About Us</li>
</ul>
#dropbtn:hover #dropdown {
display: block;
}
Demo: https://jsfiddle.net/3bfgzf37/
If you use first solution, dropdown dissapears fast, and behave strange...
Explanation: hover on a is not hover on dropdown (a is sibling), hover on li element, is, in the same time, hover on dropdown (parent-child, dropdown is 'inside' li - that produces consistent, desired, behavior).
Second one is better.
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
li a:hover {
background-color: gray;
}
#dropdown{
position: absolute;
display:none;
background-color: darkgray;
min-width: 140px;
}
#dropdown a {
color: white;
text-align: left;
padding: 10px;
display: block;
text-align: left;
}
#dropdown a:hover {
background-color: gray;
}
#dropbtn:hover #dropdown {
display: block;
}
<ul>
<li>Home</li>
<li id="dropbtn"><a href="#" >Merch</a>
<div id="dropdown">
Shirts
Pants
</div>
</li>
<li>About Us</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
Your style tag should be outside the head tag. Plus, the dropdown in this code works now. Just do some slight changes to the colors to your desire. :)
<html>
<head></head>
<style>
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
/* Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: gray;
}
</style>
<body>
<ul>
<li>Home</li>
<li>
<div class="dropdown">
<button class="dropbtn">Merch</button>
<div class="dropdown-content">
Shirts
Pants
</div>
</div>
</li>
<li>About Us</li>
</ul>
</body>
</html>

Dropdown menu in navbar not staying open when hovering over it

i have a question for you guys , i have made a navbar for my website , but i can't cope with the dropdown menu . The problem is that when I want all the links to be in the middle of the navbar , but when i hover over the navigation dropdonw menu , it won't stay open on hover. I know i can fix this by adding li float:left instead of li display:inline, but then all the links go to the left , so my question is: is there a way in which all my links could be centered in the middle of my navbar and my navigation dropdonw menu could stay open on hover ? This is my code:
nav {
font-size:75%;
text-align: center;
}
ul {
width:100%;
list-style-type: none;
margin-left: -8px;
padding: 0px;
overflow: hidden;
background-color: black;
}
li {
display: inline;
}
li a {
display: inline-block;
color: white;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
color:darkgrey;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 134px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.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;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<nav>
<ul>
<li>Начало</li>
<li class="dropdown" >
За фирмата
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
<li>За собствениците</li>
<li>Продукти</li>
<li>Поръчки</li>
<li>Мнения</li>
</ul>
</nav>
</body>
</html>
I actually have no idea how does it work in this online editor , but it does not work for me when i do it ! Please halppp !

Drop-down Function Depending on Targetting

I have a dropdown box presented on W3CSchools that I wish to convert into less class-oriented CSS (rather than having a bunch of classes, I'll just select it through the primary parent).
Here's the HTML:
<nav>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</nav>
(Effectively, I shouldn't have to have any classes here)
Here's the CSS:
/* Style The Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
Everything here works fine when converted over to being targetted merely by the nav element, apart from the functional stuff:
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
I can try to convert these into,
nav > div:hover nav > div > div {
display: block;
}
nav > div:hover mav > div > button {
background-color: #3e8e41;
}
But nothing happens. I've even tried using the primary parent class of .dropdown and removing the beginning of nav > div, although it also returns nothing.
What is making the difference? I'm targeting exactly the same elements.
You have a typo in nav > div:hover mav > div > button { mav should be nav so maybe that caused you problems with testing.
Here is the relevant code without using classes:
/*selects the nav and then select the div that is the immediate child of the nav that is hovered. When that child div is hovered it selects the first div and sets its display type to block*/
nav > div:hover > div:first-of-type{
display: block;
}
nav > div:hover > button{
background-color: #3e8e41;
}
/* Style The Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1;}
nav > div:hover > div:first-of-type{
display: block;
}
nav > div:hover > button{
background-color: #3e8e41;
}
<nav>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</nav>