DEV Community

Promise
Promise

Posted on

How to Build a Responsive Navigation Menu with HTML and CSS

Introduction

In this tutorial, we will walk through the process of creating a responsive navigation menu using HTML and CSS. A responsive navigation menu adapts its layout to fit different screen sizes, making it user-friendly on both desktop and mobile devices.

Step 1: Setting up the HTML Structure

First, let's create the basic structure of our navigation menu in HTML.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Navigation Menu</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    </nav>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Step 2: Styling the Navigation Menu with CSS

Next, let's style our navigation menu to make it responsive using CSS.

body {
    margin: 0;
    padding: 0;
    font-family: Arial, sans-serif;
}

nav {
    background-color: #333;
}

ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
    display: flex;
}

li {
    padding: 15px;
}

a {
    text-decoration: none;
    color: #fff;
}

@media screen and (max-width: 600px) {
    nav {
        display: block;
    }

    ul {
        flex-direction: column;
    }
}

Enter fullscreen mode Exit fullscreen mode

In the CSS code above, we set the basic styles for the navigation menu and then use a media query to change the layout to a vertical stack when the screen width is less than 600px.

Step 3: Testing and Final Touches

Finally, save the HTML file as index.html and the CSS file as styles.css. Open the HTML file in a browser to see your responsive navigation menu in action.

You can further customize the design by adding hover effects, animations, or dropdown menus to enhance the user experience.

That's it! You have successfully created a responsive navigation menu using HTML and CSS. Feel free to experiment with different styles and layouts to suit your project's needs.

Top comments (0)