How TO – Centered Top Navigation


Previous


Next

Learn how to create a navigation bar with a centered link/logo.


Centered Menu Link

<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}

.topnav {
position: relative;
overflow: hidden;
background-color: #333;
}

.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}

.topnav a:hover {
background-color: #ddd;
color: black;
}

.topnav a.active {
background-color: #04AA6D;
color: white;
}

.topnav-centered a {
float: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.topnav-right {
float: right;
}

/* Responsive navigation menu (for mobile devices) */
@media screen and (max-width: 600px) {
.topnav a, .topnav-right {
float: none;
display: block;
}

.topnav-centered a {
position: relative;
top: 0;
left: 0;
transform: none;
}
}
</style>
</head>
<body>

<!– Top navigation –>
<div>

<!– Centered link –>
<div>
<a href=”#home”>Home</a>
</div>

<!– Left-aligned links (default) –>
<a href=”#news”>News</a>
<a href=”#contact”>Contact</a>

<!– Right-aligned links –>
<div>
<a href=”#search”>Search</a>
<a href=”#about”>About</a>
</div>

</div>

<div style=”padding-left:16px”>
<h2>Responsive Top Navigation with Centered and Right-Aligned Links</h2>
<p>Resize the browser window to see the responsive effect.</p>
</div>

</body>
</html>

Create A Top Navigation Bar

Step 1) Add HTML:

<!– Top navigation –>
<div class=”topnav”>

  <!– Centered link –>
  <div class=”topnav-centered”>
    <a href=”#home” class=”active”>Home</a>
  </div>

  <!– Left-aligned links (default) –>
  <a href=”#news”>News</a>
  <a href=”#contact”>Contact</a>

  <!– Right-aligned links –>
  <div class=”topnav-right”>
    <a href=”#search”>Search</a>
    <a href=”#about”>About</a>
  </div>

</div>

Step 2) Add CSS:

/* Add a black background color to the top navigation */
.topnav {
  position: relative;
  background-color: #333;
  overflow: hidden;
}

/* Style the links inside the navigation bar */
.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}

/* Add a color to the active/current link */
.topnav a.active {
  background-color: #04AA6D;
  color: white;
}

/* Centered section inside the top navigation */
.topnav-centered a {
  float: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* Right-aligned section inside the top navigation */
.topnav-right {
  float: right;
}

/* Responsive navigation menu – display links on top of each other instead of next to each other (for mobile devices) */
@media screen and (max-width: 600px) {
  .topnav a, .topnav-right {
    float: none;
    display: block;
  }

  .topnav-centered a {
    position: relative;
    top: 0;
    left: 0;
    transform: none;
  }
}


Next


Previous

Scroll to Top