3

I can not get li:focus to show ul that is inside it.

Here is https://jsfiddle.net/cgxwzdy0/10/ with css display property

and the other one with visibility property https://jsfiddle.net/cgxwzdy0/12/.

<div id="menu">
  <ul>
    <li>
      <a href="javascript: void(0)">
      Lalalalal
      </a>
      <ul>
        <li>
          <a href="javascript: void(0)"><span>Link 1</span></a>
          <a href="javascript: void(0)"><span>Link 2</span></a>
          <a href="javascript: void(0)"><span>Link 3</span></a>
        </li>
      </ul>
    </li>
  </ul>
</div>

Css:

#menu ul li > ul {
  display: none;
}
#menu ul li li:focus > ul {
  display: block !important;
}

Or with visibility:

Css

#menu ul li > ul {
  visibility: hidden;
}
#menu ul li li:focus > ul {
  visibility: visible !important;
}

Please no javascript solutions. And if this not possible with css, please can you explain why?

When i turn on Developer tools and choose :focus menu is shown. Take a look at image.

enter image description here

pregmatch
  • 2,629
  • 6
  • 31
  • 68

2 Answers2

2

To be able to focus the element you need to consider tabindex attribute but there is another issue with the a element inside will block the focus effect.

Here is an example where you can focus when you click outside the text and not when clicking on the text:

#menu ul li > ul {
  display: none;
}
#menu ul > li {
  border:1px solid;
  padding:10px;
}
#menu ul li:focus > ul {
  display: block;
}
<div id="menu">
  <ul>
    <li tabindex="-1">
      <a href="javascript: void(0)">
      Lalalalal
      </a>
      <ul>
        <li>
          <a href="javascript: void(0)"><span>Link 1</span></a>
          <a href="javascript: void(0)"><span>Link 2</span></a>
          <a href="javascript: void(0)"><span>Link 3</span></a>
        </li>
      </ul>
    </li>
  </ul>
</div>

The tabindex global attribute indicates if its element can be focused, and if/where it participates in sequential keyboard navigation (usually with the Tab key, hence the name).ref

To make it work with a element you can consider :target

#menu ul li > ul {
  display: none;
}
#menu ul > li {
  border:1px solid;
  padding:10px;
}
#menu ul li #sub-menu:target {
  display: block;
}
<div id="menu">
  <ul>
    <li>
      <a href="#sub-menu">
      Lalalalal
      </a>
      <ul id="sub-menu">
        <li>
          <a href="javascript: void(0)"><span>Link 1</span></a>
          <a href="javascript: void(0)"><span>Link 2</span></a>
          <a href="javascript: void(0)"><span>Link 3</span></a>
        </li>
      </ul>
    </li>
  </ul>
</div>

The :target CSS pseudo-class represents a unique element (the target element) with an id matching the URL's fragment.ref

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Perfect! Works. – pregmatch Aug 27 '18 at 15:24
  • 1
    it does not work when the test is clicked which most internet users do @pregmatch – Raymond Nijland Aug 27 '18 at 15:25
  • @RaymondNijland that's what I also explained ;) – Temani Afif Aug 27 '18 at 15:26
  • "that's what I also explained" i know, i wanted it to make it more clear to @pregmatch – Raymond Nijland Aug 27 '18 at 15:27
  • i upvoted for finding these methodes but both methods are pretty hackie. First method does not work when clicking on the text which can be a large area when using a small menu item.. Second method also messes up the browser history and no easy method to fold back the menu.. For future readers read mine answer https://stackoverflow.com/a/52044043/2548147 which does not have these limit factors. – Raymond Nijland Aug 27 '18 at 17:39
0

Also a trick can be to use :active and :hover in combination with position absolute and relative.
So the menu does not fold back after clicking it but fold when the :hover is lost

#menu ul li {
  
  position: relative;  
}

#menu ul li ul {

   position: absolute; 
   top: 0px;
   left: 0px;
   padding: 20px;
   
   width: 100%;
   height: 100%;
  
   display: none;
   visibility: hidden;
}

#menu ul li:active > ul{
  
   display: block;
   visibility: visible;  
}

#menu ul li ul:hover {
 
   display: block;
   visibility: visible; 
}
<div id="menu">
  <ul>
    <li tabindex="-1">
      <a href="javascript: void(0)">
      Lalalalal
      </a>
      <ul>
        <li>
          <a href="javascript: void(0)"><span>Link 1</span></a>
          <a href="javascript: void(0)"><span>Link 2</span></a>
          <a href="javascript: void(0)"><span>Link 3</span></a>
        </li>
      </ul>
    </li>
  </ul>
</div>

see a demo here https://jsfiddle.net/cgxwzdy0/242/

This demo contains colors so you can see what is happening and why it works https://jsfiddle.net/cgxwzdy0/244/

Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34