CSS-only dropdown mega menu with hover delay - html

I have a CSS only dropdown mega menu. Now I need to add delay on hover, but only when not already hovering a tab. The goal is to remove the delay before showing subsequent sheets - without using JavaScript. It may or or may not be possible. I have tried using sibling selector to set zero transition delay on the neighbour of the hovered element - this naturally doesn't work because the transition delay rule applies to the 'to' hover state, not the 'from' state.
body * {
outline: 1px solid black;
background-color: #0001;
}
ul {
margin: 0;
padding: 10px;
display: inline-flex;
}
li {
display: inline-block;
padding: 10px;
}
ul li div {
position: absolute;
background-color: white;
width: 200px;
height: 200px;
visibility: hidden;
transition: visibility 0s;
transition-delay: 300ms;
}
ul li:hover button {
background-color: blue;
color: white;
}
ul li:hover div {
visibility: visible;
}
ul li:not(:hover) div {
transition-delay: 0s;
}
a {
padding: 10px;
display: block;
}
a:hover {
text-decoration: none;
}
<ul>
<li>
<button>Link</button>
<div>Sheet</div>
</li>
<li>
<button>Link</button>
<div>Sheet</div>
</li>
</ul>
<a href='#'>Background link</a>

Is this what you want. adding transition delay on the hover state not the actual element css
ul li:hover div{visibility: visible;transition-delay: 1000ms;}
body * {
outline: 1px solid black;
background-color: #0001;
}
ul {
margin: 0;
padding: 10px;
display: inline-flex;
}
li {
display: inline-block;
padding: 10px;
}
ul li div {
position: absolute;
background-color: white;
width: 200px;
height: 200px;
visibility: hidden;
transition: visibility 0s;
}
ul li:hover button {
background-color: blue;
color: white;
}
ul li:hover div {
visibility: visible;
transition-delay: 1000ms;
}
ul li:not(:hover) div {
transition-delay: 0s;
}
a {
padding: 10px;
display: block;
}
a:hover {
text-decoration: none;
}
<ul>
<li>
<button>Link</button>
<div>Sheet</div>
</li>
<li>
<button>Link</button>
<div>Sheet</div>
</li>
</ul>
<a href='#'>Background link</a>

Related

Understanding need for a selector in dropdown menu

I was trying to learn how to create drop down menu from CSS tricks. This is code they have:
<nav role="navigation">
<ul>
<li>One</li>
<li>Two
<ul class="dropdown">
<li>Sub-1</li>
<li>Sub-2</li>
<li>Sub-3</li>
</ul>
</li>
<li>Three</li>
</ul>
</nav>
css
a {
text-decoration: none;
}
nav {
font-family: monospace;
}
ul {
background: darkorange;
list-style: none;
margin: 0;
padding-left: 0;
}
li {
color: #fff;
background: darkorange;
display: block;
float: left;
padding: 1rem;
position: relative;
text-decoration: none;
transition-duration: 0.5s;
}
li a {
color: #fff;
}
li:hover {
background: red;
cursor: pointer;
}
ul li ul {
background: orange;
visibility: hidden;
opacity: 0;
min-width: 5rem;
position: absolute;
transition: all 0.5s ease;
margin-top: 1rem;
left: 0;
display: none;
}
ul li:hover > ul,
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}
ul li ul li {
clear: both;
width: 100%;
}
What I don't understand from above is why they need the following selector:
ul li ul:hover
Because when I remove it the menu still works. Can someone explain why? Which is the correct selector?
Here is demo:
https://stackblitz.com/edit/js-aftbkv?file=style.css
Article link:
https://css-tricks.com/solved-with-css-dropdown-menus/
the hover selector means: when the mouse is on that element, something is going to happen. it adds some functionality.
has u see the elements of the dropdown menu they have display:none and on hover they set their display to display:block appering them.
If u delete all the hover it will not work! maybe is because u deleted them and not save the file or maybe the cache, but without :hover will not be functionality.

Why my hrefs doesn't work properly when i expand my list by using :focus parameter?

