HTML-CSS: Menu on image hover
HTML-CSS : Exercise-37 with Solution
Using HTML, CSS displays a menu overlay when the user hovers over the image.
- Use a <figure> to wrap the <img> element and a <div> element that will contain the menu links.
- Use the opacity and right attributes to animate the image on hover, creating a sliding effect.
- Set the left attribute of the <div> to the negative of the element's width. Reset it to 0 when hovering over the parent element to slide in the menu.
- Use display: flex, flex-direction: column and justify-content: center on the <div> to vertically center the menu items.
HTML Code:
Explanation:
- <figure class="hover-menu">:
- Defines a figure element to contain the image and the menu.
- The class "hover-menu" is added for styling purposes.
- <img src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-7.jpeg"/>:
- Inserts an image with the specified source.
- <div>:
- Defines a div element to contain the menu links.
- <a href="#">:
- Creates link elements with "#" as the href attribute value, which typically refers to the current page.
- Three links are created for Home, Contact, and About pages.
CSS Code:
Explanation:
- .hover-menu:
- Defines the styling for the container element with class "hover-menu".
- Sets its positioning, dimensions, background color, and text alignment.
- .hover-menu *:
- Applies box-sizing to all elements inside .hover-menu to include padding and border in their total width and height.
- .hover-menu img:
- Defines the styling for the image inside .hover-menu.
- Sets its positioning, maximum width, initial opacity, and transition effect.
- .hover-menu div:
- Defines the styling for the overlay menu inside .hover-menu.
- Sets its positioning, dimensions, background color, padding, and transition effect.
- Uses flexbox to center the menu items vertically.
- .hover-menu div a:
- Defines the styling for the links inside the overlay menu.
- Sets their display, color, opacity, padding, and transition effect.
- .hover-menu:hover img:
- Defines the styling for the image inside .hover-menu on hover.
- Reduces its opacity and moves it to the left.
- .hover-menu:hover div:
- Defines the styling for the overlay menu inside .hover-menu on
HTML-CSS Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics