Fade-in effect on a drop-down menu doesn't happen always - html

I want my drop-down menu to have a fade-in effect when mouse-hovered. I've written the following code for that, but the fade-in effect can be observed sometimes only, not always. Code:
* {
margin: 0;
padding: 0;
}
.header {
position: fixed;
list-style-type: none;
height: 35px;
width: 100%;
background: #333333;
}
.header > li {
display: inline-block;
padding: 8px;
position: relative;
color: #FFF;
}
.header > li:hover {
background: #000000;
}
.dropdown ul {
list-style-type: none;
display: none;
position: absolute;
top: 100%;
left: 0;
color: red;
width: 100%;
opacity: 0;
background: yellow;
border: 1px solid black;
transition: opacity 1s;
}
.dropdown:hover ul {
opacity: 1;
display: block;
}
<ul class="header">
<li class="home">Test</li>
<li class="dropdown">Dropdown ❱
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
</ul>
I think the effect doesn't occur when I unhover and hover over "Dropdown ❱" within a second. I'm trying to do this using HTML and CSS only.
How can I achieve what I want?
BTW, How do I create a fade-out effect for the menu?

display is not an animatable property so you can use visibility instead of display.
Here is the list of animatable-properties.
* {
margin: 0;
padding: 0;
}
.header {
position: fixed;
list-style-type: none;
height: 35px;
width: 100%;
background: #333333;
}
.header > li {
display: inline-block;
padding: 8px;
position: relative;
color: #FFF;
}
.header > li:hover {
background: #000000;
}
.dropdown ul {
list-style-type: none;
/*display: none;*/
visibility: hidden;
position: absolute;
top: 100%;
left: 0;
color: red;
width: 100%;
opacity: 0;
background: yellow;
border: 1px solid black;
transition: visibility 1s, opacity 1s;
}
.dropdown:hover ul {
opacity: 1;
visibility: visible;
}
<ul class="header">
<li class="home">Test</li>
<li class="dropdown">Dropdown ❱
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
</ul>

You can use a little jQuery to achieve this...
$('.header ul').hide();
$('.header li').hover(function(){
$(this).find('ul').fadeIn();
}, function(){
$(this).find('ul').fadeOut();
});
JSFIDDLE link here: https://jsfiddle.net/omerblink/7n59kew1/
And if you still wanna keep it to just HTML / CSS then just discard the idea of display and use visibility.
.dropdown ul {
list-style-type: none;
visibility: hidden;
position: absolute;
top: 100%;
left: 0;
color: red;
width: 100%;
opacity: 0;
background: yellow;
border: 1px solid black;
transition: visibility 1s, opacity 1s;
}
.dropdown:hover ul {
opacity: 1;
visibility: visible;
}
JSFIDDLE link to HTML/CSS solution: https://jsfiddle.net/omerblink/jwd9hLo1/

Related

Tabs like a little vibrate on hover in HTML, CSS

I want to create a menu but, when it's finished the tab looks like it shakes a little on hover, like when it's hovered, the text on the tab goes up a bit and then drops back to normal
* {
margin: 0px;
padding: 0px;
background: #dbdbea;
}
.tab {
width: 100%;
height: 100px;
position: absolute;
background: #999999;
}
.tab ul {
display: inline-flex;
background: #999999;
position: absolute;
margin-top: 29px;
margin-left: 15px;
}
.tab ul li {
background: #999999;
list-style: none;
}
.tab ul li a {
background: #999999;
color: #222222;
margin: 0px 11.666px 0px 11.666px;
padding: 0px 23.334px 0px 23.334px;
text-decoration-line: none;
font-family: arial;
font-size: 36px;
transition: 1s;
}
.tab ul li a:hover {
background: #666666;
margin: 0px;
padding: 30px 35px 30px 35px;
}
<div class="tab">
<ul>
<li>Link 1</li>
<li>Link 2</li>
</ul>
</div>
I tried everything I could but, the results still weren't what I wanted, i was really confused about how to fix this bug
can someone tell me what's wrong with the code?
Can you please check the below code? Hope it will work for you. We have removed some flex utilities and give hover effect using pseudo-element with the transition.
Please refer to this link: https://jsfiddle.net/yudizsolutions/p41tnefh/4/
* {
margin: 0px;
padding: 0px;
background: #dbdbea;
}
.tab ul {
display: flex;
align-items: center;
background: #999;
padding-left: 15px;
list-style: none
}
.tab ul li {
align-items: center;
background: #999;
}
.tab ul li a {
color: #222222;
text-decoration: none;
font-family: arial;
font-size: 36px;
padding: 29px 25px;
transition: 1s;
display: block;
background: #999;
position: relative;
overflow: hidden;
z-index: 1;
}
.tab ul li a::before {
content: '';
display: black;
position: absolute;
width: 100%;
height: 100%;
left: 50%;
top: 50%;
background: #666666;
height: 50%;
width: 80%;
transition: 0.6s all ease-in-out;
-webkit-transition: 0.6s all ease-in-out;
z-index: -1;
transform: translate(-50%, -50%);
opacity: 0;
}
.tab ul li a:hover:before {
opacity: 1;
height: 100%;
width: 100%;
}
<div class="tab">
<ul>
<li>Link 1</li>
<li>Link 2</li>
</ul>
</div>