I have a problem with focus parameter in css. My basic problem is, when I click on my href, it expands my list with hrefs which don't work.
When I comment css below, these hrefs all are fine, but they are also enabled when they are invisible.
* {
box-sizing: border-box;
padding: 0;
margin: 0;
font-size: 20px;
}
li.position {
width: 100px;
background-color: powderblue;
border-bottom: 1px solid black;
}
ul a:hover {
background-color: green;
}
.sub-menu {
position: absolute;
opacity: 0;
}
.sub-menu ul li a {
cursor: default;
pointer-events: none;
}
ul li a:focus + .sub-menu {
opacity: 1;
pointer-events: none;
}
ul li a:focus + .sub-menu ul li a {
pointer-events: auto;
cursor: pointer;
}
<ul>
<li>
click here
<div class="sub-menu">
<ul>
<li class="position">amazon</li>
<li class="position">amazon</li>
<li class="position">amazon</li>
</ul>
</div>
</li>
</ul>
You explicitly said the browser should ignore clicks on the links with pointer-events:none;
You are using opacity to hide your links. This is not the best way because the links are still there, just invisible.
What you really want to do really hide the links when they shouldn't be visible. I recommend using display: none and display: block. You can also do things like this, which keeps the links visible to screen readers but not humans:
.hidden {
display: block;
width: 0;
height: 0;
padding: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
By default, selector pointer-events: none should have rule .sub-menu set.
Even with focus, tags a continue to be inactive. Add rule pointer-events: auto for the .sub-menu:hover ul li a selector.
.sub-menu:hover ul li a {
pointer-events: auto;
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
font-size: 20px;
}
li.position {
width: 100px;
background-color: powderblue;
border-bottom: 1px solid black;
}
ul a:hover {
background-color: green;
}
.sub-menu {
position: absolute;
opacity: 0;
pointer-events: none;
}
.sub-menu ul li a {
cursor: pointer;
}
ul li a:focus + .sub-menu {
opacity: 1;
pointer-events: auto;
}
.sub-menu:hover ul li a {
pointer-events: auto;
}
<ul>
<li>
click here
<div class="sub-menu">
<ul>
<li class="position">amazon</li>
<li class="position">amazon</li>
<li class="position">amazon</li>
</ul>
</div>
</li>
</ul>

Why does the nested ul element bring down the entire li elements when hovering over one li element

This is a two part question. The first part is my problem when hovering over an li element that is supposed to reveal a nested ul element. It brings the rest of the li tags at the bottom of the nested ul element. I don't know what I'm doing wrong but it pushes the initial li elements downward. I wish someone can explain what I'm doing wrong. Here is the code
The second question I have is how do I create li elements that specificly have a hovering effect on them and not any nested li elements? I wanted to create a menu list that changes the color of the text when you hover over it, but I didn't want the nested li elements to also have the hover effect
HTML
<div id="container">
<ul class="list-inline">
<li>Fire
<ul>
<li>charmander</li>
<li>magmar</li>
<li>vulpix</li>
</ul>
</li>
<li>Grass
<ul>
<li>bulbasaur</li>
<li>bellsprout</li>
<li>oddish</li>
</ul>
</li>
<li>Electric
<ul>
<li>pichu</li>
<li>magneton</li>
<li>voltorb</li>
</ul>
</li>
<li>Water
<ul>
<li>squirtle</li>
<li>poliwag</li>
<li>krabby</li>
</ul>
</li>
</ul>
</div>
SCSS
$green: #33cc33;
$blue : #0099ff;
$yellow: #ffcc00;
$red: #ff3333;
#mixin secondUl($color) {
li {
color: white;
background: $color;
min-width: 100px;
border-bottom: 1px solid white;
}
a {
color: white;
font-weight: normal;
text-align: center;
display: block;
width:100% ;
padding: 5px 0;
}
} //secondUl
#container {
width: 600px;
margin: auto;
margin-top: 30px;
border-top: 1px solid black;
color: black;
font-family: arial,
sans-serif;
ul {
margin: 15px 0;
position: relative;
} //ul
} //container
.list-inline {
ul {
position: absolute;
padding: 0;
top: 0;
left: -25% ;
z-index: 2;
display: none;
li {
display: inherit;
min-width: 100px;
margin: 0;
} //li
} //ul
li:nth-of-type(1) ul {
#include secondUl($red);
}
li:nth-of-type(2) ul {
#include secondUl($green);
}
li:nth-of-type(3) ul {
#include secondUl($yellow);
}
li:nth-of-type(4) ul {
#include secondUl($blue);
}
li {
list-style: none;
display: inline-block;
margin: 0 10px;
&:hover ul {
display: block;
}
} //li
li:nth-child(1) a:hover {
color: $red;
}
li:nth-child(2) a:hover {
color: $green;
}
li:nth-child(3) a:hover {
color: $yellow;
}
li:nth-of-type(4) a:hover {
color: $blue;
}
&:first-child a {
text-decoration: none;
color: black;
text-transform: capitalization;
font-weight: 600;
-webkit-transition: color 1s;
transition: color 1s;
-moz-transition: color 1s;
}
}//list-inline
First you have used global css for ul under #container #container ul {position:relative;} this css will be apply on every child ul which is in #container, and its overriding .list-inline ul{position:absolute}, you need to set immediate child selector css #container > ul {position:relative;}.
For second ans. you need to do same thing, use immediate child selector css
http://codepen.io/anon/pen/LRZVRO
For problem 1 add vertical-align:top to your li
li {
list-style: none;
display: inline-block;
margin: 0 10px;
vertical-align: top;
&:hover ul {
display: block;
}
}
For Problem 2 use child instead of decendant selector. E.G:
> li:nth-child(1)>a:hover {
color: $red;
}
See: http://codepen.io/anon/pen/rrpVaV
It works well. you have to add width of li
.list-inline li {
min-width: 110px;
}
And
.list-inline li:hover ul {
display: table;
}
DEMO
https://codepen.io/Dhaarani/pen/vXpONj

