Learn How To Center A Navbar

In this article, you will learn How to Center A Navbar in HTML & CSS. What is a Navbar? A Navbar is a section in…

The post Learn How To Center A Navbar appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Jatin Hemnani

In this article, you will learn How to Center A Navbar in HTML & CSS.

What is a Navbar?

A Navbar is a section in a Website that contains links to other parts of the Website for example About Page, Contact Page, Feedback Page, etc.

Let’s create a Navbar in HTML:-

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link href="style.css" rel="stylesheet" />
    <title>Center Navbar</title>
  </head>
  <body>
    <nav>
      <ul class="nav-cont">
        <li class="nav-item"><a href="/">Home</a></li>
        <li class="nav-item"><a href="/products">Products</a></li>
        <li class="nav-item"><a href="/about">About</a></li>
        <li class="nav-item"><a href="/contact">Contact</a></li>
      </ul>
    </nav>
  </body>
</html>

In the above code we have a list of Navigation items with nested <a> tag so that we can navigate to different parts of our Website. And classes to design it in our CSS file.

Styling The Navbar:-

style.css

.nav-cont {
  display: flex;
  list-style-type: none;
  justify-content: center;
  flex-wrap: wrap;
  background-color: #ff7675;
}
.nav-item {
  padding: 1em;
}

In the above code we have added some styles to our <ul> with class of nav-cont.

Each line of code represents:-

  • display:flex; : it expands the item to display Horizontally(in our case) or Vertically.
  • list-style-type:none; : to remove the list icon (like square, circle).
  • justify-content:center; : to center the whole navbar.
  • flex-wrap:wrap;: for the responsiveness of the website (for different screen sizes) as it will adjust automatically & wouldn’t overflow.
  • padding:1em; : to add some space between different items.

Result of the above code:-

Center A Navbar

The post Learn How To Center A Navbar appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Jatin Hemnani


Print Share Comment Cite Upload Translate Updates
APA

Jatin Hemnani | Sciencx (2021-02-17T11:09:15+00:00) Learn How To Center A Navbar. Retrieved from https://www.scien.cx/2021/02/17/learn-how-to-center-a-navbar/

MLA
" » Learn How To Center A Navbar." Jatin Hemnani | Sciencx - Wednesday February 17, 2021, https://www.scien.cx/2021/02/17/learn-how-to-center-a-navbar/
HARVARD
Jatin Hemnani | Sciencx Wednesday February 17, 2021 » Learn How To Center A Navbar., viewed ,<https://www.scien.cx/2021/02/17/learn-how-to-center-a-navbar/>
VANCOUVER
Jatin Hemnani | Sciencx - » Learn How To Center A Navbar. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/17/learn-how-to-center-a-navbar/
CHICAGO
" » Learn How To Center A Navbar." Jatin Hemnani | Sciencx - Accessed . https://www.scien.cx/2021/02/17/learn-how-to-center-a-navbar/
IEEE
" » Learn How To Center A Navbar." Jatin Hemnani | Sciencx [Online]. Available: https://www.scien.cx/2021/02/17/learn-how-to-center-a-navbar/. [Accessed: ]
rf:citation
» Learn How To Center A Navbar | Jatin Hemnani | Sciencx | https://www.scien.cx/2021/02/17/learn-how-to-center-a-navbar/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.