Accessible HTML5/CSS3 dropdown menu without Javascript - html

I have created a dropdown menu using pure css, which works fine with a mouse, but I would like to make it more accessible via the keyboard. I don't want to use Javascript to do this.
Here is a snippet of the html code, and a jsfiddle:
<nav>
<ul>
<li>Home</li>
<li>About us »
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
<li>Option 5</li>
</ul>
</li>
</ul>
</nav>
And here is the CSS:
BODY {
font-family : Arial, Helvetica, Swiss, Geneva, Sans-serif;
font-size: 0.8em;
width: 795px;
margin: 0px auto;
padding: 0px;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul, nav ul li:focus > ul, nav ul li:active > ul {
display: block;
}
nav ul {
background: #696969;
padding: 0px 0px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: "";
clear: both;
display: block;
}
nav ul li {
float: left;
border-bottom: 1px solid #FFF;
}
nav ul li:hover, nav ul li:focus, nav ul li:active {
background: #F8A509;
}
nav ul li:hover a, nav ul li:focus a, nav ul li:active a {
color: #fff;
}
nav ul li a {
display: block;
padding: 4px 14px;
color: #fff;
text-decoration: none;
}
nav ul ul {
background: #BDD575;
padding: 0px;
position: absolute;
top: 100%;
-moz-box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.20);
-webkit-box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.20);
box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.20);
}
nav ul ul li {
float: none;
border-bottom: 1px solid #FFF;
position: relative;
}
nav ul ul li a {
padding: 4px 14px;
color: #fff;
white-space: nowrap;
border-right: 1px solid #FFF;
}
nav ul ul li a:hover, nav ul ul li a:focus, nav ul ul li a:active {
background: #9ED112;
color: #fff;
}
nav ul ul ul {
position: absolute; left: 100%; top:0;
}
Any ideas where I am going wrong?
TIA for any helpful advice.

The W3C's draft specification for Accessible Rich Internet Applications (WAI-ARIA) makes it possible to supplement HTML with attributes that explicitly communicate the roles, states, and properties of the various parts that make up a menu system. As assistive technology (AT) users interact with the menu, ARIA passes updated information to their AT, and their AT then communicates that information in meaningful ways.
See more about on Accessible Dropdown Menus
Working Demo
Markup
<nav role="navigation" aria-label="Main menu">
<ul id="nav" role="menubar" aria-hidden="false">
<li role="menuitem" aria-haspopup="false">Home</li>
<li tabindex="0" aria-haspopup="true">About us »
<ul data-test="true" role="menu" aria-hidden="true">
<li role="menuitem">Option 1</li>
<li role="menuitem">Option 2</li>
<li role="menuitem">Option 3</li>
<li role="menuitem">Option 4</li>
<li role="menuitem">Option 5</li>
</ul>
</li>
</ul>
</nav>

Related

Change priority for drawing border

