redface
redface

Reputation: 325

How to set focus in li elements using tabindex

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

Upvotes: 1

Views: 9369

Answers (4)

Kosh
Kosh

Reputation: 18378

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>
  <a href="#">Home</a>
  <a href="#">Projects</a>
  <a href="#">Publications</a>
  <a href="#">News</a>
  <a href="#">Members</a>
  <a href="#">Contact Us</a>
</nav>

Upvotes: 3

Austen Holland
Austen Holland

Reputation: 1978

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><a href="#">Sub-Menu 1</a></li>
        <li><a href="#">Sub-Menu2</a>
      </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>

Upvotes: 1

Maram AlSaegh
Maram AlSaegh

Reputation: 69

This might help you

li a:focus {
  background: #00C6F0;
}

Upvotes: 0

jleggio
jleggio

Reputation: 1286

You can try using the CSS :focus selector.

ul#menu li:focus{
  background: #00C6F0;
}

Upvotes: 0

Related Questions