Align ::before pseudo element with list item contents - html

I have a side navigation I'm building, and it needs to be fully keyboard accessible as well as have the links themselves nested in list items. Everything else works fine, but I've been asked to produce a hover animation which is an animated pseudo element.
I've got most of that done, being the structure of the nav and the animation. However, the animated ::before element always seems to show up at the top of the list item, and will push the anchor down by whatever height I've set. I've tried both height:inherit and 100% for the ::before content, but that doesn't work.
The ideal behavior is that the content would animate in-line with the anchor from the bottom to the top, and simply occupy the height (100%) of the li element with my specified width.
Note: I can't use list-style:none because a screen reader will not identify how many items are in the list.
EDIT: took out the button element. Just focusing on the menu.
Here's a CodePen: https://codepen.io/anon/pen/EZbLMY
Feel free to fork it.
HTML:
<div id="mySidenav" class="sidenav" aria-label="Menu" aria-hidden="true">
<nav>
<ul role="navigation">
<li id="about">About</li>
<li id="services">Services</li>
<li id="clients">Clients</li>
<li id="contact">Contact</li>
</ul>
</nav>
</div>
CSS:
/* The side navigation menu */
.sidenav {
height: 100%;
width: 250px;
position: absolute;
z-index: 2;
top: 0;
background-color: #fff;
overflow-x: hidden;
padding-top: 60px;
}
.showOverlay{
display: block !important;
}
/* The navigation menu links */
.sidenav a{
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #000;
display: block;
width: inherit;
}
/*Clean up space between elements*/
.sidenav nav ul{
padding: 0;
margin: 0;
}
.sidenav nav ul li::before{
content:"";
width: 4px;
height: 0px;
display: block;
background-color: #000000;
}
.sidenav nav ul li:hover::before{
height: 20px;
transition: 0.3s ease-out;
padding-left: 2px;
}

Change the position of the ::before element to absolute. Also, to line things up neatly, add a position:relative to the li elements.
/* The side navigation menu */
.sidenav {
height: 100%;
width: 250px;
position: absolute;
z-index: 2;
top: 0;
background-color: #fff;
overflow-x: hidden;
padding-top: 60px;
}
.showOverlay {
display: block !important;
}
/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #000;
display: block;
width: inherit;
}
/*Clean up space between elements*/
.sidenav nav ul {
padding: 0;
margin: 0;
}
/*Make li elements relative, so that the before elements are positioned correctly*/
.sidenav nav ul li {
position: relative;
}
.sidenav nav ul li::before {
content: "*";
width: 4px;
height: 0px;
display: block;
background-color: #000000;
/*Add absolute positioning*/
position: absolute;
top: 8px;
}
.sidenav nav ul li:hover::before {
height: 20px;
transition: 0.3s ease-out;
padding-left: 2px;
}
<div id="mySidenav" class="sidenav" aria-label="Menu" aria-hidden="true">
<nav>
<ul role="navigation">
<li id="about">About</li>
<li id="services">Services</li>
<li id="clients">Clients</li>
<li id="contact">Contact</li>
</ul>
</nav>
</div>

Related

How to fix this dropdown navbar

