I created a navbar
having a drop down menu displayed when the button
is hovered upon.
This is the codepen: https://codepen.io/sahxil/pen/qBoVgXg
I have four problems with the navbar. If you look at the codepen and hover over Solutions
:
- You can see that the navbar gets displaced i.e. The other buttons shift towards the bottom.
- A normal drop-down(triggered by
mouseover
) should collapse when the cursor no longer hovers over the nav button right? but it does not collapse. It collapses only the second time it is hovered upon.(See the codepen for more clarity). - The Nav Buttons when hovered upon get a red underline just below them having width of 30%, however, with buttons having a drop down, the underline is displaced(as you can see when you hover over
Solutions
.
Apart from this, another problem is that the button
with drop-down menus are taking more width in the navbar
even when they are not hovered on.
How do I fix this?
CodePudding user response:
Use
#navbar {align-items: flex-start}
to tell flexbox to align your items correctly: https://css-tricks.com/snippets/css/a-guide-to-flexbox/Use
mouseout
as a companion to mouseover. Like mouseover, it also works on child elements. https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseout_eventSet
position: relative
on the parent element (div.select-btn
). The reason this is happening is because your parent element is positioned asstatic
. See this for an explanation of why that matters: https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_blockI assume that by "button" you mean anchor elements? I don't see any actual button elements in your code. If you're finding that the spacing is too wide, then try adjusting
justify-content
on eithersection#header
and/or onul#navbar