Currently I'm working on a website with a dropdown menu.
I want, that if you click on it, it appears, but if you hover away, it disappears until you click it again.
I have the following snippet for the HTML-part (The links are not implemented yet and the dropdown menu is only on the main page at the moment):
<div class='dropdown-project' id="project">
<a class='dropbtn-project' href='#project'>Projects</a>
<div class='dropcontent-project'>
Project1
Project2
</div>
</div>
and this for the CSS-part:
.dropbtn-project {
padding: 16px;
border: none;
}
.dropdown-project {
position: relative;
display: inline-block;
}
.dropcontent-project {
display: none;
position: absolute;
background-color: var(--main-header-background);
min-width: 150px;
z-index: 1;
}
.dropcontent-project a {
padding: 5px 10px;
display: block;
}
.dropcontent-project a:hover {
color: var(--hover-fonts-color);
background: var(--main-decor-color)
}
.dropdown-project:target .dropcontent-project {display: block;}
.dropdown-project:hover .dropbtn-project {background-color: var(--main-decor-color);}
.dropdown-project:not(:hover) .dropcontent-project {display: none;}
But with this I have the problem, that the target will stay after I clicked once, so it will reappear on hover after one click.
If you want to check it out, it's on https://www.mikecraft1224.tk.
(The text is in German, so "Projekte" is "projects" and "Projekt" is "project")
add javascript functions to handle this.
Here I added two functions, one for adding class and another one for removing the class. and I give style to the class also
.active .dropcontent-project {display: block;}
when clicking on the a element, an active class is added to the project element.
On mouse leave from the project element, the added class gets removed by calling the removeClass function.
function addClass() {
var element = document.getElementById("project");
element.classList.add("active");
}
function removeClass() {
var element = document.getElementById("project");
element.classList.remove("active");
}
.dropbtn-project {
padding: 16px;
border: none;
}
.dropdown-project {
position: relative;
display: inline-block;
}
.dropcontent-project {
display: none;
position: absolute;
background-color: var(--main-header-background);
min-width: 150px;
z-index: 1;
}
.dropcontent-project a {
padding: 5px 10px;
display: block;
}
.dropcontent-project a:hover {
color: var(--hover-fonts-color);
background: var(--main-decor-color)
}
.active .dropcontent-project {display: block;}
.dropdown-project:hover .dropbtn-project {background-color: var(--main-decor-color);}
<div class='dropdown-project' id="project" onmouseleave="removeClass()">
<a class='dropbtn-project' href='#project' onclick="addClass()" >Projects</a>
<div class='dropcontent-project'>
Project1
Project2
</div>
</div>
I recommend that you make the link that you will use as a base for the dropdown in the HTML array as a container element and add the dropdown content inside it.
But if you still say you need to run it with this HTML structure, the following style codes will solve the problem.
.dropbtn-project {
padding: 16px;
border: none;
}
.dropcontent-project {
transition: 0.2s ease-out;
opacity: 0;
pointer-events: none;
background-color: var(--main-header-background);
min-width: 150px;
z-index: 1;
}
.dropcontent-project a {
padding: 5px 10px;
display: block;
}
.dropcontent-project a:hover {
color: var(--hover-fonts-color);
background: var(--main-decor-color)
}
.dropbtn-project:hover ~ .dropcontent-project,
.dropcontent-project:hover {
opacity: 1;
pointer-events: initial;
}
Related
I have a problem with the nth-child proprety.
The problem is on #subnav element. I don't understand why nth-child doesn't work (suppose to change border color and width).
Here my problem and code :
<div class="container">
<!-- subnav -->
<ul id="subnav">
<li><span>Philosophie</span></li>
<li><span>Musiciens</span></li>
<li><span>Programmes</span></li>
<li><span>Médias</span></li>
<li><span>Agenda</span></li>
</ul>
and the sass
.container {
#subnav {
margin-top: 2rem;
display: flex;
justify-content: space-evenly;
li {
padding-bottom: 0.5rem;
border-bottom: 4px 0.5rem;
border-color: solid #f36e52;
}
a {
color: #fff;
text-decoration: none;
span:nth-child(1):after, span:nth-child(2):after{
content: '';
height: 4px;
text-align: right;
float:right;
margin-top: 0.3rem;
}
span:nth-child(1):after {
width: 30%;
background-color: #f36e52;
}
span:nth-child(2):after {
content: '';
width: 100%!important;
background-color: #fff555;
}
}
}
}
I would like to change the width and also the color of each links, but it only take the attributes of span:nth-child(1).
Any idea ? thanks for your help
They all spans are first-child (or nth-child(1)), you should use nth-child on li to select correctly.
And to change the color of links you should write color attribute directly on span not :after.
The final code should be like below:
#subnav {
li {
&:nth-child(1) {
span {
color: red;
&:after {
content: '';
/*some code*/
}
}
}
&:nth-child(2) {
span {
color: green;
&:after {
content: '';
/*some code*/
}
}
}
}
}
I am trying to create a horizontal subnavigation bar in CSS (without an unordered list), but I can't get the dropdown menu to appear.
Here's my code in HTML:
<div class="navbar sticky">
Home
<div class="subnav">
<button class="subnavbtn">Learn <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
Print
Review
Examples
More Info
</div>
</div>
<div class="subnav">
<button class="subnavbtn">Game <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
Play Now!
How to Play
Cards
</div>
</div>
Minigames
</div>
Here's my code in CSS:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.navbar {
overflow: hidden;
background-color: green;
position: sticky;
top: 0;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.subnav {
float: left;
overflow: hidden;
}
.subnav .subnavbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover,
.subnav:hover .subnavbtn {
background-color: chartreuse;
color: black;
}
.subnav-content {
display: none;
position: absolute;
left: 0;
background-color: red;
width: 100%;
z-index: 1;
visibility: hidden;
}
.subnav-content a {
float: left;
color: white;
text-decoration: none;
display: inline;
}
.subnav-content a:hover {
background-color: #eee;
color: black;
}
.subnav:hover .subnav-content {
visibility: visible;
display: block;
}
I've tried changing the opacity or even using visibility, but it just won't work for me. Sometimes the drop down will appear, however the top nav bar will transform (the "Game" link will shift right, starting at the point where "More Info" ends even though they are on different bars).
Most solutions I've seen while searching this issue is that they are not using (display: block;), but I have been and I don't know what to do at this point.
Here's fiddle
Remove overflow:hidden; from your .navbar declaration and replace it with float:left; and width:100%;
Floated elements are removed from the calculated height of the parent element. However, overflow:hidden; invokes the height to be calculated via block formatting context but, was hiding your dropdowns cause overflow is hidden.
Also, floating the parent element means the children dictate the parent's height making it more dynamic.
Revised Fiddle Here
Just remove the position property from the div with class name navbar.
.navbar {
overflow: hidden;
background-color: green;
top: 0;
}
Dropdown menu appear out of the navbar.
So, you should replace overflow: hidden with height: 50px in .navbar:
.navbar {
height: 50px;
background-color: green;
position: sticky;
top: 0;
}
I have have made a site which has a dropdown menu. The rest of the web page doesn't change when the dropdown menu is called for because I have given the page position:relative;.
And the dropdown has a higher z-index than the page so it is visible at all times. The only problem I have is that I also want to change the background-color of the dropdown-menu when someone hovers over it.
But unfortunantely, when I hover over the dropddown, the browser thinks that I am hovering over the page and for some reason doesn't understand that I am actually hovering over the dropdown-menu. Can anyone please help me with this.
#page {background-color;
height: 2500px;
width: 1600px;
margin: auto;
position: relative;
}
#dropdown {
margin: 0px;
}
.dropdownitem {
opacity: 0.7;
z-index: 1;
background-color: red;
}
.dropdownitem:hover {
background-color: aqua;
color: white
}
I think I have come to understand what the problem is. The thing is that I have a div with
position:absolute
And inside that div there are a number of list items. Because my parent div has absolute positioning, the element has been taken out of document flow and when I hover over the list items, the :hover pseudo class does not work on those nested list items. So now I have found the reason, but what only rests is the solution, and I haven't yet found that.
This may help
ul,
li {
padding: 0;
margin: 0;
}
li {
list-style: none;
padding: 1em;
}
#page {
margin: auto;
position: relative;
}
.mainmenu,
.submenu {
border: thin solid darkgray;
text-align: center;
}
.submenu {
display: none;
}
.mainmenu:hover>.submenu {
position: absolute;
top: 50px;
left: 0;
display: inline-block;
}
.mainmenu,
.dropdownitem {
width: 4em;
}
.dropdownitem {
opacity: 0.7;
background-color: red;
}
.dropdownitem:hover {
background-color: aqua;
color: white
}
<div id="page">
<nav>
<ul id="dropdown">
<li class="mainmenu">Main
<ul class="submenu">
<li class="dropdownitem">One</li>
<li class="dropdownitem">Two</li>
</ul>
</li>
</ul>
</nav>
</div>
Title says it all; my css:
a.menu:hover {
opacity: 1;
text-shadow: 0 0 10px white;
}
a.menu:hover ~ .dropdown {
display: block;
}
.dropdown {
display: none;
width: 50px;
height: 50px;
position: absolute;
top: 120px;
left: 120px;
background: red;
}
HTML:
<p class="left_topbar">
<img src="css/img/logo.png">
Games ▾
</p>
<div class="dropdown"></div>
Why does the .dropdown now get visible when hovering over the menu link?
Actually your h2 is not a child of h1 tag. You have to use sibling operator(+) to achieve this.
h1:hover + h2{
display: block;
}
The above solution will point the next immediate sibling element. If you want to target all the elements then use the ~ operator.
h1:hover ~ h2{
display: block;
}
EDIT:
Based on your edit, Looks like you have to change the order like below.
a.menu:hover {
opacity: 1;
text-shadow: 0 0 10px white;
}
.dropdown {
display: none;
width: 50px;
height: 50px;
position: absolute;
top: 120px;
left: 120px;
background: red;
}
a.menu:hover ~ .dropdown {
display: block;
}
HTML
<div class="left_topbar">
<img src="css/img/logo.png">
Games ▾
<div class="dropdown"></div>
</div>
You want to change
>
to
+
As the arrow is a descendant selector whereas the plus is a sibling selector.
I'm building out a tooltip feature for our site, it's what should be a simple highlight over an icon image and some text appears next to it. the problem I'm having is the words that should be inside of that tooltip bubble breaks into a new line for each. when the code is on its own it works fine.
ul { list-style-type: none; margin: 0; }
li { width: 50px; height: 50px; background: #000; color: #fff; position: relative; }
li:hover { background: #eee; color: #000; }
li:hover #z { display: block; }
#z { position: absolute; left: 50px; height: 50px; background: orange; color: #fff; display: none; }
<ul>
<li>
<div id="z">
some word that shouldn't break
</div>
</li>
</ul>
http://jsfiddle.net/emqLnmo8/1/
Use white-space: nowrap; to stop the words from wrapping.
So with your example: fiddle.