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
Related
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.
i'm writing on my website and i've some problems with the following code (full code on codepen):
<section class="sidebar">
<ul class="sidebar-menu clearfix">
<li class="sidebar-menu-item">HOME</li>
<li class="sidebar-menu-item">PORTFOLIO</li>
<li class="sidebar-menu-item">BLOG</li>
<li class="sidebar-menu-item">ABOUT ME</li>
<li class="sidebar-menu-item">CONTACT</li>
</ul>
</section>
https://codepen.io/anon/pen/pdErVq
As you can see all the :after elements are aligned in the first row.
How can i expand the elements in the proper <li> tags?
Give position:relative to .sidebar-menu > li class.
.sidebar {
position: fixed;
background-color: #333;
opacity: 0.75;
width: 300px;
height: 100%;
left: -0px;
top: 0;
}
.sidebar-menu {
position: relative;
top: 100px;
padding-left: 20px;
}
.sidebar-menu > li {
color: #fff;
margin-right: 10px;
list-style: none;
font-size: 25px;
font-style: italic;
padding: 10px 5px;
cursor: pointer;
-webkit-transition: all 0.5s;
transition: all 0.5s;
position:relative;
}
.sidebar > ul > li:after {
content: "";
display: block;
position: absolute;
top: 10px;
right: 0;
background-color: rgba(250,250,250,0.5);
width: 0;
height: 37px;
-webkit-transition: all 0.5s;
transition: all 0.5s;
}
.sidebar-menu > li:hover:after {
width: 280px;
}
<section class="sidebar">
<ul class="sidebar-menu clearfix">
<li class="sidebar-menu-item">HOME</li>
<li class="sidebar-menu-item">PORTFOLIO</li>
<li class="sidebar-menu-item">BLOG</li>
<li class="sidebar-menu-item">ABOUT ME</li>
<li class="sidebar-menu-item">CONTACT</li>
</ul>
</section>
Your li's need position: relative.
You can try this Demo
li{position:relative}
li a{position:absolute;}
I am added a tag in li
So I made a custom before pseudo line and I'm trying to figure out how to vertically align it center to my list items. How does one do that? I tried absolute positioning but it stacks all of them and places them in the middle rather than them being on each list item.
.menu {
background: #ececec;
width: 200px;
margin-top: 40px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 20px;
transition: 0.5s all ease-in-out;
}
.menu li {
padding: 10px 0;
cursor: pointer;
}
.menu ul {
position: relative;
}
.menu li:before {
content: '';
height: 30px;
width: 3px;
background-color: #FE715D;
left: -10px;
position: absolute;
border-radius: 20px;
opacity: 0;
transition: 0.5s all ease-in-out;
}
.menu li:hover:before {
transition: 0.5s all ease-in-out;
opacity: 1;
}
<div class="menu">
<ul>
<li class="lifirst">About Me</li>
<li class="limenu">My Skills<li class="limenu">Portfolio</li>
<li class="limenu">Contact</li>
</ul>
</div>
To prevent the stacking of the pseudo elements, you have to set a position for their corresponding parent.
absolute
The element is removed from the normal document flow; no space is created for the element in the page layout. Instead, it is positioned relative to its closest positioned ancestor if any
—position on MDN
In other words: If you add position: relative; to your <li> elements, every pseudo elements position is depending on its corresponding list item:
.menu {
background: #ececec;
width: 200px;
margin-top: 40px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 20px;
transition: 0.5s all ease-in-out;
}
.menu li {
padding: 10px 0;
cursor: pointer;
position: relative;
}
.menu ul {
position: relative;
}
.menu li:before {
content: '';
height: 30px;
width: 3px;
background-color: #FE715D;
left: -40px;
top: 7px;
position: absolute;
border-radius: 20px;
opacity: 0;
transition: 0.5s all ease-in-out;
}
.menu li:hover:before {
transition: 0.5s all ease-in-out;
opacity: 1;
}
<div class="menu">
<ul>
<li class="lifirst">About Me</li>
<li class="limenu">My Skills</l<li class="limenu">Portfolio</li>
<li class="limenu">Contact</li>
</ul>
</div>
So i ve been working on this for several hours.. i have a problem in the nav bar where the content refuses to go display: inline. Previous versions worked fine but were too disorganized , code wise. here is my code.
CSS:
body {
background-color: #5C7584;
margin: 0 auto;
//width: 1000px;
width: 100%;
margin-top: 10px;
}
#canvas {
width: 1000px;
margin: 0 auto;
background-color: white;
}
a {
color: #4086b6;
text-decoration: none;
}
///////////////////////////////////
ul, li {
list-style: none;
}
ul li {
//padding-right: 10px;
//padding-left: 10px;
//display: inline;
}
.parentClass {
display: inline;
//position: relative;
list-style: none;
//float: left;
}
.dropDown {
position: relative;
background-color: lightcyan;
display: block;
list-style: inherit;
//text-align: right;
//background-color: lightcyan;
width: auto;
height: 0px;
visibility: hidden;
opacity: 0;
-webkit-transition: all .25s;
-moz-transition: all .25s;
-ms-transition: all .25s;
-o-transition: all .25s;
transition: all .5s;
}
#linkline{
}
.parentClass:hover {
background-color: lightcyan;
}
.parentClass:hover .dropdown {
opacity: 1;
visibility: visible;
height: auto;
}
/////////////////////////////
#toplinks a {
//padding-left: 0px;
text-shadow: 1px 1px lightgrey;
}
#toplinks a:hover {
color: lightskyblue;
//border-radius: 50px 50px;
}
#toplinks a:active {
color: white;
}
#top {
height: 102px;
background-image: url("../assets/topbar_plain.png");
background-color: white;
opacity: .9;
z-index: 2;
position: relative;
}
#toplinks {
float: right;
width: 500px;
text-align: right;
padding-right: 30px;
padding-top: 70px;
}
#logo {
background-image: url("../assets/logo_plain.png");
width: 102px;
height: 33px;
float: left;
background-repeat: no-repeat;
margin-top: 40px;
margin-left: 80px;
}
HTML:
<div id="top" >
<div id="canvas">
<div id="toplinks">
<ul class="linkline">
<li id="indexx" class="parentClass">Overview
<ul></ul>
</li>
<li class="parentClass">Services
<ul class="dropDown">
<li class="c2"> DDoS Mitigation </li>
<li class="c1"> Server Hosting </li>
</ul>
</li>
<li class="parentClass">AboutUs
<ul class="dropDown">
<li class="c2"> Link 1 </li>
<li class="c1"> Link 2 </li>
<li class="c2"> Link 3 </li>
</ul>
</li>
</ul>
</div>
</div>
</div>
Try with:
display:inline-block
ul {
list-style: none;
}
li {
float: left;
display: block;
margin: 0 1em 0 0;
border: 1px solid purple;
}
<ul>
<li>Menu item 1</li>
<li>Menu item 2</li>
</ul>
Hi, I simplified your code down to the lists and their styling which is what you need to fix. Make sure ul is list-style none and the li's in that list are float left display block and then style from there.
It looks like something strange is happening because of the <ul class="dropDown"> nested inside the <li class="parentClass">. Try this as a start:
.dropDown {
display: none;
}
.parentClass {
display: inline-block;
}
Try to change the parentClass like that.
.parentClass {
display: inline-block;
position: relative;
list-style: none;
float: left;
}
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/