0

<ul id="menu">
   <li>
   <input id="check01" type="checkbox" name="menu"/>
   <label for="check01">Tasto menu 01</label>
 <ul class="submenu">
  <li><a href="#">Sotto menu 1</a></li>
  <li><a href="#">Sotto menu 2</a></li>
</ul>
 </li>
  <li>
   <input id="check02" type="checkbox" name="menu"/>
    <label for="check02">Tasto menu 02</label>
    <ul class="submenu">
  <li><a href="#">Sotto menu 3 (long text)</a></li>
  <li><a href="#">Sotto menu 4</a></li>
  <li><a href="#">Sotto menu 5</a></li>
</ul>
 </li>
    </ul>
      <article>
     <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate earum nulla 
 asperiores corrupti eius maxime laboriosam voluptates sequi at iure consequatur suscipit voluptatibus magnam alias harum incidunt quis. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quam officia labore maiores perspiciatis nulla aperiam magnam dicta commodi debitis distinctio temporibus in saepe unde. Incidunt vel labore accusamus voluptas eligendi. 
   </p>
   </article>
 <style>
 /*General styles for body*/
 body{
 margin:0;
 padding:3em 0;
 font-family:Helvetica, Arial, sans-serif;
 background:#eee;
 color:#222;
 }

 article{margin:10px;}

  /*Style for the first level menu bar*/
  ul#menu{
   position:fixed;
   top:0;
   width:100%;
   height:3em;
  margin:0;
   padding:0 10px;
   background:#333;
    color:#eee;
   box-shadow:0 -1px rgba(0,0,0,.5) inset;
  }

  ul#menu > li{
    float:left;
    list-style-type:none;
   position:relative;
   }

   label{
    position:relative;
    display:block;
   padding:0 18px 0 12px;
   line-height:3em;
   transition:background 0.3s;
   cursor:pointer;
  }

   label:after{
   content:"";
   position:absolute;
   display:block;
   top:50%;
   right:5px;
   width:0;
   height:0;
   border-top:4px solid rgba(255,255,255,.5);
   border-bottom:0 solid rgba(255,255,255,.5);
   border-left:4px solid transparent;
  border-right:4px solid transparent;
    transition:border-bottom .1s, border-top .1s .1s;
    }

  label:hover,
  input:checked ~ label{background:rgba(0,0,0,.3);}

  input:checked ~ label:after{
   border-top:0 solid rgba(255,255,255,.5);
   border-bottom:4px solid rgba(255,255,255,.5);
    transition:border-top .1s, border-bottom .1s .1s;
     }

  /*hide the inputs*/
   input{display:none}

   /*show the second levele menu of the selected voice*/
   input:checked ~ ul.submenu{
    max-height:300px;
    transition:max-height 0.5s ease-in;
   }

  /*style for the second level menu*/
 ul.submenu{
   max-height:0;
   padding:0;
   overflow:hidden;
 list-style-type:none;
 background:#444;
   box-shadow:0 0 1px rgba(0,0,0,.3);
   transition:max-height 0.5s ease-out;
   position:absolute;
   min-width:100%;
  }

  ul.submenu li a{
     display:block;
    padding:12px;
    color:#ddd;
    text-decoration:none;
    box-shadow:0 -1px rgba(0,0,0,.5) inset;
    transition:background .3s;
     white-space:nowrap;
   }

   ul.submenu li a:hover{
     background:rgba(0,0,0,.3);
   }
    </style>

I'm building a pure css dropdown menu with pure css with this concept https://codepen.io/Tont/pen/hdsev

The menu works as expected and the dropdown toggles when user clicks on the top menu item.

I want to close any dropdown if there is a click anywhere outside of the menu.

How can I achieve this with pure css no JavaScript?

I know how achieve this with JavaScript, looking for Css solution

BenB
  • 2,747
  • 3
  • 29
  • 54
  • I dont see how you could do it without JS. With JS you either could use an eventlister to listen for mouseclicks and using an if statement to check if the click was within the menu or outside. Alternativly to use an onclick event for everythign else that closes the menu. As there is no parent selector within HTML and CSS or a way to recognize clicks and do soemthing with it outside of checkbox. You cant uncheck a checkbox without JS unless you click on that specific checkbox. – tacoshy Dec 05 '20 at 03:14
  • You have to use Javascript, when and if you decide to do so heres another question that might provide an answer, https://stackoverflow.com/questions/152975/how-do-i-detect-a-click-outside-an-element/3028037#3028037 – Eduardo Dec 05 '20 at 04:21

1 Answers1

-1

CSS can't be used for this purpose. You are trying to essentially "listen" for when the user clicks outside of the selected area.

To listen for that, you will need an event listener from Javascript. You can then add a style property of display:none or whatever suits your purpose.

This solution would only require a few lines of JavaScript, but there is no way to listen for a click without a JavaScript eventlistener.

Kalin Patel
  • 501
  • 3
  • 5