Hover submenu doesn't stay opened

I made full width dropdown submenu. the problem is that submenu disappears when I try to move mouse from mainlist to submenu. Also, transition on submenu is not applied. Code I wrote is at down below. Please check it and correct it.
body {
margin: 0;
padding: 0;
}
ul,
li,
a {
list-style: none;
text-decoration: none;
}
.wrap {
position: relative;
width: 100%;
height: 100px;
}
.list {
margin: 0;
padding: 0;
width: 100%;
left: 0;
top: 100px;
height: 100px;
text-align: center;
}
.list li {
display: inline-block;
margin: 20px;
}
.list>li:hover ul {
display: list-item;
opacity: 1;
}
.list>li:hover>a {
color: red;
}
.sub_list {
margin: 0;
padding: 0;
position: absolute;
display: none;
width: 100%;
height: 100px;
left: 0;
top: 50px;
text-align: center;
opacity: 0;
transition: all 0.5s;
}
.sub_list li {
display: inline-block;
margin: 20px;
}
.sub_list li a:hover {
color: red;
}
<div class="wrap">
<ul class="list">
<li>list-1
<ul class="sub_list">
<li>sublist-a</li>
<li>sublist-b</li>
<li>sublist-c</li>
</ul>
</li>
<li>list-2</li>
<li>list-3</li>
<li>list-4</li>
<li>list-5</li>
</ul>
</div>
I'd like to make submenu stay visible when mouse is on whole area of submenu div(100% width of screen).
please help
thank you
In addition to the problem regarding margin/padding/positioning addressed in other answers, the transition wouldn't work because you can't transition from display: none; to another state or vice versa. Instead, solely rely on opacity and add the pointer-events property so that the submenu will not itself trigger the hover or overlay any other content when it's hidden.
Here's the fully working code:
body {
margin: 0;
padding: 0;
}
ul, li, a {
list-style: none;
text-decoration: none;
}
.wrap {
position: relative;
width: 100%;
height: 100px;
}
.list {
margin: 0;
padding: 0;
width: 100%;
left: 0;
top: 100px;
height: 100px;
text-align: center;
}
.list li {
display: inline-block;
padding: 20px;
}
.list > li:hover ul {
pointer-events: all;
opacity: 1;
}
.list > li:hover > a {
color: red;
}
.sub_list {
margin: 0;
padding: 0;
position: absolute;
width: 100%;
height: 100px;
left: 0;
top: 50px;
text-align: center;
opacity: 0;
transition: all 0.5s;
pointer-events: none;
}
.sub_list li {
display: inline-block;
margin: 20px;
}
.sub_list li a:hover {
color: red;
}
<div class="wrap">
<ul class="list">
<li>list-1
<ul class="sub_list">
<li>sublist-a</li>
<li>sublist-b</li>
<li>sublist-c</li>
</ul>
</li>
<li>list-2
<ul class="sub_list">
<li>sublist-a</li>
<li>sublist-b</li>
<li>sublist-c</li>
</ul>
</li>
<li>list-3
<ul class="sub_list">
<li>sublist-a</li>
<li>sublist-b</li>
<li>sublist-c</li>
</ul>
</li>
<li>list-4
<ul class="sub_list">
<li>sublist-a</li>
<li>sublist-b</li>
<li>sublist-c</li>
</ul>
</li>
<li>list-5
<ul class="sub_list">
<li>sublist-a</li>
<li>sublist-b</li>
<li>sublist-c</li>
</ul>
</li>
</ul>
</div>
I fixed the issue for you: https://codepen.io/anon/pen/rboPLE
This is what i changed:
.list li {
display: inline-block;
padding: 20px; // this line was margin: 20px; before
}
When trying to reach the submenu you left the .list li item because it had a margin. With Padding the space belongs to the element and its still hovered when you move the mouse to submenu.
I colored the example in the link above so you can see the elements boundaries.
your code is perfect but minor issues is there.
use this css code:
.sub_list {
opacity: 0;
transition-duration: 200ms;
transition-timing-function: ease-in;
transition-property: opacity, margin-top, visibility;
visibility: hidden;
margin: 50px 0 0;
padding: 0;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 50px;
text-align: center;
}
.list > li:hover ul {
margin-top:0;
opacity: 1;
visibility: visible;
}
use this code its work perfect transition effect and dropdown submenu issues is solved.

