How to set focus in li elements using tabindex - css

I'm using ul to show the navigation bar in my homepage. I want to show a different color to any navigation bar which is focused. How can I do that on element li
Here is my code
<div id="menubar">
<ul id="menu" >
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li tabindex="1"><a href="#" >Home</a></li>
<li tabindex="1" ><a href="#" >Projects</a></li>
<li tabindex="1" ><a href="#" >Publications</a></li>
<li tabindex="1" ><a href="#" >News</a></li>
<li tabindex="1"><a href="#" >Members</a></li>
<li tabindex="1"><a href="#" >Contact Us</a></li>
</ul>
</div>
and style I'm trying is
background: #00C6F0;
But due to some unknown reason I"m unable to do so . Please help

You're overcomplicating your code. There is a <nav> tag in HTML5.
You do not need all those "wrappers" and tabindex is unnecessary too.
a {display:block}
a:focus {background: #00C6F0}
a:nth-child(2):focus {background: #f90}
a:nth-child(3):focus {background: #f99}
a:nth-child(4):focus {background: #f41}
a:nth-child(5):focus {background: #cfa}
a:nth-child(6):focus {background: #afc}
<nav>
Home
Projects
Publications
News
Members
Contact Us
</nav>

If you would like to have different background colours when you tab through the menu vs simply hovering over it, you can use this example here:
The important parts are the
.navbar ul li a[tabindex="1"]:focus {
background: #00C6F0;
}
as these select the individual tab-indexes while they are in focus. If you want all the menu items to have the same colour while tabbing though it, you can remove the tab-indexes 2-7 etc, and just have the selector as .navbar ul li a[tabindex]:focus
.navbar ul li a[tabindex="1"]:focus {
background: #00C6F0;
}
.navbar ul li a[tabindex="2"]:focus {
background: #feca57;
}
.navbar ul li a[tabindex="3"]:focus {
background: #ff6b6b;
}
.navbar ul li a[tabindex="4"]:focus {
background: #48dbfb;
}
.navbar ul li a[tabindex="5"]:focus {
background: #5f27cd;
}
.navbar ul li a[tabindex="6"]:focus {
background: #8395a7;
}
.navbar ul li a[tabindex="7"]:focus {
background: #10ac84;
}
* {
box-sizing: border-box;
}
.navbar {
text-align: center;
margin: 20px 0;
position: relative;
background: #E51573;
}
.navbar ul {
padding: 0;
}
.navbar ul li {
display: inline-block;
line-height: 30px;
font-size: 14px;
text-transform: uppercase;
padding: 0;
-webkit-transition: ease-in-out .15s;
-o-transition: ease-in-out .15s;
transition: ease-in-out .15s;
}
.navbar ul li:hover {
background: #EE2C86;
color: white;
}
.navbar ul li a {
display: inline-block;
color: white;
padding: 5px 9px;
text-decoration: none;
-webkit-transition: ease-in-out .15s;
-o-transition: ease-in-out .15s;
transition: ease-in-out .15s;
width: 100%;
font-weight: bold;
}
.navbar ul li a:hover {
background: #EE2C86;
color: white;
}
.navbar ul li:hover a {
color: white;
}
.navbar ul li a.active {
color: #FFF;
background: #71261F;
}
.navbar ul li:hover ul {
display: block;
}
.navbar ul li ul {
display: none;
position: absolute;
background: #9D9FA2;
padding: 0;
z-index: 200;
}
.navbar ul li ul li {
border: none;
padding: 0;
display: block;
}
.navbar ul li ul li:hover {
border-radius: 0;
}
.navbar ul li ul li:first-child {
border: none;
}
.navbar ul li ul li a {
margin: 5px 0;
color: white;
}
.navbar ul li ul li a:hover {
border-radius: 0;
}
.navbar ul li ul ul {
position: absolute;
left: 200px;
top: 0;
display: none;
}
.navbar ul li li:hover ul {
display: block!important;
}
<div class="navbar">
<ul>
<li><a tabindex="1" href="#">Home</a></li>
<li><a tabindex="2" href="#">Menu2</a>
<ul>
<li>Sub-Menu 1</li>
<li>Sub-Menu2
</ul>
</li>
<li><a tabindex="3" href="#">Menu3</a></li>
<li><a tabindex="4" href="#">Menu4</a></li>
<li><a tabindex="5" href="#">Menu5</a></li>
<li><a tabindex="6" href="#">Menu6</a></li>
<li><a tabindex="7" href="#">Menu7</a></li>
</ul>
</div>

You can try using the CSS :focus selector.
ul#menu li:focus{
background: #00C6F0;
}

This might help you
li a:focus {
background: #00C6F0;
}

Related

How to manage click event and hover effect on touch device?

The touch device works both :hover or href="/products.php" on the first tap. Is there any CSS solution for managing menu nav link and its submenu ?
html, body {
font-family: Arial, Helvetica, sans-serif;
background-color: tomato;
height: 100vh;
}
.navigation {
width: 300px;
}
.mainmenu, .submenu {
list-style: none;
padding: 0;
margin: 0;
}
.mainmenu a {
display: block;
background-color: #CCC;
text-decoration: none;
padding: 10px;
color: #000;
}
.mainmenu a:focus, .mainmenu a:hover {
background-color: #C5C5C5;
}
.mainmenu li:focus .submenu, .mainmenu li:hover .submenu {
display: block;
max-height: 200px;
}
.submenu a {
background-color: #999;
}
.submenu a:focus, .submenu a:hover {
background-color: #666;
}
.submenu {
overflow: hidden;
max-height: 0;
-webkit-transition: all 0.5s ease-out;
}
<nav class="navigation">
<ul class="mainmenu">
<li>Home</li>
<li>About</li>
<li>Products
<ul class="submenu">
<li>Tops</li>
<li>Bottoms</li>
<li>Footwear</li>
</ul>
</li>
<li>Contact us</li>
</ul>
</nav>
Demo: https://codepen.io/athimannil/pen/LOrPVw

css menu sub-items below can't be reached on hover

I have a css menu in Wordpress with 3 levels, and the problem is that the level below the menu level2 with level 3 opened can't be reached on hover (the complete 2nd level menu closes).
Any help please?
My code is:
<div class="nav">
<ul>
<li>Main-menu
<ul class="sub-menu">
<li>Sub-item</li>
<li>Second-submenu
<ul class="sub-menu">
<li>XY</li>
<li>XY</li>
<li>XY</li>
</ul>
</li>
<li>Last-sub-menu
<ul class="sub-menu">
<li>XY</li>
<li>XY</li>
<li>XY</li>
</ul>
</li>
</ul>
</li>
<li>Second-item</li>
</ul>
</div>
And the css:
.nav { font-size:1.4em; }
.nav ul { padding:0; margin: 0; background: #0A488E; }
.nav ul li { display:inline-block; margin-right:5%; }
.nav ul li a { color:white; }
.nav li ul.sub-menu {
display: none;
position: absolute;
padding: 7px 21px;
padding-left: 21px;
text-align: left;
margin-left: -21px;
background: #0A488E;
font-size: 0.9em;
z-index: 100;
}
.nav li:hover > ul.sub-menu, .nav li ul.sub-menu li:hover > ul.sub-menu {
display: block;
}
.nav li ul.sub-menu li {
display:block;
margin: 9px 0;
}
.nav li ul.sub-menu li ul.sub-menu {
position: relative;
padding: 0 5%;
}
The fiddle is here: https://jsfiddle.net/f7ny3s3r/2/
(The Last sub-menu is the problematic one)
Thank you for the help!
Based on what you are asking for, I fixed it here -
https://jsfiddle.net/f7ny3s3r/7/
Instead of display: none into display block. You can keep it block and use height and opacity with a transition. This will solve your problem, and add animation.
So instead of :
.nav li ul.sub-menu {
display: none;
Use:
.nav li ul.sub-menu {
transition: 0.5s all;
display: block;
height: 0;
opacity:0;
And
.nav li:hover > ul.sub-menu, .nav li ul.sub-menu li:hover > ul.sub-menu {
height: auto;
opacity:1;
}
You can change the transition to whatever you want, including a delay (or no transition at all).

Highlight Menu on Current Page

I don't understand why the menu won't highlight on current page? on .menu ul li a:hover, .active {color: #788d35} If I put "background-color" instead of "color"it will work. How do I highlight just the text if current page is active?
HTML:
<section class="menu">
<ul>
<li><a class="active" href="home.html"> HOME </a></li>
<li> PORTFOLIO
<ul>
<li> illustrations </li>
<li> portraits </li>
<li> environments </li>
<li> sketches </li>
</ul>
</li>
<li> STORE
<ul>
<li> society6 </li>
<li> redbubble </li>
</ul>
</li>
<li> CONTACT </li>
<li> ABOUT </li>
</ul>
</section>
CSS:
.menu {
height: 29px;
width: 100%;
/*background:orange;*/
}
.menu ul {
width: auto;
list-style-type: none;
font-family: "calibri", "arial";
}
.menu ul li {
position: relative;
display: inline;
float: left;
width: auto;
border-right: 2px solid purple;
margin-left: 10px;
line-height: 12px;
}
.menu ul li:last-child {
border: none;
}
/*removes border after 'about'*/
.menu ul li a {
display: block;
padding: 3px;
color: #854288;
text-decoration: none;
font-size: 20px;
font-weight: strong;
padding-right: 25px;
}
.menu ul li a:hover,
.active {
color: #788d35
/* <-- if I put "background-color" instead of "color" it will work. How do I highlight just the text if current page is active? */
}
.menu ul li ul {
display: none;
}
.menu ul li:hover > ul {
display: block;
position: absolute;
top: 22px;
float: left;
text-align: left;
z-index: 1000;
background-color: white;
}
.menu ul li ul li {
position: relative;
max-width: 140px;
min-width: 140px;
width: 100%;
border: none;
margin-left: -40px;
}
.menu ul li ul li a {
padding: 4px;
margin-left: 1px;
}
Your .active selector is being overruled by the .menu ul li a selector. So you can either change .active selector to .menu ul li a.active or change the color property color: #788d35; to color: #788d35 !important;
See: http://jsfiddle.net/5mpjj/
For more information: http://hungred.com/useful-information/css-priority-order-tips-tricks/

CSS Dropdown Menu on iPad

I have a CSS drop-down menu that works perfectly on mouse hover but that only partly works on iPad & iPhone. Tapping the drop-down works the first time but fails on subsequent taps until the home link is tapped, which 'resets' the menu. Seems weird to me but maybe someone will recognize this behavior. The menu can be viewed at http://raleighfutbol4all.com/
<div id="menu">
<ul>
<li><a class="link" href="http://raleighfutbol4all.com">Home</a></li>
<li><a class="sm_parent" onClick="return true">Program</a>
<ul>
<li><a class="link" href="/program.php">Methods</a></li>
<li><a class="link" href="/schedule.php">Schedule</a></li>
<li><a class="link" href="locations.php">Locations</a></li>
<li><a class="link" href="reading.php">Reading</a></li>
</ul>
</li>
<li><a class="link" class="titlebar" href="contact.php">Contact</a></li>
<li><a class="sm_parent" onClick="return true">About</a>
<ul>
<li><a class="link" href="/about.php">The Coaches</a></li>
<li><a class="link" href="/mission.php">Mission Statement</a></li>
<li><a class="link" href="/testimonials.php">Testimonials</a></li>
<li><a class="link" href="/gallery.php">Gallery</a></li>
</ul>
</li>
</ul>
</div> <!-- menu -->
CSS:
#menu
{
display: block;
position: absolute;
white-space:nowrap;
z-index: 3;
}
#menu a.link:hover
{
color: #ffffff;
border: 1px solid #FFE135;
border-radius: 6px;
margin: 0px; /* needed to offset 1px border */
}
#menu a.sm_parent:hover /* sm_parent = submenu parent, has no link */
{
cursor: default;
}
#menu > ul /* top level ul only */
{
padding-left: 15px;
padding-right: 15px;
margin: 0;
}
#menu ul /* all ul's*/
{
display: inline-table;
position: relative;
list-style: none;
z-index: 3;
}
#menu ul li
{
float: left;
text-align: left;
}
#menu ul li a
{
display: block;
padding: 4px 20px;
color: #FFE135;
text-decoration: none;
margin: 1px; /* hover will replace this with 1px border, prevents shifting */
z-index: 3;
}
#menu ul li a:link { color: #FFE135; }
#menu ul li a:visited { color: #FFE135; }
#menu ul li a:active { color: #FFE135;}
#menu ul li:hover > ul
{
visibility: visible;
opacity: 1.0;
}
#menu ul li ul
{
visibility: hidden;
opacity: 0.0;
-webkit-transition: all 0.4s ease-in-out 0.0s;
-moz-transition: all 0.4s ease-in-out 0.0s;
-ms-transition: all 0.4s ease-in-out 0.0s;
-o-transition: all 0.4s ease-in-out 0.0s;
transition: all 0.4s ease-in-out 0.0s;
}
#menu ul ul
{
display: block;
background: black;
border-radius: 8px;
padding: 2px 8px;
position: absolute;
z-index: 3;
/*
top: 100% works with Chrrome, etc but not with IE, (of course) but not with
margin and or border values adding margin & border values between top menu (ul)
and submenu (ul) will require more % value deduction
*/
top: 88%;
}
#menu ul ul li
{
display: block;
position: relative;
float: none;
}
#menu ul ul > li
{
/* submenus with slightly smaller font */
font-size: 90%;
}
#menu ul ul li a
{
padding: 4px 10px;
}
#menu ul ul ul
{
/* 2nd generation submenu open to right of 1st generation dropdown submenus */
position: absolute; left: 100%; top:0;
}
#menu ul ul ul > li
{
/* This prevents 2nd generation > submenus from having ever diminishing font size*/
font-size: 100%;
}
First i noticed website is not responsive. most of the time drop downs not good for the Mobile equipment like IPod,Ipad and Android mobile. but if you can make responsive website with menu. you can get very good touch friendly user experience.
Here example mobile navigation : http://osvaldas.info/drop-down-navigation-responsive-and-touch-friendly

css make drop down list open to up (opposite direction) and not to down

I have created a dropdown list using css. The drop down list works perfect, opening the menu to down. How can I modified in order to open the menu to up (opposite direction)? make something like drop up list.
This is my html code.
<div id="menu">
<ul>
<li> <?php echo $user_data['name']; ?>
<ul>
<li>Logout</li>
<li>Change Password<li>
<li>Settings<li>
</ul>
</li>
<li> Profile
<ul>
<li>Edit Profile</li>
<li>View Profile</li>
</ul>
</li>
<li> Home </li>
</ul>
</div>
and this is my css.
#menu ul{
padding:0px;
margin:0px;
list-style:none;
}
#menu ul li {
float:left;
}
#menu ul li a:hover{
background:#fff;
color:#333;
}
#menu ul li ul{
position:absolute;
height:0px;
overflow:hidden;
}
#menu ul li ul li{
float:none;
}
#menu ul li:hover ul{
overflow:visible;
}
#menu ul li:hover ul li a{
padding:10px;
}
#menu ul li ul li a{
-webkit-transition:0.3s;
-moz-transition:0.3s;
padding: 0px 10px;
}
http://codepen.io/bjornmeansbear/pen/MwGYZL
The following CSS should help you — it incorporates the "drop-up" and also cleans up a few other things...
#menu {
margin-top: 100px;
}
#menu ul {
padding: 0px;
margin: 0px;
list-style: none;
}
#menu > ul > li {
float: left;
margin: 10px;
position: relative;
}
#menu a { display: block;}
#menu a:hover {
background: #fff;
color: #333;
}
#menu ul li ul {
position: absolute;
height: 0px;
width: 250%;
overflow: hidden;
}
#menu ul li:hover ul {
height: initial;
bottom: 100%;
overflow: visible;
background: lightgray;
}
#menu li li a {
padding: 5px 10px;
}
#menu a {
-webkit-transition: 0.3s;
-moz-transition: 0.3s;
padding: 0px 10px;
}
Basically, if you position the <li> that houses the child <ul> relatively, you can use the absolute position of the <ul> to position it anywhere based directly on its the parent... does that make sense?

Resources