I am writing a navigation bar with a dropdown option.
I have 2 problems: I can't get the dropdown to position properly under the menu, + also when I try to move the mouse to click on the elements in the dropdown submenu it closes as soon as I move.
Can anyone suggest a fix?
Thanks
On codepen:
https://codepen.io/-royqooe/pen/GRxaVbm
/* CSS section for home */
.navtop {
position: relative;
background-color: #333333;
height: 50px;
width: 100%;
border: 0;
}
.navtop div {
display: flex;
margin: 0 auto;
height: 100%;
}
.navtop div h1,
.navtop div a {
display: inline-flex;
align-items: center;
}
.navtop div h1 {
flex: 1;
font-size: 24px;
padding: 0;
margin: 0;
margin-left: 2%;
color: #f5f8ff;
font-weight: normal;
}
.navtop div a {
padding: 0 12px;
text-decoration: none;
color: #c1c4c8;
font-weight: bold;
}
.navtop div a i {
padding: 2px 8px 0 0;
}
.navtop div a:hover {
color: #66ccff;
}
/* sequel for home and navbar */
nav.navtop {
font-family: monospace;
}
.navtop>.navbar>ul {
list-style: none;
margin: 0;
padding-left: 0;
}
.navtop li {
display: block;
float: left;
padding: 0.5rem 0;
position: relative;
text-decoration: none;
transition-duration: 0.3s;
}
.navtop ul li ul {
background: red;
visibility: hidden;
opacity: 0;
min-width: 5rem;
position: absolute;
transition: all 0.5s ease;
margin-top: 1rem;
left: 0;
display: none;
}
.navtop ul li:hover>ul,
.navtop ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}
.navtop ul li ul li {
clear: both;
width: 100%;
}
#media screen and (max-width: 760px) {
.topbar-text {
display: none;
}
}
<nav class="navlol navtop" role="navigation">
<div class="navbar">
<h1>Websitee Title</h1>
<ul>
<li><i class="fa-solid fa-file"></i><span class="topbar-text">Home</span></li>
<li>Two
<ul class="dropdownnn">
<li>Sub-1</li>
<li>Sub-2</li>
<li>Sub-3</li>
</ul>
</li>
<li class="navelements">Three</li>
<li><i class="fas fa-user-circle"></i><span class="topbar-text">Profile</span></li>
<li><i class="fas fa-sign-out-alt"></i><span class="topbar-text">Logout</span></li>
</ul>
</div>
</nav>
got an easy fix for you:
add this
.navtop li {
min-height:25px;
}
the issue is that that menu li was way shorter than the others to the sides due to the icons, even better maybe is instead adding this instead:
.navtop li {
height:100%
}
both worked for me
In the screenshot, the yellow color is the area that <li> element is covering. You can see there is a gap between <li> element and submenu. When you try to move the mouse to submenu, your cursor goes out of yellow area and the submenu hides.
The solution would be to position the submenu so it starts exactly after yellow area and make sure there is no gap. Apply margin-top of 0.5rem on submenu instead of 1rem.

Nav bar padding and margins not reacting as expected

There seems to be an issue with my dropdown class and li class formatting (specifically padding/margins) not interacting as expected. The two-page selections with dropdown menus are getting extra padding from somewhere when I add left margins in the li CSS. while trying to fix that issue I also discovered that the padding between the nav text (in li a) isn't acting right either. I'm not sure where to go from here as I'd like to add a logo to the left side of the nav but can't move the nav text around properly.
The StackOverflow built-in HTML CSS console runs it weird, so here's the CodePen link
/* Nav Bar and Logo*/
/* Logo */
.logo {
position: absolute;
margin: 20px 0px 0px 0px;
}
/* nav-bar color and placement*/
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #0d0d0d;
font-family: Verdana;
}
/* nav text side alighnment*/
li {
float: left;
margin-left: 100px;
}
/* nav text formating*/
li a {
display: block;
color: #98c94f;
text-align: center;
padding: 20px 20px;
text-decoration: none;
}
/* Hovering on nav */
li a:hover:not(.active),
.dropdown:hover {
background-color: #2a3b12;
transition: 0.5s;
}
/* Current page your in*/
.active {
background-color: #8bc33c;
color: black;
font-style: italic;
padding: 19px 39px;
font-size: 18px;
}
/* Far-side button*/
.booking {
background-color: #3231ab;
color: #f4f9ec;
}
/* drop-down text alighnment*/
.dropdown {
float: left;
overflow: hidden;
}
/* drop-down box format*/
.dropdown-content {
display: none;
position: absolute;
background-color: #d1e7b1;
min-width: 160px;
/* drop-down box shadow*/
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
/* drop box postion (directly under nav) */
top: 67.5px;
}
/* drop-down text format*/
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* drop-down hover color*/
.dropdown-content a:hover {
background-color: #bbdb8a;
transition: 0.3s;
}
/* actavates drop-down when hovering */
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<div class="logo">
<a herf="#"><img src="DJ_Turntable_LOGO_3.png"></a>
</div>
<li class="Home"><a class="active" href="Index.html">Home</a></li>
<div class="dropdown">
<li class="About">About</li>
<div class="dropdown-content">
My Story
</div>
</div>
<li class="Packages">Packages</li>
<div class="dropdown">
<li class="Events">Events</li>
<div class="dropdown-content">
Weddings
School Celebrations
Corporate Events
Special Occasions
</div>
</div>
<li class="Reviews">Reviews </li>
<li class="Gallery">Gallery </li>
<li class="Resourses">Resourses </li>
<li class="Contact">Contact </li>
<li style="float:right"><a class="booking" href="https:">Book Now!</a></li>
</ul>