Drop-down list doesn't appear on mouse hover

I'm trying to make a drop-down menu appear when mouse-hovered, but for some reason, the menu doesn't appear.
* {
margin: 0;
padding: 0;
}
.header {
position: fixed;
list-style-type: none;
height: 35px;
width: 100%;
overflow: hidden;
background: #333333;
}
.header > li {
display: inline-block;
padding: 8px;
}
.header > li:hover {
background: #000000;
}
.home {
margin-left: 10%;
}
.home a:link {
color: #FFF;
}
.home a:visited {
color: #AAA;
}
.home a:hover {
color: #00F;
}
.home a:active {
color: #F00;
}
.dropdown {
display: block; /* I guess that something is wrong here */
width: auto;
color: #FFF;
}
.dropdown li {
list-style-type: none;
display: block; /* I guess that something is wrong here too */
position: absolute; /* and here */
top: 100%;
color: red;
width: auto;
opacity: 0;
background: yellow;
border: 1px solid black;
transition: opacity 1s;
}
.dropdown:hover,
.dropdown li:hover {
opacity: 1;
}
.main {
margin-top: 20px;
padding-top: 20px;
height: 50px;
display: block;
background: white;
text-align: center;
font-size: 20px;
transition: background 1s;
}
.main:hover {
background: #CCCCCC;
}
<body>
<ul class="header">
<li class="home">⌂ Home
</li>
<li class="dropdown">Dropdown ❱
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
</ul>
<br>
<section>
<div class="main">
Content...
</div>
</section>
</body>
I'm trying to do this using CSS and HTML only.
I've commented beside three lines which I think are causing the problem, but I can't seem to figure it out. What is the issue? How do I fix it?
Firstly, we remove the overflow:hidden from the header which would stop the submenus from showing at all.
Then the parent li should be set to position:relative and the child ul to position:absolute (not the individual li).
Remove the display:none on :hover and there you are.
* {
margin: 0;
padding: 0;
}
.header {
position: fixed;
list-style-type: none;
height: 35px;
width: 100%;
/*overflow: hidden;*/
background: #333333;
}
.header > li {
display: inline-block;
padding: 8px;
}
.header > li:hover {
background: #000000;
}
.home {
margin-left: 10%;
}
.home a:link {
color: #FFF;
}
.home a:visited {
color: #AAA;
}
.home a:hover {
color: #00F;
}
.home a:active {
color: #F00;
}
.dropdown {
color: #FFF;
position: relative;
}
.dropdown ul {
list-style-type: none;
position: absolute;
/* and here */
top: 100%;
left: 0;
color: red;
width: auto;
display: none;
background: yellow;
border: 1px solid black;
}
.dropdown:hover ul {
display: block;
}
.main {
margin-top: 20px;
padding-top: 20px;
height: 50px;
display: block;
background: white;
text-align: center;
font-size: 20px;
transition: background 1s;
}
.main:hover {
background: #CCCCCC;
}
<body>
<ul class="header">
<li class="home">⌂ Home
</li>
<li class="dropdown">Dropdown ❱
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
</ul>
<br>
<section>
<div class="main">
Content...
</div>
</section>
</body>

Sub-menu :before transition

