How to create a top navigation bar using CSS? Last Updated : 29 Apr, 2024 Comments Improve Suggest changes Like Article Like Report A top navigation bar is a horizontal bar that typically contains links to different sections of a website. It is a fundamental part of a website's layout and helps users navigate easily. In this article, we will explore different approaches to creating a top navigation bar using CSS. These are the following approaches: Table of Content Using Float propertyUsing flexboxUsing Float propertyIn this approach, we are using Using Float property to create a top navigation bar using CSS. Syntax:float: leftExample: Implementation of a top navigation bar using Float property. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Navbar</title> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <style> /* styles.css */ body { margin: 0; font-family: Arial, sans-serif; } .navbar { overflow: hidden; background-color: green; } .navbar a { float: left; display: block; color: white; text-align: center; padding: 14px 20px; text-decoration: none; } .navbar a:hover { background-color: #4CAF50; color: black; } .navbar a.icon { display: none; } @media screen and (max-width: 600px) { .navbar a:not(:first-child) { display: none; } .navbar a.icon { float: right; display: block; } .navbar.responsive { position: relative; } .navbar.responsive a.icon { position: absolute; right: 0; top: 0; } .navbar.responsive a { float: none; display: block; text-align: left; } } </style> <body> <div class="navbar" id="myNavbar"> <a href="#">GFG</a> <a href="#">Home</a> <a href="#">About</a> <a href="#">Contact</a> <a href="javascript:void(0);" class="icon" onclick="toggleMenu()"> ☰ </a> </div> <script> function toggleMenu() { let navbar = document.getElementById("myNavbar"); navbar.className = navbar.className === "navbar" ? "navbar responsive" : "navbar"; } </script> </body> </html> Output: Using flexboxIn this approach, we are using Using flexbox to create a top navigation bar using CSS. Syntax:display: flex;flex-direction: column;Example: Implementation of a top navigation bar using flexbox. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Navbar</title> <style> body { margin: 0; font-family: Arial, sans-serif; } .navbar { display: flex; justify-content: space-between; align-items: center; background-color: green; padding: 14px 20px; } .navbar a { color: white; text-decoration: none; padding: 10px 15px; } .navbar a:hover { background-color: #4CAF50; color: black; } .navbar .logo { flex: 1; } .navbar .menu { display: flex; } .navbar.responsive .menu { display: block; } .navbar .icon { display: none; } @media screen and (max-width: 600px) { .navbar .icon { display: block; cursor: pointer; } .navbar .menu { display: none; } .navbar.responsive .menu { display: flex; flex-direction: column; background-color: green; position: absolute; top: 15%; left: 0; right: 0; } .navbar.responsive .menu a { padding: 10px 20px; /* text-align: center; */ } } </style> </head> <body> <div class="navbar" id="myNavbar"> <p href="#" class="logo">GFG</p> <div class="menu"> <a href="#">Home</a> <a href="#">About</a> <a href="#">Contact</a> </div> <a href="javascript:void(0);" class="icon" onclick="toggleMenu()"> ☰ </a> </div> <script> function toggleMenu() { let navbar = document.getElementById("myNavbar"); navbar.classList.toggle("responsive"); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to create a top navigation bar using CSS? G ghuleyogesh Follow Improve Article Tags : CSS Web Technologies Similar Reads How to create a split navigation bar using CSS ? The article will demonstrate how to create a split navigation bar using CSS. There are two ways to make the split navigation bar these are with the help of flexbox properties and CSS position property. A split navigation bar is a popular layout where navigation links are divided into two sections. I 4 min read How to create Vertical Navigation Bar using HTML and CSS ? After reading this article, you will be able to build your own vertical navigation bar. To follow this article you only need some basic understanding of HTML and CSS. Let us start writing our vertical navigation bar, first, we will write the structure of the navigation bar. In this tutorial, we crea 3 min read How to Create a Navigation Bar with CSS Flexbox? The navigation bar is a crucial aspect of modern web design. We can create a responsive navigation bar using CSS Flexbox, which provides a flexible and efficient way to layout elements, making it an excellent choice for creating navigation bars. Below are different possible ways for creating differe 5 min read How To Create a More Button in a Navigation Bar using CSS? Adding a More button in a navigation bar is used to handle overflow items when there is insufficient space to display all navigation links. We will explore how to implement a More button using CSS. ApproachCreate a web page structure using the <nav> element, which contains list items for each 2 min read How to create a navigation bar using React-Bootstrap? React-Bootstrap is a front-end framework that was designed with React in mind. The NavBar Component is a navigation header that is responsive and powerful. It supports navigation, branding, and many other related features. In this article, we are going to implement a navigation bar using React Boots 2 min read How to create a Menu Icon using CSS ? The Menu Icon is mostly used on smaller screens, there's limited space to display a full navigation menu. A menu icon helps in hiding the navigation items initially and revealing them when the user needs them. In this article, we will see how To Create a Menu Icon using CSS. There are many ways to c 3 min read How to Create a Navigation Bar with Icons in Tailwind CSS ? A navigation bar, commonly known as a nav bar, serves as a fundamental component of user interface design, facilitating seamless navigation across a website or application. Using the icons in the navigation bar gains a distinctive advantage in visual communication. Icons streamline navigation by usi 2 min read How to create Animated Blur Navbar using CSS? The Navbar is the main component of any website through which the user can navigate through all the components and sections of a site. That's why it is really important to have a well-designed navigation bar or menu. So today we will be looking at a navbar in which all the elements get blur except t 2 min read How to Create A Sticky NavBar Using Tailwind CSS? Sticky Navbar in Tailwind CSS is mostly used in applications where maintaining quick access to navigation links is essential as users scroll through content. It ensures that the navbar remains visible at the top of the screen, enhancing user experience by providing constant access to key sections of 4 min read How to create navigation bar using <div> tag in HTML ? In this article, we will know to create the navigation bar using the <div> tag in HTML. The Navbar is a navigation header that contains the links to navigate to the different pages or sections of the site that helps to make the website interactive. The navbar is an important & fundamental 3 min read Like