I want draw <div> border over <li> border.
Now it see like that:
But i want draw that:
All my code on JsFiddle:
#mainmenu ul ul {
display: none;
}
#mainmenu ul {
list-style-type: none;
margin: 0px;
padding: 0px;
}
#mainmenu ul li {
list-style-type: none;
text-align: center;
width: 120pt;
height: 20pt;
padding: 10pt 0pt 10pt 0pt;
border: 2px gray solid;
}
#mainmenu ul li:hover {
background-color: gray;
}
#mainmenu ul li:hover ul {
position: absolute;
top: 10px;
left: 130pt;
display: block;
}
#mainmenu h1 {
font-size: 14pt;
margin: 0px;
}
#mainmenu {
display: inline-block;
border: 2px red solid;
}
<div id="mainmenu">
<ul>
<li>
<h1>Семинар 1</h1>
<ul>
<li>Задача 1</li>
<li>Задача 2</li>
<li>Задача 3</li>
</ul>
</li>
<li>
<h1>Семинар 2</h1>
<ul>
<li>Задача 1</li>
<li>Задача 2</li>
<li>Задача 3</li>
</ul>
</li>
</ul>
</div>
Big thanks for your response!
Based on your screenshot, I'm guessing you're trying to change the border-color of the left-most boxes.
Rather than "covering" up the gray border with a red one, I'd recommend specifically targeting those top-level <li>s with CSS, and setting the border of those elements to red. You can use the CSS direct descendant selector to do that:
#mainmenu > ul > li {
border: 2px solid red;
}
Full Example Below (click "run code snippet"):
#mainmenu ul ul {
display: none;
}
#mainmenu ul {
list-style-type: none;
margin: 0px;
padding: 0px;
}
#mainmenu ul li {
list-style-type: none;
text-align: center;
width: 120pt;
height: 20pt;
padding: 10pt 0pt 10pt 0pt;
border: 2px gray solid;
}
#mainmenu > ul > li {
border: 2px solid red;
}
#mainmenu ul li:hover {
background-color: gray;
}
#mainmenu ul li:hover ul{
position: absolute;
top: 10px;
left: 130pt;
display: block;
}
#mainmenu h1 {
font-size: 14pt;
margin: 0px;
}
#mainmenu {
display: inline-block;
}
<div id="mainmenu">
<ul>
<li><h1>Семинар 1</h1>
<ul>
<li>Задача 1</li>
<li>Задача 2</li>
<li>Задача 3</li>
</ul>
</li>
<li><h1>Семинар 2</h1>
<ul>
<li>Задача 1</li>
<li>Задача 2</li>
<li>Задача 3</li>
</ul>
</li>
</ul>
</div>
Your Image:
From your image (that I have posted above), I believe you are looking for something like this:
#mainmenu ul ul {
display: none;
}
#mainmenu ul {
list-style-type: none;
margin: 0px;
padding: 0px;
}
#mainmenu ul li {
list-style-type: none;
text-align: center;
width: 120pt;
height: 20pt;
padding: 10pt 0pt 10pt 0pt;
}
ul li ul li {
border: 2px gray solid;
margin-left:6px;
}
#mainmenu ul li:hover {
background-color: gray;
}
#mainmenu ul li:hover ul {
position: absolute;
top: 10px;
left: 130pt;
display: block;
}
#mainmenu h1 {
font-size: 14pt;
margin: 0px;
z-index:2;
}
#mainmenu {
display: inline-block;
border: 6px red solid;
}
<div id="mainmenu">
<ul>
<li>
<h1>Семинар 1</h1>
<ul>
<li>Задача 1</li>
<li>Задача 2</li>
<li>Задача 3</li>
</ul>
</li>
<li>
<h1>Семинар 2</h1>
<ul>
<li>Задача 1</li>
<li>Задача 2</li>
<li>Задача 3</li>
</ul>
</li>
</ul>
</div>

How to show all siblings on :hover in pure CSS