CSS Vertical Menu with submenu callouts overlapping

Its been a while - but I found myself a new exiting hobby in web development, and I am hoping that you can help me overcome the following issue:
I am working on a personal website. Content etc. is all preliminary. My main focus is the vertical menu on the left hand side. While I am pretty happy with the appearance so far, there are still some questions:
1) I had to set the transition as ease out to >0s, so that I have enough time between the main-menu hover and the hover over the sub-menu. Because of this, if I hover between main-menu items, they will briefly overlap due to the phase out. Is there a way to work around this?
(the focus selector might be an option, but that requires to click the menu item, and I prefer the hover)
2) The sub-menu is aligned to the top of the li of the main-menu. Because of that, the last item's sub (Testing) exceeds the boundaries of the page, giving whitespace below the page. Is there a way to make the sub-menu shift up if it presses against the edge of the overall body?
I probably could give it a different tag and design it differently, am I am hoping there is a more general solution.
3) Minor issue: When switching from main-menu hover to sub-menu hover, the main-icon "blinks" very briefly. Is that normal behavior? Certainly isn't pretty.
4) Last but not least: As this is my first time doing a website, I would be more than grateful to get your general feedback on inefficiencies in the code, so I can be more lean in the future.
(see updated fiddle below - full code removed to save space.)
Thanks for your help!
EDIT
THanks for your answers - issue #1 has been solved using the approaches you outlined!
However, the last submenu still gives me a bit a headache.
If I use the "last-child" selector, and the code added below, it will not change anything. However, if I give the last "bubble" a different tag, using the exact same code, the bubble will align to the bottom of the last main-menu item, but the containing UL remains defiant.
As you can see from the original CSS, the last "relative" is the main-menu IL, so shouldn't the submenu and the containing UL both align to the bottom of their respective parent/grandparent?
using a dedicated, different tag for the last-child "bubble_last
Using the last-child selector
and the accompanying new code (updated fiddle with full code: Heres the Fiddle ...use large screen for the result to see the alignment issue):
.navigation ul li:last-child {
border: solid orange;
}
.navigation .bubble_last{
position: absolute;
margin-top: 0%;
padding: 0px;
left: 60px;
bottom: 0px;
width: 400px;
height: 200px;
border: solid red;
}
.navigation .bubble_last ul{
position: absolute;
left: 0;
bottom: : 0px;
margin-top: 0px;
padding: 0px;
width: 20vw;
height: 100px;
font-size: 15px;
font-size: 1.2vw;
font-weight: 400;
border: solid blue;
margin-left: 28%;
display: block;
}
.navigation .bubble_last ul li {
list-style: none;
background-color: none;
border-radius: 3px;
height: 2vw;
line-height: 1.2vw;
width: 20vw;
position: relative;
padding-top: 0px;
margin: 0;
padding: 0;
}
Instead of adding a transition delay on .bubble, you just need to make the .bubble class wide enough (and start behind the main li trigger with a left: 60px) so that the hover will be continuous:
.navigation .bubble {
position: absolute;
margin-top: 0%;
padding: 0px;
left: 60px;
top: 0px;
width: 200px;
height: 100%;
background-color: none;
}
Then just add a margin-left to the submenu:
.navigation ul li ul {
margin-left: 20px;
}
To fix your second issue, simply add the following CSS to make the last submenu positioned relative to the bottom of the nav item:
ul li ul.subm:last-child {
top: auto;
bottom:0px;
}
Updated Demo: JSFiddle
How about this? Adjust your navigation to use the nav element, set it to use flex box, adjust that so it fits the height of your page, and then adjust the transition effects so they transition in and out properly.
I hope this helps.
#import url("http://fonts.googleapis.com/css?family=Roboto");
* {
border: none;
margin: 0;
padding: 0;
}
html,
body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
font-family: Roboto Condensed;
box-sizing: border-box;
}
.landing {
display: table;
margin: auto;
width: 100%;
height: 100%;
background: url("https://imageshack.com/i/poXkyeIYj") 50% 50% no-repeat;
background-size: cover;
top: 0;
position: relative;
z-index: 1;
}
.landing .welcome {
margin: 10px;
width: 89%;
color: #FFF;
text-align: center;
display: table-cell;
vertical-align: middle;
position: absolute;
left: 10%;
top: 30%;
}
#mug {
background: url("https://imageshack.com/i/pmJaAuFkj") 50% 50% no-repeat;
background-size: cover;
border: solid 3px #FFF;
/*max-width: 20%;
max-height: 20%; */
width: 150px;
height: 150px;
margin: auto;
border-radius: 100%;
}
nav {
width: 50px;
height: 100%;
flex: 1;
display: flex;
justify-content: center;
flex-direction: column;
}
nav img {
max-width: 50px;
max-height: 50px;
}
.nav ul {
*zoom: 1;
list-style: none;
margin: 0;
padding: 0;
-ms-flex: 0 100px;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
justify-content: space-between;
margin-top: 0px;
padding: 0px;
width: 11vw;
height: auto;
font-size: 15px;
font-size: 1.2vw;
font-weight: 400;
border-color: none;
}
nav ul li {
background-color: rgba(222, 225, 229, 0.8);
border-radius: 3px;
width: 100%;
font-size: 15px;
font-size: 1.2vw;
font-weight: 900;
}
.nav ul:before,
.nav ul:after {
content: "";
display: block;
}
.nav ul:after {
clear: both;
}
.nav ul>li {
position: relative;
}
.nav a {
display: block;
padding: 10px 20px;
line-height: 1.2em;
color: #fff;
border-left: 1px solid #595959;
text-decoration: none;
color: #FFF;
}
.nav a:hover {
text-decoration: none;
background-color: rgba(242, 93, 38, 0.8);
}
.nav li ul {
background: #273754;
}
.nav li ul li {
width: 200px;
}
.nav li ul a {
border: none;
}
.nav li ul a:hover {
background: rgba(0, 0, 0, 0.2);
}
.nav li ul {
position: absolute;
left: 11vw;
top: 0;
z-index: 1;
visibility: hidden;
opacity: 0;
filter: alpha(opacity=0);
-webkit-transition: 200ms ease;
-moz-transition: 200ms ease;
-o-transition: 200ms ease;
transition: 200ms ease;
}
.nav ul>li:hover ul {
visibility: visible;
opacity: 1;
filter: alpha(opacity=100);
}
span.caption {
display: none;
}
<body>
<div class="landing">
<div class="welcome">
<div id="mug">
</div>
<h1>Welcome to my personal Website!</h1>
<h2>On this site you will find a information about the person, the profession and other stuff about me. Enjoy!</h2>
</div>
<!-- cleaned up the navigation, nested it within the rest of the page, and adjusted the css -->
<nav class="nav">
<ul>
<li>
<img src="https://imageshack.com/i/potj2pVwp"> <span class="caption">Home</span>
</li>
<li>
<img src="https://imageshack.com/i/pntQ9nVMp"><span class="caption">About Me</span>
<ul>
<li>What I do</li>
<li>Motorcycling</li>
<li>Music</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/po4WFq6Yp"><span class="caption">Professional</span>
<ul>
<li>Current Employment</li>
<li>Working Experience</li>
<li>Education</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/pmcfm7Kbp"> <span class="caption">Projects</span>
<ul>
<li>Subnav Item</li>
<li>Subnav Item</li>
<li>Subnav Item</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/pnM0Fmgrp"> <span class="caption">Misc</span>
<ul>
<li>Subnav Item</li>
<li>Subnav Item</li>
<li>Subnav Item</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/poqRnk6ap"> <span class="caption">Testing</span>
<ul>
<li>Subnav Item</li>
<li>Subnav Item</li>
<li>Subnav Item</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/pmc8tts9p"> <span class="caption">Contact</span>
</li>
</ul>
</nav>
</div>

