css close menu by clicking outside code example
Example: Click Outside Close Menu Box
window.addEventListener('mouseup', function(event){
var box = document.getElementById('box1');
if (event.target != box && event.target.parentNode != box){
box.style.display = 'none';
}
});