Why animation on nav bar, that red line, if you set 100% width on nav li a::after works backward and not from left to right side how it should be or how I want to?
HTML
<nav>
<ul>
<li>Domů</li>
<li>Fun Switcher</li>
<li>Radio</li>
<li>Fotky</li>
<li>Games</li>
<li>Chat</li>
</ul>
</nav>
CSS
nav {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
background: linear-gradient(#ffffff, #f5f5f5);
grid-area: nav;
font-weight: bold;
}
nav ul {
list-style: none;
margin: 0;
display: flex;
justify-content:flex-start;
align-items: center;
padding: 1em 0;
}
nav li a {
text-decoration: none;
color: #000000;
padding-left: 3em;
position: relative;
transition: all ease-in-out 250ms;
}
nav li a::after {
content:'';
position: absolute;
display: block;
height: 0.4em;
background-color: red;
bottom: -1em;
/* width: 100%; look for whole line */
transition: all ease-in-out 250ms;
}
nav li a:hover::after {
width: 60%;
}
nav li a:hover{
color: red;
}
You want to change multiple things because some css property is not appropriate for your html class...
nav {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
background: linear-gradient(#ffffff, #f5f5f5);
grid-area: nav;
font-weight: bold;
}
nav ul {
list-style: none;
margin: 0;
display: block;
}
nav li {
padding: 13px 20px;
display: inline-block;
}
nav li a {
text-decoration: none;
color: #000000;
position: relative;
transition: all ease-in-out 250ms;
}
nav li a::after {
content:'';
position: absolute;
display: block;
height: 0.4em;
background-color: red;
bottom: -1em;
/* width: 100%; look for whole line */
transition: all ease-in-out 350ms;
left: 0;
width:0;
}
nav li a:hover::after {
width: 100%;
}
nav li a:hover{
color: red;
}
<nav>
<ul>
<li>Domů</li>
<li>Fun Switcher</li>
<li>Radio</li>
<li>Fotky</li>
<li>Games</li>
<li>Chat</li>
</ul>
</nav>
Is this an accpetable solution for you?
`a{
display: flex;
align-items: center;
justify-content: center;
}`
add this to your css code
heres my codepen:
https://codepen.io/oskedram/pen/abWREpg
Navigation bar presently drops down, 1st sub menu expands left, 2nd sub menu expands right so that it's on top of the primary menu. Trying to get the 2nd sub menu to continue expanding left, but any tweaks I make don't change it. Tried to get the ul last child to pull right without success, would appreciate any help. Here's the code I've currently got.
HTML:
<li {{#id}}id="{{id}}"{{/id}}
class="wsite-menu-subitem-wrap {{#is_current}}wsite-nav-current{{/is_current}}"
>
<a
{{^nonclickable}}
{{^nav_menu}}
href="{{url}}"
{{/nav_menu}}
{{/nonclickable}}
{{#target}}
target="{{target}}"
{{/target}}
class="wsite-menu-subitem"
>
<span class="wsite-menu-title">
{{{title_html}}}
</span>{{#has_children}}<span class="wsite-menu-arrow">></span>{{/has_children}}
</a>
{{#has_children}}{{> navigation/flyout/list}}{{/has_children}}
<div class="wsite-menu-wrap" style="display:none">
<ul class="wsite-menu">
{{#children}}{{> navigation/flyout/item}}{{/children}}
</ul>
</div>
CSS:
/* Navigation Menu*/
#nav {position: relative;}
#nav ul {
text-align: right;
overflow: hidden;
}
#nav ul li {
list-style: none;
display: inline-block;
}
#nav ul li a {
display: block;
color: #8e8e8e;
padding: 30px 15px;
font-size: 14px;
text-transform: uppercase;
-webkit-transition: all 240ms linear;
-moz-transition: all 240ms linear;
-o-transition: all 240ms linear;
-ms-transition: all 240ms linear;
transition: all 240ms linear;
}
.minimize #nav ul li a {
padding: 15px;
}
#nav > ul li:first-child a {
padding-left: 30px !important;
}
#nav > ul li:last-child a {
padding-right: 0 !important;
}
#nav ul li#active a,
#nav ul li a:hover {
color: #primary;
text-decoration:none;
border: 0;
}
#mobile-nav, #mobile-input, #nav-trigger, #mobile-cart {
display: none;
}
/* Navigation Submenu*/
#wsite-menus {
position: relative;
z-index: 14;
}
#wsite-menus .wsite-menu {
padding: 10px 0;
background: #headerBg;
-webkit-box-shadow: 0px 0px 3px rgba(99, 99, 99, .1);
box-shadow: 0px 0px 3px rgba(99, 99, 99, .1);
}
#wsite-menus > .wsite-menu-wrap > .wsite-menu .wsite-menu-wrap {
position: absolute !important;
top: 0 !important;
margin-top: -10px !important;
}
#wsite-menus .wsite-menu li a {
font-family: 'Roboto', sans-serif;
color: #8e8e8e;
background: transparent !important;
font-size:13px;
text-transform: uppercase;
border: 0;
padding: 10px 20px;
}
#wsite-menus .wsite-menu li a > span {
padding: 0;
}
#wsite-menus .wsite-menu li a:hover {
color: #primary;
}
#wsite-menus .wsite-menu-arrow {
right: 20px;
width: 10px;
overflow: hidden;
}
#wsite-menus .wsite-menu-arrow:before {
display: inline-block;
content: '\25b6';
font-size: 75%;
vertical-align: top;
line-height: 1.5;
}
Try setting the menu's container element to display: flex; and flex-direction: column;. This way child elements will never be one next to the other.
Edit:
Forget the flex.
I'll suppose you need to put your nav to the right (that's why you want submenus to the left), and you need submenus to display one at a time.
Assuming that, I've made an example so you can see it working.
Note: Just check the commented style, other things are just visuals to match your context and to be easy to scan.
* {
margin: 0;
padding: 0;
}
#root {
display: flex;
}
nav {
margin-left: auto;
margin-right: 100px;
background-color: rgb(30, 30, 30);
color: white;
width: 160px;
}
li {
list-style: none;
padding: 4px;
}
li:hover {
background-color: rgb(90, 90, 90);
}
a {
cursor: pointer;
}
/* dropdown box */
.dropdown {
position: relative;
}
/* dropdown button, it's just a span,
you can use whatever you want */
.dropdown .dropdown_btn {}
/* dropdown content default */
.dropdown .dropdown-content {
display: none;
position: absolute;
z-index: 1;
top: 0;
/* "left -100%" gives same result as "right: 100%"*/
left: -100%;
/* keep size to mach the "left -100%" */
min-width: 100%;
background-color: rgb(60, 60, 60);
}
/* while mouse hover, content will display */
.dropdown:hover .dropdown-content {
display: block;
}
<div id="root">
<nav>
<ul>
<li><a>Item 1</a></li>
<li class="dropdown">
<span class="dropdown-btn">Item 2 / Drop A</span>
<ul class="dropdown-content">
<li><a>Drop A Item 1</a></li>
<li><a>Drop A Item 2</a></li>
</ul>
</li>
<li class="dropdown">
<span class="dropdown-btn">Item 3 / Drop B</span>
<ul class="dropdown-content">
<li><a>Drop B Item 1</a></li>
<li><a>Drop B Item 2</a></li>
</ul>
</li>
<li><a>Item 4</a></li>
</ul>
</nav>
</div>
I have a task to create a drop-down menu and i create it with using absolute and negative value but i can change my mind i want to create a drop-down without using absolute there is any possibility to create and how can do it kindly help in this query
.mainmenu {
position: relative;
cursor: pointer;
display: inline-block;
padding: 15px;
-webkit-transition: all .5s;
}
.submenu {
position: absolute;
height: 0px;
opacity: 0;
-webkit-transition: all .5s;
}
.mainmenu:hover {
border: 1px solid #000;
-webkit-transition: all .5s;
}
.mainmenu:hover .submenu {
border: 1px solid #000;
padding: 50px;
height: auto;
top: 100%;
left: -1px;
opacity: 1;
-webkit-transition: all .5s;
}
.mainmenu:hover .hide_line {
height: 5px;
width: 100%;
position: absolute;
top: 100%;
right: 0px;
background: #fff;
z-index: 1000;
}
<div class="mainmenu">
Menu
<div class="hide_line">
</div>
<div class="submenu">
submenu
</div>
</div>
You can use list for this purpose
.mainmenu a{
text-decoration:none;
color:#000000;
}
.mainmenu {
position: relative;
cursor: pointer;
display: inline-block;
padding: 15px;
-webkit-transition: all .5s;
list-style:none;
}
.mainmenu > li{
padding-left:0px;
margin-left:0px;
}
.submenu {
opacity:0;
list-style:none;
}
.mainmenu:hover .submenu {
border: 1px solid #000;
height: auto;
opacity: 1;
-webkit-transition: all .5s;
}
.submenu li{
padding-left:0px;
margin-left:0px;
}
<ul class="mainmenu">
<li><a href='#'>Main Menu</a>
<ul class="submenu">
<li><a href='#'>Sub menu</a></li>
</ul>
</li>
</ul>
I hope you can do rest of the formatting
I've created a simple dropdown menu that scales and fades in whenever I hover over the parent list item, "Services". Everything works as it should except for when I hover over the child element (dropdown menu) which also makes it appear. I understand that simply hiding it with opacity will not prevent a user from hovering over it but it allows for the 0.4s transition to take effect. If I were to hide it with display:none or visibility:hidden, the transition becomes instant. I'm considering using jQuery to solve this but was wondering if there is a solution based purely in CSS.
JSFiddle
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
<nav>
<ul>
<li>
[Logo]
</li>
<li>
About
</li>
<li class="hover">
Services
<div class="services-list-container">
<ul>
<li>
Information
</li>
<li>
Technology
</li>
<li>
Training
</li>
<li>
Project Support
</li>
</ul>
</div>
</li>
<li>
News
</li>
<li>
Jobs
</li>
<li>
Contact
</li>
</ul>
</nav>
It's not that you are hovering over the child element, it's that your list elements are so tall from the height:60px that when you hover under the words, it's still going over the li element.
If you are targeting supporting browsers you can use pointer-events to prevent the submenu from causing a hover mouse event
Give your element pointer-events:none to disable
.services-list-container {
pointer-events:none;
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
Then to make sure you can still enter the submenu, reset pointer-events while your .hover elements are hovered
.hover:hover .services-list-container {
pointer-events:all;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
Demo
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
pointer-events:none;
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
pointer-events:all;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
<nav>
<ul>
<li>
[Logo]
</li>
<li>
About
</li>
<li class="hover">
Services
<div class="services-list-container">
<ul>
<li>
Information
</li>
<li>
Technology
</li>
<li>
Training
</li>
<li>
Project Support
</li>
</ul>
</div>
</li>
<li>
News
</li>
<li>
Jobs
</li>
<li>
Contact
</li>
</ul>
</nav>
A combination of visibility:hidden; with the opacity changes helps you to keep that animation while still preventing it from triggering outside of the intended menu area:
https://jsfiddle.net/4wg4sbqy/
CSS:
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
visibility: hidden;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
visibility: visible;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
HTML:
<nav>
<ul>
<li>
[Logo]
</li>
<li>
About
</li>
<li class="hover">
Services
<div class="services-list-container">
<ul>
<li>
Information
</li>
<li>
Technology
</li>
<li>
Training
</li>
<li>
Project Support
</li>
</ul>
</div>
</li>
<li>
News
</li>
<li>
Jobs
</li>
<li>
Contact
</li>
</ul>
</nav>
Please check if this is what you want. Submenu is hidden until the hover takes place.
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav>ul>li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165, 175, 185, 1);
}
nav>ul>li>a {
padding: 0 20px;
line-height: 60px;
}
nav>ul>li:hover>a {
color: rgba(105, 115, 125, 1);
}
nav>ul>li:hover .services-list-container {
display: inline-block;
}
nav>ul>li:first-of-type {
margin-right: auto;
}
.services-list-container {
visibility: hidden;
z-index: -1;
position: absolute;
width: 200px;
top: 100%;
opacity: 0;
transition: opacity .4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
background-color: rgba(235, 240, 245, 1);
}
.hover:hover .services-list-container {
z-index: 0;
visibility: visible;
opacity: 1;
}
<nav>
<ul>
<li>
[Logo]
</li>
<li>
About
</li>
<li class="hover">
Services
<div class="services-list-container">
<ul>
<li>
Information
</li>
<li>
Technology
</li>
<li>
Training
</li>
<li>
Project Support
</li>
</ul>
</div>
</li>
<li>
News
</li>
<li>
Jobs
</li>
<li>
Contact
</li>
</ul>
#menu {
width: 100%;
margin: 10px auto;
}
#menu ul,
#menu li {
margin: 0 auto;
padding: 0 0;
list-style: none
}
#menu ul {
width: 100%;
}
#menu li {
float: left;
display: inline;
position: relative;
}
#menu a {
display: block;
line-height: 35px;
padding: 0 14px;
text-decoration: none;
}
#menu li a {
border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
color: #444;
background-color: #e5e5e5;
}
#menu input {
display: none;
margin: 0 0;
padding: 0 0;
width: 80px;
height: 30px;
opacity: 0;
}
#menu label {
font-size: 20px;
display: none;
width: 35px;
height: 20px;
line-height: 20px;
text-align: center;
color: #77778b
}
#menu label span {
position: absolute;
left: 35px;
}
#menu ul.menus {
height: auto;
overflow: hidden;
width: 180px;
border-top: 1px solid #e5e5e5;
border-left: 1px solid #e5e5e5;
border-bottom: 1px solid #e5e5e5;
position: absolute;
z-index: 99;
display: none;
left: -1px;
}
#menu ul.menus li {
display: block;
width: 100%;
}
#menu ul.menus a {
color: #77778b;
}
#menu li:hover ul.menus {
display: block
}
<nav id='menu'>
<input type='checkbox' />
<label><i class='icon-reorder' /><span>Menu</span>
</label>
<ul>
<li><a href='/'>Home</a>
</li>
<li><a class='dropdown' href='#'>Menu1</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
</ul>
</li>
<li><a class='dropdown' href='#'>Menu2</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
<li><a href='#'>Submenu3</a>
</li>
</ul>
</li>
</ul>
</nav>
How can I make the Dropdowns Fade in with only CSS? A bit noobish question but still trying to understand CSS Any one have any idea how to make it possible plz suggest it.
Thanks !
One possible solution is to use opacity: 0 instead of display: none and using transition on hover:
#menu {
width: 100%;
margin: 10px auto;
}
#menu ul,
#menu li {
margin: 0 auto;
padding: 0 0;
list-style: none
}
#menu ul {
width: 100%;
}
#menu li {
float: left;
display: inline;
position: relative;
}
#menu a {
display: block;
line-height: 35px;
padding: 0 14px;
text-decoration: none;
}
#menu li a {
border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
color: #444;
background-color: #e5e5e5;
}
#menu input {
display: none;
margin: 0 0;
padding: 0 0;
width: 80px;
height: 30px;
opacity: 0;
}
#menu label {
font-size: 20px;
display: block;
width: 35px;
height: 20px;
line-height: 20px;
text-align: center;
color: #77778b;
visibility: hidden;
}
#menu label span {
position: absolute;
left: 35px;
}
#menu ul.menus {
height: auto;
overflow: hidden;
width: 180px;
border-top: 1px solid #e5e5e5;
border-left: 1px solid #e5e5e5;
border-bottom: 1px solid #e5e5e5;
position: absolute;
z-index: 99;
display: block;
left: -1px;
opacity: 0;
}
#menu ul.menus li {
display: block;
width: 100%;
}
#menu ul.menus a {
color: #77778b;
}
#menu li:hover ul.menus {
opacity: 1;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
<nav id='menu'>
<input type='checkbox' />
<label><i class='icon-reorder' /><span>Menu</span>
</label>
<ul>
<li><a href='/'>Home</a>
</li>
<li><a class='dropdown' href='#'>Menu1</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
</ul>
</li>
<li><a class='dropdown' href='#'>Menu2</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
<li><a href='#'>Submenu3</a>
</li>
</ul>
</li>
</ul>
</nav>
Also you can use transition same way to animate on mouse out:
#menu {
width: 100%;
margin: 10px auto;
}
#menu ul,
#menu li {
margin: 0 auto;
padding: 0 0;
list-style: none
}
#menu ul {
width: 100%;
}
#menu li {
float: left;
display: inline;
position: relative;
}
#menu a {
display: block;
line-height: 35px;
padding: 0 14px;
text-decoration: none;
}
#menu li a {
border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
color: #444;
background-color: #e5e5e5;
}
#menu input {
display: none;
margin: 0 0;
padding: 0 0;
width: 80px;
height: 30px;
opacity: 0;
}
#menu label {
font-size: 20px;
display: block;
width: 35px;
height: 20px;
line-height: 20px;
text-align: center;
color: #77778b;
visibility: hidden;
}
#menu label span {
position: absolute;
left: 35px;
}
#menu ul.menus {
height: auto;
overflow: hidden;
width: 180px;
border-top: 1px solid #e5e5e5;
border-left: 1px solid #e5e5e5;
border-bottom: 1px solid #e5e5e5;
position: absolute;
z-index: 99;
display: block;
left: -1px;
opacity: 0;
-webkit-transition: opacity 0.5s ease-in-out;
-moz-transition: opacity 0.5s ease-in-out;
-ms-transition: opacity 0.5s ease-in-out;
-o-transition: opacity 0.5s ease-in-out;
transition: opacity 0.5s ease-in-out;
}
#menu ul.menus li {
display: block;
width: 100%;
}
#menu ul.menus a {
color: #77778b;
}
#menu li:hover ul.menus {
opacity: 1;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
<nav id='menu'>
<input type='checkbox' />
<label><i class='icon-reorder' /><span>Menu</span>
</label>
<ul>
<li><a href='/'>Home</a>
</li>
<li><a class='dropdown' href='#'>Menu1</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
</ul>
</li>
<li><a class='dropdown' href='#'>Menu2</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
<li><a href='#'>Submenu3</a>
</li>
</ul>
</li>
</ul>
</nav>