中心响应DIV包含链接

时间:2018-03-07 09:46:16

标签: javascript html css

如何在不弄乱响应式菜单的情况下将其链接到其他页面居中。

我尝试了很多不同的解决方案,但它们最终都会破坏页面,例如导航栏消失或占用整个页面。

浮动:左移除它们会居中但不会在水平线上。

提前致谢。



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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

只需删除float:left并在display:inline-block.header a text-align:center类中添加.header

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>