I'm building a topbar-style menu and I found a problem with flexbox.
My submenus contain an image that has certain flex-basis
. The container seems to ignore it when setting its width for some reason. How can I avoid content overflow in this case? Setting flex-shrink
on any of the containers doesn't seem to help.
Here's how it looks like:
The point is to make the orange block as wide as the contents.
.menu {
background-color: red;
display: flex;
}
.option {
background-color: yellow;
display: flex;
flex-direction: column;
}
.submenu-container {
position: relative;
}
.submenu {
background-color: orange;
flex-shrink: 0;
left: 0;
position: absolute;
top: 0;
white-space: nowrap;
}
.submenu-item {
display: flex;
}
.icon {
flex-basis: 40px;
flex-shrink: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Menu Bar</title>
</head>
<body>
<div class="menu">
<div class="option">
<div>Option</div>
<div class="submenu-container">
<div class="submenu">
<div class="submenu-item">
<div class="icon">x</div>
<div>Submenu option</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>