导航栏下方额外 space
Extra space underneath navigation bar
我目前正在尝试制作一个网站,并且设法创建了一个导航栏。但是,我的导航栏之间有一个奇怪的“额外”space。一开始它工作正常,但在我引用 JQuery 之后。但是,我怀疑 JQuery 是导致问题的原因,因为即使我将其删除,该网站看起来还是一样。 Here's a screenshot of the problem. I'm trying to get rid of the circled part.
我的HTML代码:
<div id="nav"> <!-- Navigation Bar -->
<nav>
<ul>
<li><a class="selected" href="#">Home</a></li>
<li><a href="about.html">About Me</a></li> <!-- Link to about page -->
<li class="drop">
<a href="games.html" class="dropbtn">Games</a>
<div class="drop-content">
<a href="rfg.html">Red Faction: Guerrilla</a> <!-- Contains links to the respective pages -->
<a href="wots3.html">Way of the Samurai 3</a>
<a href="singularity.html">Singularity</a>
</div>
</li>
<li><a href="reviews.html">Reviews</a></li>
<li><a href="external.html">External Store</a></li> <!-- Link to external site -->
<li><a href="video.html">Videos</a></li> <!-- Contains links to Youtube -->
<li><a href="feedback.html">Feedback</a></li> <!--Feedback form-->
这是样式表:
#nav nav{ /* Navigation bar*/
padding-top: 5%;
margin-bottom: 5% }
#nav ul {
list-style-type: none;
margin: 0;
background-color: #1C86EE;
padding: 0;
position: relative;
width: 100%;
bottom: 0;
display: inline-block }
#nav li {
float: left;
width: 14% }
li a, .dropbtn {
display: inline-block;
color: #FFFFFF;
text-align: center;
padding-left: 15%;
padding-right: 15%;
text-decoration: none }
.selected {
background-color: #6CCC0A;
padding-right: 25% }
li a:hover, .drop:hover .dropbtn {
background-color: #BFA810 }
a {
padding-top: 1em;
padding-bottom: 1em }
li.drop {
display: inline-block }
.drop-content {
display: none;
position: absolute;
background-color: #970707;
min-width: 14%;
box-shadow: 0px 8px 16px 0px #000000;
z-index: 100 }
.drop-content a {
color: #FFFFFF;
padding: 15px 20px;
display: block;
text-align: left }
.drop-content a:hover {
background-color: #02BBC4 }
.drop:hover .drop-content {
display: block;
border: 1px solid black;
position: absolute } /*End of navigation*/
* { /* index style */
box-sizing:border-box
}
body {
font-family: Verdana,sans-serif;
margin:0
}
.slideshow-fade {
display:none
}
/* Slideshow container */
.slideshow {
max-width: 100%;
position: relative;
margin: auto;
}
.slideshow img {
opacity: 0.7
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 1em;
margin-top: -1em;
color: #FFFFFF;
font-weight: bold;
font-size: 1.5em;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #000000;
font-size: 1.3em;
padding: 1% 2%;
position: absolute;
bottom: 2%;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.number {
color: #000000;
font-size: 1em;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 1em;
width: 4%;
margin: 0 2px;
background-color: #bbbbbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width : 720px ){
.prev, .next,.text {font-size: 11px}
}
.slideshow-fade a {
color: #FFFFFF
}
.slideshow-fade a:visited {
color: #5115D0
} /*End of style for index*/
footer { /*Footer*/
background: #000000;
width: 100%;
height: 5%;
position: relative;
bottom: 0;
left: 0;
padding-top: 2%;
overflow: hidden;
}
footer p {
font-family: arial, calibri, sans-serif;
color: #FFFFFF;
text-align: center;
padding-bottom: 2%;
} /*End of footer*/
任何帮助将不胜感激。谢谢。
这里放上jsfiddle文件,方便大家看问题。 https://jsfiddle.net/8xutoea5/
编辑:添加了更多 CSS 代码。
编辑:我解决了这个问题。感谢大家花时间解决这个问题。
这似乎是您 CSS 中其他地方的边距,此处未提供,这是导致问题的原因。您可以尝试设置 margin-bottom:0;
几个地方,看看是否可以解决问题。
#nav nav ul li {
margin-bottom: 0;
padding-bottom: 0;
}
#nav nav ul li a {
margin-bottom: 0;
}
或提供您的其余样式sheet。
是"External Store"段造成的,太长了,绕行了。您可以缩短它或强制使用 "no-wrap".
css 属性 white-space
的值为 nowrap
.
您的内部标签上没有 classes,因此我建议将 class 添加到 a
标签并向其添加规则。
a.listLink{
white-space: nowrap;
}
或者您可以只向该特定标签添加样式属性,但我觉得该解决方案不够酷。
这很可能是因为您的 ul
为 inline-block
,这将导致一些 space 元素之后。此外,您的 li
漂浮在导航上,但没有明确修正。
您可以通过多种方式解决这个问题,这里是您可以使用 display: flex;
并删除 li
上的 float: left;
的方法。这是一个非常坚实的基础,您可以通过更改 justify-content
属性 来构建。您可以在 css-tricks.com
找到很棒的 flexbox 指南
更新
我对 fiddle 进行了一些更新以解决该问题。请注意顶部的代码,我还做了一些其他更改。有什么不明白的地方写
* {
box-sizing: border-box;
}
#nav ul {
list-style-type: none;
margin: 0;
background-color: #1C86EE;
padding: 0;
position: relative;
width: 100%;
bottom: 0;
display: flex;
justify-content: space-between;
}
#nav li {
width: 14%;
}
我目前正在尝试制作一个网站,并且设法创建了一个导航栏。但是,我的导航栏之间有一个奇怪的“额外”space。一开始它工作正常,但在我引用 JQuery 之后。但是,我怀疑 JQuery 是导致问题的原因,因为即使我将其删除,该网站看起来还是一样。 Here's a screenshot of the problem. I'm trying to get rid of the circled part.
我的HTML代码:
<div id="nav"> <!-- Navigation Bar -->
<nav>
<ul>
<li><a class="selected" href="#">Home</a></li>
<li><a href="about.html">About Me</a></li> <!-- Link to about page -->
<li class="drop">
<a href="games.html" class="dropbtn">Games</a>
<div class="drop-content">
<a href="rfg.html">Red Faction: Guerrilla</a> <!-- Contains links to the respective pages -->
<a href="wots3.html">Way of the Samurai 3</a>
<a href="singularity.html">Singularity</a>
</div>
</li>
<li><a href="reviews.html">Reviews</a></li>
<li><a href="external.html">External Store</a></li> <!-- Link to external site -->
<li><a href="video.html">Videos</a></li> <!-- Contains links to Youtube -->
<li><a href="feedback.html">Feedback</a></li> <!--Feedback form-->
这是样式表:
#nav nav{ /* Navigation bar*/
padding-top: 5%;
margin-bottom: 5% }
#nav ul {
list-style-type: none;
margin: 0;
background-color: #1C86EE;
padding: 0;
position: relative;
width: 100%;
bottom: 0;
display: inline-block }
#nav li {
float: left;
width: 14% }
li a, .dropbtn {
display: inline-block;
color: #FFFFFF;
text-align: center;
padding-left: 15%;
padding-right: 15%;
text-decoration: none }
.selected {
background-color: #6CCC0A;
padding-right: 25% }
li a:hover, .drop:hover .dropbtn {
background-color: #BFA810 }
a {
padding-top: 1em;
padding-bottom: 1em }
li.drop {
display: inline-block }
.drop-content {
display: none;
position: absolute;
background-color: #970707;
min-width: 14%;
box-shadow: 0px 8px 16px 0px #000000;
z-index: 100 }
.drop-content a {
color: #FFFFFF;
padding: 15px 20px;
display: block;
text-align: left }
.drop-content a:hover {
background-color: #02BBC4 }
.drop:hover .drop-content {
display: block;
border: 1px solid black;
position: absolute } /*End of navigation*/
* { /* index style */
box-sizing:border-box
}
body {
font-family: Verdana,sans-serif;
margin:0
}
.slideshow-fade {
display:none
}
/* Slideshow container */
.slideshow {
max-width: 100%;
position: relative;
margin: auto;
}
.slideshow img {
opacity: 0.7
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 1em;
margin-top: -1em;
color: #FFFFFF;
font-weight: bold;
font-size: 1.5em;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #000000;
font-size: 1.3em;
padding: 1% 2%;
position: absolute;
bottom: 2%;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.number {
color: #000000;
font-size: 1em;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 1em;
width: 4%;
margin: 0 2px;
background-color: #bbbbbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width : 720px ){
.prev, .next,.text {font-size: 11px}
}
.slideshow-fade a {
color: #FFFFFF
}
.slideshow-fade a:visited {
color: #5115D0
} /*End of style for index*/
footer { /*Footer*/
background: #000000;
width: 100%;
height: 5%;
position: relative;
bottom: 0;
left: 0;
padding-top: 2%;
overflow: hidden;
}
footer p {
font-family: arial, calibri, sans-serif;
color: #FFFFFF;
text-align: center;
padding-bottom: 2%;
} /*End of footer*/
任何帮助将不胜感激。谢谢。
这里放上jsfiddle文件,方便大家看问题。 https://jsfiddle.net/8xutoea5/
编辑:添加了更多 CSS 代码。 编辑:我解决了这个问题。感谢大家花时间解决这个问题。
这似乎是您 CSS 中其他地方的边距,此处未提供,这是导致问题的原因。您可以尝试设置 margin-bottom:0;
几个地方,看看是否可以解决问题。
#nav nav ul li {
margin-bottom: 0;
padding-bottom: 0;
}
#nav nav ul li a {
margin-bottom: 0;
}
或提供您的其余样式sheet。
是"External Store"段造成的,太长了,绕行了。您可以缩短它或强制使用 "no-wrap".
css 属性 white-space
的值为 nowrap
.
您的内部标签上没有 classes,因此我建议将 class 添加到 a
标签并向其添加规则。
a.listLink{
white-space: nowrap;
}
或者您可以只向该特定标签添加样式属性,但我觉得该解决方案不够酷。
这很可能是因为您的 ul
为 inline-block
,这将导致一些 space 元素之后。此外,您的 li
漂浮在导航上,但没有明确修正。
您可以通过多种方式解决这个问题,这里是您可以使用 display: flex;
并删除 li
上的 float: left;
的方法。这是一个非常坚实的基础,您可以通过更改 justify-content
属性 来构建。您可以在 css-tricks.com
更新
我对 fiddle 进行了一些更新以解决该问题。请注意顶部的代码,我还做了一些其他更改。有什么不明白的地方写
* {
box-sizing: border-box;
}
#nav ul {
list-style-type: none;
margin: 0;
background-color: #1C86EE;
padding: 0;
position: relative;
width: 100%;
bottom: 0;
display: flex;
justify-content: space-between;
}
#nav li {
width: 14%;
}