Need to select all sibling <li> elements on hover. Tried accepted answer here but it is not working. JSFiddle here
.menu {
margin: 0;
padding: 0;
list-style: none;
overflow: hidden;;
background-color: #777;
}
.menu li {
float: none;
display: none;
}
.menu li a {
display: block;
padding: 10px 20px 10px 20px;
text-decoration: none;
color: #bbb;
}
.menu li a:hover {
color: #fff;
}
.menu .btn {
display: block;
cursor: pointer;
}
.menu li:hover ~ .menu li{/*ON THIS HOVER NOT SHOWING ALL SIBLIING LIs*/
display: block !important;
}
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
<ul class="menu">
<li class="btn"><a>☰</a></li>
<li>Home</li>
<li>Portfolio</li>
<li>Contact
<ul class="sub-menu">
<li>Sub Menu</li>
<li>Sub Menu</li>
</ul>
</li>
</ul>
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
Your problem is the selector:
.menu li:hover ~ .menu li
A hidden element can't be hovered-over, which means that li:hover is never going to match an element. Also, the general-sibling combinator is trying to find (subsequent) siblings that are <li> elements descending from sibling .menu elements. Which matches no elements in the page.
Converting that to the following selector:
.menu:hover li ~ li
.menu {
margin: 0;
padding: 0;
list-style: none;
overflow: hidden;
;
background-color: #777;
}
.menu li {
float: none;
display: none;
}
.menu li a {
display: block;
padding: 10px 20px 10px 20px;
text-decoration: none;
color: #bbb;
}
.menu li a:hover {
color: #fff;
}
.menu .btn {
display: block;
cursor: pointer;
}
.menu:hover li ~ li {
display: block;
}
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
<ul class="menu">
<li class="btn"><a>☰</a>
</li>
<li>Home
</li>
<li>Portfolio
</li>
<li>Contact
<ul class="sub-menu">
<li>Sub Menu
</li>
<li>Sub Menu
</li>
</ul>
</li>
</ul>
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
works; that said it will - because of the general sibling combinator - match only those <li> elements with a preceding <li> sibling, which means it will, and can, never show the first <li>.
So, instead, I'd suggest using:
.menu:hover li
.menu {
margin: 0;
padding: 0;
list-style: none;
overflow: hidden;
;
background-color: #777;
}
.menu li {
float: none;
display: none;
}
.menu li a {
display: block;
padding: 10px 20px 10px 20px;
text-decoration: none;
color: #bbb;
}
.menu li a:hover {
color: #fff;
}
.menu .btn {
display: block;
cursor: pointer;
}
.menu:hover li {
display: block;
}
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
<ul class="menu">
<li class="btn"><a>☰</a>
</li>
<li>Home
</li>
<li>Portfolio
</li>
<li>Contact
<ul class="sub-menu">
<li>Sub Menu
</li>
<li>Sub Menu
</li>
</ul>
</li>
</ul>
<!--NEED SOLUTION WITHOUT ALTERING HTML -->

CSS dropdown menu vertical

