
Sign up to save your podcasts
Or


In this tutorial, we will create a responsive and animated navigation bar using HTML and CSS. This navigation bar will be mobile-friendly and will have a hamburger menu that transforms into a close icon when clicked.
HTML Structure
First, let’s look at the HTML structure of our navigation bar.
Explanation
*
: This is the container for our navigation bar.* : This checkbox will be used to toggle the visibility of the menu.
* : This label acts as a button for the checkbox.
*
: These divs inside the label create the hamburger icon.*
CSS Styling
Now, let’s style our navigation bar using CSS.
body {
Explanation
* body: Sets the font family, margin, and padding for the entire page.
* nav#mobile-menu: Styles the navigation bar container with a background color and relative positioning.
* nav#mobile-menu ul: Styles the unordered list inside the navigation bar. It hides the list by default and aligns the items in a column.
* nav#mobile-menu ul li: Styles the list items to be displayed as blocks.
* nav#mobile-menu ul li a: Styles the links inside the list items with color, padding, and display properties.
* nav#mobile-menu ul li a:hover: Changes the background color of the links when hovered.
* .menu-btn: Styles the hamburger menu button with dimensions, cursor, and positioning.
* .menu-btn div: Styles the individual bars of the hamburger menu with dimensions, background color, margin, and transition.
* #menu-toggle: Hides the checkbox input.
* #menu-toggle:checked + .menu-btn div:nth-child(1): Rotates and changes the color of the first bar when the checkbox is checked.
* #menu-toggle:checked + .menu-btn div:nth-child(2): Hides the second bar when the checkbox is checked.
* #menu-toggle:checked + .menu-btn div:nth-child(3): Rotates and changes the color of the third bar when the checkbox is checked.
* #menu-toggle:checked ~ ul: Displays the menu when the checkbox is checked.
* @media screen and (min-width: 768px): Media query to style the navigation bar for larger screens. It displays the menu items in a row and hides the hamburger menu button.
Conclusion
With this HTML and CSS code, you have created a responsive and animated navigation bar. The hamburger menu transforms into a close icon when clicked, and the menu items are displayed in a column on smaller screens and in a row on larger screens. Feel free to customize the styles to match your website’s design.
By Norbert B.M.In this tutorial, we will create a responsive and animated navigation bar using HTML and CSS. This navigation bar will be mobile-friendly and will have a hamburger menu that transforms into a close icon when clicked.
HTML Structure
First, let’s look at the HTML structure of our navigation bar.
Explanation
*
: This is the container for our navigation bar.* : This checkbox will be used to toggle the visibility of the menu.
* : This label acts as a button for the checkbox.
*
: These divs inside the label create the hamburger icon.*
CSS Styling
Now, let’s style our navigation bar using CSS.
body {
Explanation
* body: Sets the font family, margin, and padding for the entire page.
* nav#mobile-menu: Styles the navigation bar container with a background color and relative positioning.
* nav#mobile-menu ul: Styles the unordered list inside the navigation bar. It hides the list by default and aligns the items in a column.
* nav#mobile-menu ul li: Styles the list items to be displayed as blocks.
* nav#mobile-menu ul li a: Styles the links inside the list items with color, padding, and display properties.
* nav#mobile-menu ul li a:hover: Changes the background color of the links when hovered.
* .menu-btn: Styles the hamburger menu button with dimensions, cursor, and positioning.
* .menu-btn div: Styles the individual bars of the hamburger menu with dimensions, background color, margin, and transition.
* #menu-toggle: Hides the checkbox input.
* #menu-toggle:checked + .menu-btn div:nth-child(1): Rotates and changes the color of the first bar when the checkbox is checked.
* #menu-toggle:checked + .menu-btn div:nth-child(2): Hides the second bar when the checkbox is checked.
* #menu-toggle:checked + .menu-btn div:nth-child(3): Rotates and changes the color of the third bar when the checkbox is checked.
* #menu-toggle:checked ~ ul: Displays the menu when the checkbox is checked.
* @media screen and (min-width: 768px): Media query to style the navigation bar for larger screens. It displays the menu items in a row and hides the hamburger menu button.
Conclusion
With this HTML and CSS code, you have created a responsive and animated navigation bar. The hamburger menu transforms into a close icon when clicked, and the menu items are displayed in a column on smaller screens and in a row on larger screens. Feel free to customize the styles to match your website’s design.