I added the following code for my button drop-down for my page http://test.hkkkki.eu/ and here it is the code snippet:
.dropbtn {
background-color: #ff4e39;
color: white;
padding: 0px 16px 0px 6.4px;
font-size: 16px;
border: none;
font-family: 'Josefin Sans',Helvetica,Arial,Lucida,sans-serif;
transition: all 0.5s ease;
}
.dropdown {
position: relative;
display: block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #fff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color:#fff;
background-color:#ff4e39;
text-decoration: none;
display: block;
}
.dropdown-content a:hover { color:#000; transition: all 0.5s ease;}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #ff4e39;}
<div class="dropdown">
<button class="dropbtn">press</button>
<div class="dropdown-content">
priopćenja
foto
video
iz medija
</div>
</div>
The result is not good, as the drop-down disappears when trying to pick a sub-menu option. And it is also not on top of the following content. It's probably due to Z-index, but I tried more than a couple of places to put it, and it didn't work. Any help is greatly appreciated.
I can see what you mean when I go to your website. I can't see the problem exactly in your code but it seems like the drop down entitled 'press' works fine, are you using the same classes to reference the dropdowns? It would be nice to see the way you created the 'press' dropdown so we can compare and see what went wrong.
Regardless here is the best I can do. w3schools has a very handy tutorial on everything you need to make a hoverable drop down. Here is a snippet to attempt to answer your question:
/* Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
/* 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: #f1f1f1;
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;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd
}
/* 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;
}
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
Related
I'm making a navigation bar for a site that I'm working on, and one of the links will show a dropdown on hover. Currently, my dropdown content displays nowhere near below the dropdown where I would like it to be. This is probably a simple problem, but I would appreciate any help!
HTML:
<nav>
HOME
ABOUT
BLOG
<div class="dropdown">
PROJECTS
<div class="dropdown-content">
Client Work
Personal Projects
</div>
</div>
</nav>
CSS:
nav {
margin-top: 4.3vmin;
text-align: center;
display: none;
}
.nav-item:link {
color: #9422ed;
text-decoration: none;
padding: 3.7vmin;
-webkit-transition: 0.3s;
transition: 0.3s;
}
.nav-item:visited {
color: #9422ed;
}
.nav-item:hover {
color: #5c0d99;
text-decoration: none;
}
.nav-item:active {
color: #5c0d99;
}
.dropdown {
overflow: hidden;
display: inline;
}
.dropdown-content {
display: none;
position: absolute;
box-shadow: 0px 2px 10px 0px rgba(0,0,0,0.2);
z-index: 1;
background-color: #f9f9f9;
min-width: 160px;
}
.dropdown-content a {
float: none;
text-decoration: none;
display: block;
text-align: left;
padding: 10px 14px;
}
.dropdown-content a:hover {
background-color: #e7e7e7;
}
.dropdown:hover .dropdown-content {
display: block;
}
Thanks in advance!
Set your .dropdown to position: relative; and then set .dropdown-content left & top or margin styles for fine positioning.
Trying out nested drop down menu using simple css.I am Trying to make navigation menu. All seems to work fine except that when I hover over the ABOUT US section under the NEW HERE, it shows me correct result. but the problem is it doesn't go away when i hover to the next section below it. how can i possibly fix that ? here's The Link For the snippet for you to consider, kindly tell me where i am doing it wrong.
here's the code for you to see..
.dropbtn2
{
position: relative;
display: none;
}
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 169px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
font-size:20px;
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;
transition:none;
}
.dropdown-content a:hover {
padding-top:15px;
transition:ease-in-out .5s;
}
.dropdown:hover .dropbtn
{
background-color: #8300ff;
color: #182318;
padding-top: 500px;
padding-bottom: 30px;
padding-right: 20px;
padding-left: 20px;
transition: ease-out 0.5s;
text-decoration: none;
box-shadow:none;
}
/* End*/
.dropdown-content2 {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width:170px;
left:170px;
top:0;
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content2 a {
color: black;
font-size:20px;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content2 a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown-content:hover .dropdown-content2 {
display: block;
transition:none;
}
.dropdown-content2 a:hover {
padding-top:15px;
transition:ease-in-out .5s;
}
.dropdown a:hover .dropbtn2
{
padding-bottom: 30px;
padding-right: 20px;
padding-left: 20px;
transition: ease-out 0.5s;
text-decoration: none;
box-shadow:none;
}
/*TEST*/
You need to change the element to hover in order to display your dropdown-content only when hovering your first link.
The value to replace is at line 182 of your codepen. Replace dropdown-content:hover .dropdown-content2 by .dropdown2:hover .dropdown-content2.
Lines 181-185:
/* Show the dropdown menu on hover */
.dropdown2:hover .dropdown-content2 { /* change the hover element here */
display: block;
transition:none;
}
My current fiddle is working fine apart from one thing. When I hover over the button I see the list displayed. Then when I hover over a list item the background changes colour which is all fine. However when the list item changes colour there is almost a box to the left of the item which is not highlighted and can't seem to get rid of it?
.dropbtn {
background-color: #9FACEC;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content li {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content li:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: #4C66E9;}
If i understand you. i believe you are looking to remove ui margin. you can do that using
ul{
padding: 0;
margin: 0;
}
By default most of HTML tags has some default styling, same is here in ul tag and that's the reason that when you hover li tags the left-side is not highlighted.
Default ul styling,
ul{
display: block;
list-style-type: disc;
margin-top: 1em;
margin-bottom: 1 em;
margin-left: 0;
margin-right: 0;
padding-left: 40px;
}
$(document).ready(function() {
var $region = $('#regionList');
$region.append('<li id="Europe">Europe</li>');
$region.append('<li id="Japan">Japan</li>');
$region.append('<li id="North America">North America</a></li>');
$("#regionList li").click(function() {
alert('Clicked list. ' + this.id);
});
})
/* Dropdown Button */
.dropbtn {
background-color: #9FACEC;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
ul {
padding: 0px;
margin: 0px;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content li {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content li:hover {
background-color: #ddd;
}
/* 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: #4C66E9;
}
.selected {
background: #FF00FF;
}
/*Add this*/
ul{
padding-left:0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown">
<button class="dropbtn">Regions</button>
<div class="dropdown-content">
<ul id="regionList"></ul>
</div>
</div>
I believe below approach solves your problem. It was caused by ul element having default padding. I also added box-sizing: border-box to list elements, so padding won't make li elements stick out of ul.
I added explanation below snippet.
Solution key part
.dropdown-content #regionList {
padding: 0;
}
.dropdown-content li {
color: black;
padding: 12px 16px;
box-sizing: border-box;
text-decoration: none;
display: block;
}
Snippet
$(document).ready(function() {
var $region = $('#regionList');
$region.append('<li id="Europe">Europe</li>');
$region.append('<li id="Japan">Japan</li>');
$region.append('<li id="North America">North America</a></li>');
$("#regionList li").click(function() {
alert('Clicked list. ' + this.id);
});
})
/* Dropdown Button */
.dropbtn {
background-color: #9FACEC;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
/* 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: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content #regionList {
padding: 0;
}
.dropdown-content li {
color: black;
padding: 12px 16px;
box-sizing: border-box;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content li:hover {
background-color: #ddd;
}
/* 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: #4C66E9;
}
.selected {
background: #FF00FF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown">
<button class="dropbtn">Regions</button>
<div class="dropdown-content">
<ul id="regionList"></ul>
</div>
</div>
Explanation
Problem is caused by browser's default CSS rules, which are for Chrome:
ul, menu, dir {
display: block;
list-style-type: disc;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
-webkit-padding-start: 40px;
}
You can see more here.
The other answers are correct. I just wanted to explain what's going on.
Your user agent stylesheet contains -webkit-padding-start: 40px; for ul elements.
Represented here in green.
As other answers said, override the padding style for ul elements set by your user agent stylesheet.
Just add padding 0 to your regionList
#regionList{
padding : 0;
}
I want to maintain the hover state while hovering on margin. Things are working fine when I remove position absolute. Technically everything seems to be fine. The element which has margin is in the inside of element which is being hovered. Is it the expected behaviour if not then what is issue exactly.
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
margin-top:20px;
background-color: #f1f1f1;
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: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
This is the expected behavior. Even though .dropdown-content is a child of .dropdown, since .dropdown-content is absolutely positioned, it's as though it's floating outside of .dropdown. If you inspect the .dropdown element in your browser, you'll see that its size only covers the .dropbtn and does not include .dropdown-content.
One way to fix this is to use padding instead of margin on .dropdown-content. However this will expand the size of the content and not leave a gap like it would by using margin.
So if you want to keep the 20px gap between the button and the hover menu, you can add another div around the links:
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<div class="dropdown-inner">
Link 1
Link 2
Link 3
</div>
</div>
</div>
And then put the background / shadow styles on the inner div instead:
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
padding-top: 20px;
min-width: 160px;
z-index: 1;
}
.dropdown-inner {
background-color: #f1f1f1;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
https://codepen.io/anon/pen/RJxGmp
An aproach to achive this is to use a pseudo element (:before) so you can place an invisible block on the gap of this two elements, so you dont lose the hover state when you reach the end of the dropdown container
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
display:block;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown:after{
content:"";
position:absolute;
left:0px;
bottom:-20px;
display: none;
vertical-align: bottom;
width: 100%;
min-width: 160px;
height: 20px;
background-color: transparent;
}
.dropdown-content {
display: none;
position: absolute;
margin-top:20px;
background-color: #f1f1f1;
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: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover:after{ display:inline-block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
This question already has answers here:
How to affect other elements when one element is hovered
(9 answers)
Closed 4 years ago.
I want to make the dropdown menu stay the same text color while hovering over the links and hovering over dropdown (it changes color when you go from hovering over dropdown to hovering over the links) in the dropdown menu.
Here is what I currently have:
/* Add a black background color to the top navigation */
.topnav {
background-color: #333;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color: white;
}
/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
/* Important for vertical align on mobile phones */
margin: 0;
/* Important for vertical align on mobile phones */
}
.dropdown .dropbtn:hover {
color: black;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover {
background-color: #ddd;
color: black;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: 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);
z-index: 1;
}
<div class="topnav">
<a class="active" href="index.html">Home</a>
Products
Contact
About Us
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
Remove color:black from the .topnav a:hover selector:
.topnav a:hover {
background-color: #ddd;
/* color: black; */
}
This leaves all anchor elements as color #f2f2f2 except the active one.
EDIT:
Sorry, I misread your question. Change this:
.dropdown .dropbtn:hover {
color: black;
}
To this:
.dropdown:hover .dropbtn {
color: black;
}
Because both the links and .dropbtn are contained within .dropdown, you're still hovering over it.