I'm trying to make a vertical dropdown menu but doesn't seem to work. It currently doesn't display any text, just a bar going across the top of the page. It is being pushed to be by by 115px for due to requirements. Here's the HTML:
<div id="wrapper">
<h1>Flags </h1>
<nav>
<ul>
<li>Introduction</li>
<li>History</li>
<li>National Flags</li>
<li>International Maritime Signal Flags
<ul>
<li>Maritime Signals: Letters</li>
<li>Maritime Signals: Numbers</li>
</ul>
</li>
</ul>
</nav>
Here is the CSS:
nav {
height:30px;
}
nav ul {
background-color: #5d2c2c;
padding: 0;
list-style: none;
position: relative;
bottom: 115px;
display: block;
}
nav ul:after {
content: ""; clear: both; display: block;
}
nav ul li {
float: right;
bottom: 115px;
position: relative;
}
nav ul li a {
display: block; padding: 5px 5px;
color: #FFF; text-decoration: none;
text-align:right;
}
nav ul ul {
background: #5d2c2c; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
float: none;
border-top: 1px solid #000;
border-bottom: 1px solid #575f6a;
position: relative;
}
The CSS needed a little work on
Try this FIDDLE it'll work
This was missing:
nav ul li:nth-child(4) ul { display:none; }
nav ul li:nth-child(4):hover ul { display:block; color:red; }
and bottom was removed from this one
nav ul li {
float: left;
position: relative;
}
I found this link the other day, it's an step by step guide with full examples, check it out: http://www.adam-bray.com/blog/91/Easy+HTML+5+%26+CSS+3+Navigation+Menu/
check this fiddle , a similar implementation
<nav>
<ul>
<li class="header">
People
<ul class="content">
<li>Sub 1</li>
<li>Sub 2</li>
</ul>
</li>
<li class="header">
Animals
<ul class="content">
<li>Sub 1</li>
<li>Sub 2</li>
</ul>
</li>
</ul>
</nav>
nav > ul{}
nav > ul > li{float:left;margin:0 5px;background:#cc3333;}
.header li a{background:#eee;color:#cc3333;}
.header li a:hover{color:#fff;background:#cc3333;}
ul{padding:0;}
li{list-style-type:none;}
a{color:#fff;text-decoration:none;padding:5px;display:block;text-align:center;}
.content{
display:none;
}
.header:hover > .content{display:block;}

How do i get my drop down menu items the same size as the nav bar parent?

I am trying to make a drop down menu using only CSS, but i'm having a hard time getting the dropdown menu the same size (width and height) as it's parent:
Working fiddle: HERE
The <nav> section from my HTML:
<nav>
<ul>
<li>Home</li>
<li>Menu With Menus
<ul>
<li>opt 1</li>
<li>opt 2</li>
<li>opt 3</li>
</ul>
</li>
<li>Whatnot</li>
</ul>
</nav>
The CSS:
nav ul {
position: relative;
display: inline-table;
list-style: none;
}
nav ul li {
float:left;
list-style-type: none;
}
nav ul li a {
background-color: #dae8ec;
color: rgb(233,78,31);
display: block;
font-weight: bold;
margin: 0 auto;
padding: 9px 18px 9px;
text-decoration: none;
border: 1px solid black;
border-radius: 2px;
}
nav ul ul {
display: none;
position: absolute;
top: 100%;
}
nav ul li:hover > ul {
display: block;
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 7px 30px;
color: rgb(233,78,31);
}
nav ul li a:hover {
background: rgb(138, 92, 132);
color:#dae8ec;
}
Any suggestions? Thanks.
Js Fiddle Demo
<nav>
<ul>
<li>Home</li>
<li>Menu Withs
<ul>
<li>opt 1</li>
<li>opt 2</li>
<li>opt 3</li>
</ul>
</li>
<li>Whatnot</li>
</ul>
</nav>
Cheers !!

CSS dropdown menu vertically

I have this site here http://surfthecurve.ca/ and I have a navigation for each nav item there is a drop down menu, the menu works fine, I just cant seem to get it to go vertically.
Here is the CSS for the navigation
.navigation{
width:100%;
background-color:#353636;
font-size:18px;
float:left;
}
.navigation ul {
list-style-type: none;
margin: 0 auto;
width:825px;
}
.navigation li {
float: left;
}
.navigation ul a {
color: #ffffff;
display: block;
padding: 0 105px 0 0;
text-decoration: none;
width:100%;
text-align:center;
text-transform:uppercase;
}
and the CSS for the drop-down
.submenu {
display: none;
}
.submenu li a {
display: block;
text-decoration: none;
color: #ffffff;
padding: 5px 15px 5px 15px;
margin-left: 1px;
white-space: nowrap;
}
.navigation li:hover .submenu {
display: block;
position: absolute;
}
.navigation li:hover .submenu li {
float: left;
font-size: 13px;
}
ul li a:hover {
background: #353636;
}
li:hover a {
background: #353636;
}
li:hover li a:hover {
background: #353636;
}
.navigation ul li ul li a{
padding-left:10px !important;
padding-right:10px !important;
padding-top:0px !important;
padding-bottom:0px !important;
}
and here is the HTML
<div class="navigation">
<ul>
<li>tutoring
<ul class="submenu">
<li>Our Approach</li>
<li>Pricing</li>
</ul>
</li>
<li>the cause
<ul class="submenu">
<li>How It Works</li>
<li>How We Give</li>
<li>Why We Give</li>
</ul>
</li>
<li>company
<ul class="submenu">
<li>About Us</li>
<li>Let's Get In Touch</li>
</ul>
</li>
<li>get involved
<ul class="submenu">
<li>Students</li>
<li>Work For Us</li>
</ul>
</li>
</ul>
</div><!--navigation-->
How would I fix this for my menu goes vertically?
Thanks in advanced,
J
This should be easy enough to get it to display vertically:
.submenu li {
clear: both;
}
What you have to do now is style it, as the individual li elements are different sizes (the element shrink wraps to the size of the text).