I have been working on creating a hover menu using CSS and Bootstrap, specifically for flex grid layouts. However, I have run into an issue where my menu is only as wide as its parent div.
Furthermore, I am struggling to center the hover menu relative to the length of each menu item. Can anyone provide guidance on how to stretch the width of my menu items to prevent text wrapping and how to center the entire menu in relation to its link?
Here is the HTML code:
<div class="d-flex flex-row">
<div class="dropdown">
<button class="dropbtn">Small</button>
<div class="dropdown-content">
<a href="#">Link 1 long link</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Long Menu item</button>
<div class="dropdown-content">
<a href="#">Link 1 long link</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn">a really long Long Menu item</button>
<div class="dropdown-content">
<a href="#">Link 1 long link</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
And here is the relevant CSS:
/* Dropdown Button */
.dropbtn {
color: #3a3a3a;
padding: 16px;
font-size: 16px;
border: none;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background: #fff;
border: 1px solid #cecece;
color:#3a3a3a;
z-index: 1;
box-sizing: border-box;
}
.dropdown-content:after, .dropdown-content:before {
bottom: 100%;
left: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.dropdown-content:after {
border-bottom-color: #fff;
border-width: 13px;
margin-left: -13px;
}
.dropdown-content:before {
border-bottom-color: #cecece;
border-width: 14px;
margin-left: -14px;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
color:$green;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
//background-color: #3e8e41;
}