How do I make the hovered element to stay. As you can see in the codepen if I hover over the "Service" it shows the content but when I try to click on the content it disappears.
index.html
.list {
display: flex;
flex-direction: column;
width: 100px;
}
.list .item {
padding: 10px;
font-size: 24px;
font-weight: normal;
background-color: #ccc;
cursor: pointer;
}
.list .item:hover {
text-decoration: underline;
}
.list .item.item-dropdown {
position: relative;
}
.list .item.item-dropdown:hover .dropdown-content {
display: block;
}
.list .dropdown-content {
display: none;
position: absolute;
left: 120px;
bottom: -20px;
padding: 5px 20px;
background-color: #ccc;
}
.list .dropdown-content:before {
content: "";
position: absolute;
width: 0;
height: 0;
border-top: 17px solid transparent;
border-right: 14px solid #ccc;
border-bottom: 17px solid transparent;
margin-left: -8px;
top: 45%;
left: -5%;
}
<div class="list">
<div class="item">Home</div>
<div class="item">About</div>
<div class="item item-dropdown">Services
<div class="dropdown-content">
<div class="item">Home</div>
<div class="item">About</div>
</div>
</div>
</div>
Note: I also see that sometimes its working and sometimes it doesn't when you hover over from the beak it works but when trying hover over outside beak it doesn't.
One way to achieve this is to wrap your menu items inside an element with margin-left.
.list {
display: flex;
flex-direction: column;
width: 100px;
}
.list .item {
padding: 10px;
font-size: 24px;
font-weight: normal;
background-color: #ccc;
cursor: pointer;
}
.list .item:hover {
text-decoration: underline;
}
.list .item.item-dropdown {
position: relative;
}
.list .item.item-dropdown:hover .dropdown-content {
display: block;
}
.list .dropdown-content {
display: none;
position: absolute;
left: 100px;
bottom: -20px;
}
.list .dd-menu{
position:relative;
margin-left:20px;
background-color: #ccc;
padding: 5px 20px;
}
.list .dd-menu:before {
content: "";
position: absolute;
width: 0;
height: 0;
border-top: 17px solid transparent;
border-right: 14px solid #ccc;
border-bottom: 17px solid transparent;
margin-left: -8px;
top: 45%;
left: -5%;
}
<div class="list">
<div class="item">Home</div>
<div class="item">About</div>
<div class="item item-dropdown">Services
<div class="dropdown-content">
<div class="dd-menu">
<div class="item">Home</div>
<div class="item">About</div>
</div>
</div>
</div>
</div>
Immediate absolute
positioned child shouldn't have any margin before it to make hover possible.