CSS - Z-index is not working with positions relative and absolute

I've made dropdown menu using pure css3, but one property(z-index) is not working as I expected and that is a very big problem, because the dropdown list is dropping ABOVE the menu. Ideally it must be dropped UNDER the menu. I've tried to do something with that problem all day but unfortunately can't figure it out, so now asking for help...
I've made a different background-colors for problem items for good seeing what i'm trying to reach. The main aim is that sub-menu with red background must be under the blue background.
P.S. I've already tried to make this menu with jQuery slideDown/slideUp properties but they don't look like ideal slide effect(as in my example). They look more like stretching, and that's not what I want..
EXAMPLE ON JSFIDDLE
ul {
list-style-type: none;
}
.menu_wrapper {
position: relative;
z-index: 999;
/* IS NOT WORKING... O_o...*/
height: 70px;
width: 600px;
background-color: blue;
}
.menu li {
display: inline;
float: left;
display: table;
height: inherit;
margin: 3px;
margin-top: 10px;
}
.menu li a {
display: table-cell;
font-family: Verdana;
font-size: 16px;
color: gold;
text-align: center;
text-decoration: none;
padding-left: 10px;
padding-right: 10px;
vertical-align: middle;
background: #05487F;
transition: .2s ease-in-out;
}
.sub-menu {
position: absolute;
z-index: 1;
/* IS NOT WORKING... O_o...*/
margin-top: -200px;
margin-left: -132px;
padding: 15px;
padding-top: 20px;
background-color: red;
transition: all 1s ease-in-out;
}
.sub-menu li {
float: none;
}
.sub-menu li a {
padding: 5px 15px;
}
.menu li a:hover + .sub-menu {
margin-top: 40px;
}
.sub-menu:hover {
margin-top: 40px;
}
<nav class="menu_wrapper">
<ul class="menu">
<li>About
</li>
<li>News
</li>
<li>
PROBLEM HERE<br>(HOVER THIS)
<ul class="sub-menu">
<li>link 1
</li>
<li>link 2
</li>
<li>link 3
</li>
</ul>
</li>
<li>Something
</li>
<li>Contacts
</li>
</ul>
</nav>
The problem is that you're establishing a stacking context on .menu_wrapper when you set z-index: 999. When a stacking context is established, you cannot position a descendant element behind an ancestor.
Remove z-index: 999 from .menu_wrapper:
.menu_wrapper {
position: relative;
/* z-index: 999; << remove */
height: 70px;
width: 600px;
background-color: blue;
}
Then change the z-index on .sub-menu from 1 to a negative number such as -1:
Updated Example
.sub-menu {
position: absolute;
z-index: -1;
margin-top: -200px;
margin-left: -132px;
padding: 15px;
padding-top: 20px;
background-color: red;
transition: all 1s ease-in-out;
}
Here is the deal with z-index.
Z-index is relative to its parent (and ultimately relative to the Window object) in the normal DOM flow. However, Absolutely positioned objects are removed from the normal DOM flow and therefore, Z-Index is relative to itself - rather than the Window object.
In your case, you have not set position for your parent menu, so it will be automatically assigned position:static which CANNOT be z-indexed.
Adding the following to your parent menu should allow you to z-index the submenu to rest above it.
position:relative;
z-index:1;

