ablnoozy
ablnoozy

Reputation: 25

Center responsive DIV containing links

how can I get my links to the other pages centered without messing with the responsive menu.

I have tried a lot of different solutions but they all end up breaking the page, such as the navbar disappearing or taking up the entire page.

With the float: left removed they become centered but not in a horizontal line.

Thanks in advance.

function menu() {

  var x = document.getElementById("divNav");
  if (x.className === "header") {
    x.className += " responsive";
  } else {
    x.className = "header";
  }
}
.header {
  overflow: hidden;
  background-color: #3A3A3A;
}

.header a {
  float: left;
  display: block;
  color: #FFFFFF;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.header a:hover {
  background-color: #2B2B2B;
}

.header .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .header a:not(:first-child) {
    display: none;
  }
  .header a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .header.responsive {
    position: relative;
  }
  .header.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .header.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<div class="header" id="divNav">
  <a href="Home.html">Home</a>
  <a href="page1.html">Graphic Design</a>
  <a href="page2.html">Vector Work</a>
  <a href="page3.html">Video Editing</a>
  <a href="page4.html">Other Work</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="menu()">&#9776;</a>

Upvotes: 0

Views: 44

Answers (1)

Minal Chauhan
Minal Chauhan

Reputation: 6158

Just remove float:left and add display:inline-block in .header a and text-align:center in .header class

function menu() {

  var x = document.getElementById("divNav");
  if (x.className === "header") {
    x.className += " responsive";
  } else {
    x.className = "header";
  }
}
.header {
  overflow: hidden;
  background-color: #3A3A3A;
  text-align:center;
}

.header a {
  display: inline-block;
  color: #FFFFFF;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.header a:hover {
  background-color: #2B2B2B;
}

.header .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .header a:not(:first-child) {
    display: none;
  }
  .header a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .header.responsive {
    position: relative;
  }
  .header.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .header.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<div class="header" id="divNav">
  <a href="Home.html">Home</a>
  <a href="page1.html">Graphic Design</a>
  <a href="page2.html">Vector Work</a>
  <a href="page3.html">Video Editing</a>
  <a href="page4.html">Other Work</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="menu()">&#9776;</a>

Upvotes: 1

Related Questions