I am trying to implement a submenu with a border-left looking bar that transitions in height from 0 to 100% upon hover of the main nav element. For some reason the transition is not working. Any help?
http://codepen.io/matthewmorrisux/pen/addLZE?editors=110
<div class="container">
<nav class="menu">
<ul>
<li class="menu__item">Home</li>
<li class="menu__item">About<ul class="sub-menu">
<li class="sub-menu__item">Item 1</li>
<li class="sub-menu__item">Item 2</li>
<li class="sub-menu__item">Item 3</li>
</ul></li>
<li class="menu__item">Process</li>
<li class="menu__item">Contact</li>
</ul>
</nav>
</div>
.menu {
position: relative;
}
.menu__item {
position: relative;
display: inline-block;
padding: 20px;
}
.sub-menu {
position: absolute;
display: none;
}
.sub-menu:before {
content: '';
display: block;
position: absolute;
width: 2px;
height: 0px;
background: black;
transition: height 1s ease;
}
.menu__item:hover .sub-menu {
display: block;
}
.menu__item:hover .sub-menu:before {
content: '';
display: block;
position: absolute;
width: 2px;
height: 100px;
background: black;
transition: height 1s ease;
}
You are using the display property to toggle the visibility between none and block, this does not work well with transitions.
Instead, try hiding the menu and its content by setting the height to 0:
.sub-menu {
position: absolute;
display: block;
height: 0;
overflow: hidden;
}
Then your .sub-menu also needs a height:
.menu__item:hover .sub-menu {
display: block;
height: 100px;
}
http://codepen.io/anon/pen/addLPR?editors=110

How do I align a dropdown menu below a <div>?

When I hover over "Dropdown >", the dropdown menu appears at the left side below the top bar. How do I align it just below "Dropdown >"?
Note that I am trying to do this with only CSS and HTML.
My Code:
* {
margin: 0;
padding: 0;
}
header {
position: fixed;
height: 35px;
width: 100%;
background: black;
}
.top-container {
margin-top: 7px;
}
/* Code for drop-down list */
.dropdown {
margin-left: 100px;
display: inline;
color: #FFF;
}
.dropdown_list {
list-style: none;
display: none;
position: absolute;
color: red;
}
.dropdown_list li {
background: yellow;
}
.dropdown:hover {
background: #333;
}
.dropdown:hover .dropdown_list,
.dropdown_list:hover {
display: block;
top: 100%;
}
<body>
<header>
<div class="top-container">
<div class="dropdown">Dropdown ❱
<ul class="dropdown_list">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</div>
</div>
</header>
</body>
Change .dropdown from inline to inline-block:
.dropdown {
display: inline-block;
}
Snippet:
* {
margin: 0;
padding: 0;
}
header {
position: fixed;
height: 35px;
width: 100%;
background: black;
}
.top-container {
margin-top: 7px;
}
/* Code for drop-down list */
.dropdown {
margin-left: 100px;
display: inline-block;
color: #FFF;
}
.dropdown_list {
list-style: none;
display: none;
position: absolute;
color: red;
}
.dropdown_list li {
background: yellow;
}
.dropdown:hover {
background: #333;
}
.dropdown:hover .dropdown_list,
.dropdown_list:hover {
display: block;
top: 100%;
}
<body>
<header>
<div class="top-container">
<div class="dropdown">Dropdown ❱
<ul class="dropdown_list">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</div>
</div>
</header>
</body>
Try this
Add position:relative to class .dropdown now your .dropdown_list is relative to the .dropdown you can use top left right bottom to place your .dropdown_list as you want.
* {
margin: 0;
padding: 0;
}
header {
position: fixed;
height: 35px;
width: 100%;
background: black;
}
.top-container {
margin-top: 7px;
}
/* Code for drop-down list */
.dropdown {
margin-left: 100px;
display: inline;
color: #FFF;
position: relative;
}
.dropdown_list {
list-style: none;
display: none;
position: absolute;
color: red;
top: 0;
left: 0;
}
.dropdown_list li {
background: yellow;
}
.dropdown:hover {
background: #333;
}
.dropdown:hover .dropdown_list,
.dropdown_list:hover {
display: block;
top: 100%;
}
<body>
<header>
<div class="top-container">
<div class="dropdown">Dropdown ❱
<ul class="dropdown_list">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</div>
</div>
</header>
</body>
Add position: relative to .dropdown and left:0 to .dropdown_list
.dropdown {
margin-left: 100px;
display: inline;
position: relative;/*add this*/
color: #FFF;
}
.dropdown_list {
list-style: none;
display: none;
position: absolute;
color: red;
left: 0; /*add this*/
}
Apply the left-margin to the container and set position: relative for the popup menu.
Example: https://jsfiddle.net/jt83vj28/