When I resize my browser the headings, navigation bars overlaps. It doesn't fits with the browser

When I place the width of by web page as 100% it fits to the browser. But when I re-adjust the size of the browser the navigation bar I used overlaps and all the positions go wrong. I can't figure out the mistake yet. I will post the css and the html code below. Please someone help me to fix my web page.
HTML:
<div class="main">
<div class="header">
<div class="logo12">
<h2 style="position:absolute; left:20px; top:2px; color:#FFFFFF;">
<a>
<font face="verdana">PHIS</font>
<small>Public Health Information System-Sri Lanka</small>
</a>
</h2>
</div>
<div>
</div>
<div id="menu">
<ul>
<li>Home</li>
<li>
Services
<ul id="SubNav">
<li id="AccessFormPHI">Data Entry</li>
<li id="DataEntry">Edit Temporary Forms</li>
<li id="ViewData">View Data</li>
<li id="ViewAggregatedData">Data Aggregation</li>
</ul>
</li>
<li>Help</li>
<li>
Profile
<ul id="SubNav">
<li id="AccessForm">View and Maintain Profile</li>
<li id="ChangePassword">Change Password</li>
<li>Log Out</li>
</ul>
</li>
</ul>
</div>
</div>
CSS:
/* Height of the navigation bar became shorter */
.main {
width: 100%;
font: 13px Georgia, "verdana", "Times New Roman", Times, serif;
color: #FFFFFFF;
background-color: #F2F2F2;
height: 1000px;
left: 0px;
}
/* No difference was appeared*/
#cssmenu {
position: absolute;
top: 100px;
left: 0px;
}
/* Adjust the olor of the navigation bar */
.logo12 {
position: absolute;
padding-top: 10px;
/*padding-left: 60px; */
top: 0%;
height: 6%;
width: 100%;
left: 0px;
background: -moz-linear-gradient(top,#084B8A ,#0B173B);
}
/* Adjust the place where navigation */
#menu {
position: absolute;
left: 45%; /* change to 390px to 350px */
top: 20px;
display: inline;
}
#menu ul {
margin-left: auto;
margin-right: auto;
width :700px; /*change to 60% to 800px*/
}
/* upto now no difference */
#menu ul li {
display: inline;
float: left;
list-style: none;
position: relative;
height: 60px;
width: 130px;
padding-bottom: 20px;
font-size: 13px;
}
/* Adjust the width of the sub navigation bar */
#menu ul li ul li {
float: left;
list-style: none;
position: relative;
height: 30px;
width: 180px;
font-size: 12px;
}
/* Adjust the color of the sub navigation bar */
#menu li ul {
margin: 0px;
padding: 10px;
display: none;
position: absolute;
left: 0px;
z-index: 99;
top: 28px;
background: -moz-linear-gradient(top,#084B8A ,#0B173B); #726E6D; /*Menu Hover Color*/
border: 1;
}
#menu ul li:hover ul {
display: block;
width: auto;
}
/* Upto now no difference was visible */
#menu li li {
list-style: none;
}
/* Color of sub navigation bar letters became gray */
#menu li li a {
color: #FFFFFF;
text-decoration: none;
}
/* Letters hover color red in the sub navigation bar */
#menu li li a:hover {
color: #900;
text-decoration: none;
}
/* Upto now no difference */
li#main1 {
padding-top: 0px;
}
/* Main navigation bar disappeared */
#menu a {
padding-right: 25px;
text-decoration: none;
letter-spacing: -1px;
font-size: 1.2em;
color: #BDBDBD;
font: cambria;
}
/* Upto now no difference */
#menu ul li a:hover {
/* background: #D8D8D8; /*a tag hover color*/
padding: 5px; /*change to 12px to 5px*/
left: 0px;
}
You can use relative width for CSS,for example:
.main, .header{width:100%;}
Add white-space: nowrap to your navigation CSS. I know this is an old post, but seems to have no solution yet.