var closeDrops = function(e) {
$('.flex-menu div').removeClass('active');
}
$('body').on('click', closeDrops);
$('.flex-menu div').on('click', function(e){
if (!$(this).hasClass('active')) {
closeDrops();
}
e.stopPropagation();
$(this).toggleClass('active');
})
body {
margin: 0;
padding: 0;
background-color: #f5f5f5;
min-height: 100vh;
font-family: sans-serif;
}
.flex-menu {
display: flex;
position: relative;
}
.flex-menu > * {
position: relative;
flex: 1 0 auto;
padding: 10px;
border: solid white;
border-width: 0 1px 1px 0;
cursor: pointer;
transition: background-color .3s ease-in-out;
}
.flex-menu > *:last-child {
border-right: none;
}
.flex-menu > * .submenu {
position: absolute;
top: 100%;
left: 0;
display: none;
background-color: white;
border-top: 1px solid #eee;
padding: 10px;
box-shadow: 0 1px 3px 0 rgba(0,0,0,.2), 0 1px 1px 0 rgba(0,0,0,.14), 0 2px 1px -1px rgba(0,0,0,.12)
}
.flex-menu .has-megamenu {
position: static
}
.flex-menu > * .submenu.megamenu {
width: 100vw;
left: 0;
box-sizing: border-box;
}
.flex-menu > *:hover .submenu,.flex-menu > *.active .submenu {
display: block;
}
.flex-menu > *.active, .flex-menu > *:hover {
box-shadow: 0 1px 3px 0 rgba(0,0,0,.2), 0 1px 1px 0 rgba(0,0,0,.14), 0 2px 1px -1px rgba(0,0,0,.12);
background-color: white;
}
.flex-menu > *.active{
z-index: 1;
}
.flex-menu > *:hover {
z-index: 2;
}
.has-megamenu:last-child .megamenu{
background-color: transparent; padding: 0;
box-shadow: none;
}
.has-megamenu:last-child .megamenu > * {
box-shadow: 0 1px 3px 0 rgba(0,0,0,.2), 0 1px 1px 0 rgba(0,0,0,.14), 0 2px 1px -1px rgba(0,0,0,.12);
}
.placed-right {
background-color: white;
padding:10px;
float: right;
}
@media (max-width: 500px) {
.flex-menu {
flex-direction: column;
}
.flex-menu > * .submenu {
width: 100vw;
box-sizing: border-box;
}
.placed-right {
float: none;
width: 100%;
display: block;
box-sizing: border-box;
}
.flex-menu .has-megamenu {
position: relative;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="flex-menu">
<div>first item</div>
<div>second item
<div class="submenu">
This is a dropdopwn content.
</div>
</div>
<div class="has-megamenu">third item
<div class="submenu megamenu">
This is a mega menu dropdopwn content. You can put anything here. A full page, of content, if you want
</div>
</div>
<div class="has-megamenu">fourth item
<div class="submenu megamenu">
<span class="placed-right">
I am right-aligned and I don't care about my parent's width, ok? <hr />I'll only wrap when I don't fit in page.
</span>
</div>
</div>
</div>