Button in CSS3 not clickable

I want to make some fancy social buttons with Font Awesome, CSS3 and lists in HTML. It should look like this. But now it is not possible to click on the anchor (a-tag).
How can I accomplish this?
ul {
list-style: outside none none;
}
ul li {
display: block;
float: left;
background-color: #099;
margin-right: 10px;
padding: 10px 0px;
text-align: center;
width: 40px;
opacity: 0.2;
transition: all 0.2s ease-in-out 0s;
}
li::before {
color: #FFF;
font-size: 35px;
}
ul li a {
color: transparent;
font: 0px/0 a;
white-space: nowrap;
}
ul li:hover {
opacity: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet"/>
<ul id="menu-social" class="menu">
<li id="menu-item-googleplus" class="fa fa-google-plus menu-item menu-item-googleplus">
Google+
</li>
<li id="menu-item-facebook" class="fa fa-facebook menu-item menu-item-facebook">
Facebook
</li>
</ul>
Add position relative to the li elements and then stretch the anchor.
ul li {
display: block;
float: left;
background-color: #099;
margin-right: 10px;
padding: 10px 0px;
text-align: center;
width: 40px;
opacity: 0.2;
transition: all 0.2s ease-in-out 0s;
/* Add position */
position:relative;
}
ul li a {
color: transparent;
font: 0px/0 a;
white-space: nowrap;
/* Stretch a element */
position: absolute;
left:0;
top:0;
width:100%;
height: 100%;
}

CSS drop-down transition doesn't work

I want to make a drop-down menu transition, but it doesn't work at all. Other transitions work just fine but this one is really stubborn. What exactly am I doing wrong ?
In this example, the drop-down does not transition when it is shown:
a {
color: #FFF;
text-decoration: none;
}
li {
height: 20px;
padding-top: 7px;
padding-bottom: 7px;
padding-left: 5px;
padding-right: 5px;
font-size: 17px;
color: #FFF;
display: inline-block;
vertical-align: top;
background-color: #4CA0DB;
}
li:hover {
transition: all 0.3s;
background-color: #4CA0DB;
}
.carret {
position: relative;
margin-left: 5px;
bottom: 1px;
font-size: 12px;
}
/*this is where it breaks*/
.menu-hover ul li {
display: none;
}
.menu-hover:hover ul li {
background-color: #4CA0DB;
display: block;
}
.menu-hover ul {
transition: all 0.3s ease;
}
<nav>
<ul>
<li class="menu-hover">ABOUT THE UNIVERSITY<span class="carret">▼</span>
<ul class="dropdown">
<li>
За нас</li>
<li>
Професии</li>
<li>
Планове</li>
<li>
Специалности</li>
<li>
Преподаватели</li>
<li>
Финансиране</li>
<li>
Кандидатстване</li>
</ul>
</li>
</ul>
</nav>
I finally got it working by using visibility: hidden and visibility: visible instead of display: none and display: block. Thanks for the help! This is how it looks now:
.menu-hover ul li {
display: block;
visibility: hidden;
}
.menu-hover:hover ul li {
background-color: #4CA0DB;
visibility: visible;
transition: all 0.3s ease;
}