导航栏右侧有额外的空间

时间:2017-01-07 01:05:23

标签: javascript html css navbar

我目前正在创建我的第一个网站,并且我创建了一个导航栏。但我注意到酒吧右侧有一些额外的空间。因此,当我突出显示我的酒吧上的最后一件事时,有一些空间不会改变颜色(当我将鼠标悬停在标签上的东西上时,它会改变背景颜色)。所以我想知道是否有人有解决这个问题的方法?



ul.tab {
    list-style-type: none;
    margin: auto;
    font-family: "CopperPlate", Times, serif;
    padding-left: 0px;
    padding-right: 0px;
    overflow: hidden;
    background-color: #1A1B1F;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.6);
    opacity: .95;
    width: 742px;
}

ul.tab li {
    float: left;
    padding: 0px;
}

ul.tab a {
    display: block;
    color: #E7E8EC;
    text-align: center;
    font-size: 16px;
    padding: 16px 64px;
    text-decoration: none;
    transition: .3s;
}



.home:hover {
  background-color: #0EB323;
}

.about:hover {
  background-color: #0EB323;
}

.projects:hover {
  background-color: #0EB323;
}

.contact:hover {
  background-color: #0EB323;
}
li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: rgba(34, 43, 47, .8);
    width: 200px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-color: #0EB323}

.dropdown:hover .dropdown-content {
    display: block;
}

<!DOCTYPE HTML>
<html>

<title> Website </title>

<head> 

<link rel = "stylesheet" href = "CSScode.css">

<script src = "JavaScript.js"></script>
  
<style>
</style>

</head>

<body>

<ul class = "tab">
  <li><a class = "home" href = "HomePage.html">Home</a></li>
  <li><a class = "about" href = "#about"> About </a></li>

  <li class = "dropdown">
    <a class = "projects dropbtn"> Projects </a>
    <div class = "dropdown-content">
      <a href="#"> Project 1 </a>
      <a href="#"> Project 2 </a>
      <a href="#"> Project 3 </a>
    </div>

  <li><a class = "contact" href = "ContactPage.html"> Contact </a></li>
</ul>

</body>

</html>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

您已将导航栏的宽度设置为742px。您所有商品(您的所有商品)的总宽度并不足以填满整个酒吧。

尝试:

ul.tab li {
    float: left;
    padding: 0px;
    width: 25%;
}

4件物品,占总容器总量的25%。

答案 1 :(得分:0)

导航栏的宽度大于导航项的宽度。除非您在与导航栏的宽度相加的项目上声明硬宽度,否则您不应使用固定宽度。

我要做的是将Profile.Model更改为ul.tab,以便它只占用所需的空间,并缩小以匹配其直接后代的大小,以及将display:inline-block;添加到该元素的父级(在本例中为text-align: center;)以使元素居中。

这是一个演示 - http://codepen.io/anon/pen/JEdywM

答案 2 :(得分:0)

尝试使用flexbox display: flex上的ulflex: 1上的li

要将dropdown菜单与按钮对齐,relative position按钮,下拉div上100%宽度,并从overflow hidden

中删除ul

此外,您不需要向a元素添加左右填充,因为您将它们显示为块。

ul.tab {
    list-style-type: none;
    margin: auto;
    font-family: "CopperPlate", Times, serif;
    padding-left: 0px;
    padding-right: 0px;
    /*overflow: hidden;*/
    background-color: #1A1B1F;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.6);
    opacity: .95;
    width: 742px;
  
  display: flex;
  
}

ul.tab li {
  flex: 1;
    /*float: left;*/
    padding: 0px;
}

ul.tab a {
    display: block;
    color: #E7E8EC;
    text-align: center;
    font-size: 16px;
    padding: 16px 0;
    text-decoration: none;
    transition: .3s;
}



.home:hover {
  background-color: #0EB323;
}

.about:hover {
  background-color: #0EB323;
}

.projects:hover {
  background-color: #0EB323;
}

.contact:hover {
  background-color: #0EB323;
}
li.dropdown {
    display: inline-block;
  position: relative;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: rgba(34, 43, 47, .8);
    width: 100%;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 0;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-color: #0EB323}

.dropdown:hover .dropdown-content {
    display: block;
}
<!DOCTYPE HTML>
<html>

<title> Website </title>

<head> 

<link rel = "stylesheet" href = "CSScode.css">

<script src = "JavaScript.js"></script>
  
<style>
</style>

</head>

<body>

<ul class = "tab">
  <li><a class = "home" href = "HomePage.html">Home</a></li>
  <li><a class = "about" href = "#about"> About </a></li>

  <li class = "dropdown">
    <a class = "projects dropbtn"> Projects </a>
    <div class = "dropdown-content">
      <a href="#"> Project 1 </a>
      <a href="#"> Project 2 </a>
      <a href="#"> Project 3 </a>
    </div>

  <li><a class = "contact" href = "ContactPage.html"> Contact </a></li>
</ul>

</body>

</html>

答案 3 :(得分:0)

您的li元素都具有动态宽度,这些宽度将呈现为像素的浮点值,并最终舍入为全像素。那个边际来自哪里。为了防止这种情况:

ul.tab的CSS规则中,全部删除宽度设置并添加display: inline-block;

PLUS:将ul换行到包含text-align: center的容器div中:

(以整页模式查看片段以查看结果)

&#13;
&#13;
.listcontainer {
  text-align: center;
  }
ul.tab {
    list-style-type: none;
    margin: auto;
    font-family: "CopperPlate", Times, serif;
    padding-left: 0px;
    padding-right: 0px;
    overflow: hidden;
    background-color: #1A1B1F;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.6);
    opacity: .95;
    display: inline-block;

}

ul.tab li {
    float: left;
    padding: 0px;
}

ul.tab a {
    display: block;
    color: #E7E8EC;
    text-align: center;
    font-size: 16px;
    padding: 16px 64px;
    text-decoration: none;
    transition: .3s;
}



.home:hover {
  background-color: #0EB323;
}

.about:hover {
  background-color: #0EB323;
}

.projects:hover {
  background-color: #0EB323;
}

.contact:hover {
  background-color: #0EB323;
}
li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: rgba(34, 43, 47, .8);
    width: 200px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-color: #0EB323}

.dropdown:hover .dropdown-content {
    display: block;
}
&#13;
<!DOCTYPE HTML>
<html>

<title> Website </title>

<head> 

<link rel = "stylesheet" href = "CSScode.css">

<script src = "JavaScript.js"></script>
  
<style>
</style>

</head>

<body>
<div class="listcontainer">
<ul class = "tab">
  <li><a class = "home" href = "HomePage.html">Home</a></li>
  <li><a class = "about" href = "#about"> About </a></li>

  <li class = "dropdown">
    <a class = "projects dropbtn"> Projects </a>
    <div class = "dropdown-content">
      <a href="#"> Project 1 </a>
      <a href="#"> Project 2 </a>
      <a href="#"> Project 3 </a>
    </div>

  <li><a class = "contact" href = "ContactPage.html"> Contact </a></li>
</ul>
</div>
</body>

</html>
&#13;
&#13;
&#13;