我目前正在开发一个旧项目中的旧导航栏。我遇到了一个问题,我真的不明白为什么会发生这种情况。导航栏水平溢出,“Iniciar Sesion”和“Registrarse”由于溢出而不可见。这是代码:
@font-face {
font-family: 'HelveticaDisplay';
src: url('/media/fonts/HelveticaNowDisplay-Black.otf') format('opentype');
}
* {
font-family: "HelveticaDisplay", sans-serif;
}
body {
max-width: 100%;
color: #181818;
margin: 0;
}
/*HEADER*/
nav {
display: flex;
justify-content: space-around;
align-items: center;
z-index: 1;
padding: 15px 150px;
transition: 0.6s;
width: 100%;
position: fixed;
background-color: #FFFFFF;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
}
nav.sticky {
padding: 20px 6%;
background: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
}
.logo {
color: rgb(17, 17, 17);
width: 180px;
padding: 0;
display: flex;
justify-content: center; /* Centrar horizontalmente */
align-items: center; /* Centrar verticalmente */
}
.logo img {
max-height: 100%;
width: 150px;
object-fit: contain;
}
.nav-links {
position: relative;
display: flex;
justify-content: center;
align-items: center;
padding: 0;
}
.nav-links li {
list-style: none;
transition: all 0.3s ease 0s;
padding: 0 5px;
text-align: center;
}
.nav-links a {
color: rgb(17, 17, 17);
text-decoration: none;
font-size: 15px;
text-transform: uppercase;
white-space: nowrap;
transition: all 0.3s ease 0s;
}
.nav-links a:hover {
color: #7feffd;
}
.burger {
display: none;
}
.burger div{
width: 25px;
height: 3px;
background-color:rgb(17, 17, 17);
margin: 5px;
transition: all 0.3s ease;
}
/* BUSQUEDA */
.box {
width: 100%;
background-color: #F6F6F6;
border: 1px solid #AAAAAA;
display: flex;
align-items: center;
justify-content: space-between;
margin: 2%;
margin-top: 0;
margin-bottom: 0;
transition: all 0.3s ease 0s;
padding: 0 0 0 15px;
box-sizing: border-box;
}
nav.sticky .box{
width: 100%;
background-color: rgb(236, 236, 236);
border: none;
border-radius: 30px;
display: flex;
align-items: center;
padding-left: 20px;
margin: 4%;
margin-top: 0;
margin-bottom: 0;
transition: all 0.3s ease 0s;
}
.box input {
background-color: transparent;
flex-grow: 1;
height: 40px;
border: none;
outline: none;
font-size: 14px;
}
.nav-search-btn {
background: none;
border: none;
line-height: 1em;
-webkit-border-radius: 0 2px 2px 0;
width: 42px;
height: 42px;
border-radius: 0 2px 0;
right: 0;
top: 0;
bottom: 0;
cursor: pointer;
background-color: transparent;
box-shadow: none;
}
<nav id="navbarxd">
<a href="#" class="logo">
<img src="media/images/icons/logoextendido.png" alt="">
</a>
<div class="box">
<input type="text" name="" placeholder="Buscar productos, marcas, etc.">
<button type="submit" class="nav-search-btn">
<i class="bi-search" style="color: #AAAAAA;"></i>
</button>
</div>
<ul class="nav-links">
<li>
<a href="#">Iniciar Sesión</a>
</li>
<li>
<a href="#">Registrarse</a>
</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
我尝试过修改 Flexbox、宽度、高度,什么也没有。
您的
150px
元素上有 nav
左右填充,并使用开发工具显示。这会导致flexbox内容太大。删除那个,它基本上已经修复了。请记住,左侧的空白区域已被图像占据,我已将其替换为本演示的示例图像。
弹性盒内的项目上还有一个
margin: 2%
。相反,请在 gap: 2%
上使用 nav
,以便设置所有 Flexbox 项目之间的空间。
@font-face {
font-family: 'HelveticaDisplay';
src: url('/media/fonts/HelveticaNowDisplay-Black.otf') format('opentype');
}
* {
font-family: "HelveticaDisplay", sans-serif;
}
body {
max-width: 100%;
color: #181818;
margin: 0;
}
/*HEADER*/
nav {
display: flex;
justify-content: space-around;
align-items: center;
z-index: 1;
/*padding: 15px 150px;*/
padding: 15px 0;
gap: 2%;
transition: 0.6s;
width: 100%;
position: fixed;
background-color: #FFFFFF;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
}
nav.sticky {
padding: 20px 6%;
background: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
}
.logo {
color: rgb(17, 17, 17);
width: 180px;
padding: 0;
display: flex;
justify-content: center; /* Centrar horizontalmente */
align-items: center; /* Centrar verticalmente */
}
.logo img {
max-height: 100%;
width: 150px;
object-fit: contain;
}
.nav-links {
position: relative;
display: flex;
justify-content: center;
align-items: center;
padding: 0;
}
.nav-links li {
list-style: none;
transition: all 0.3s ease 0s;
padding: 0 5px;
text-align: center;
}
.nav-links a {
color: rgb(17, 17, 17);
text-decoration: none;
font-size: 15px;
text-transform: uppercase;
white-space: nowrap;
transition: all 0.3s ease 0s;
}
.nav-links a:hover {
color: #7feffd;
}
.burger {
display: none;
}
.burger div{
width: 25px;
height: 3px;
background-color:rgb(17, 17, 17);
margin: 5px;
transition: all 0.3s ease;
}
/* BUSQUEDA */
.box {
width: 100%;
background-color: #F6F6F6;
border: 1px solid #AAAAAA;
display: flex;
align-items: center;
justify-content: space-between;
/*margin: 2%;*/
margin-top: 0;
margin-bottom: 0;
transition: all 0.3s ease 0s;
padding: 0 0 0 15px;
box-sizing: border-box;
}
nav.sticky .box{
width: 100%;
background-color: rgb(236, 236, 236);
border: none;
border-radius: 30px;
display: flex;
align-items: center;
padding-left: 20px;
margin: 4%;
margin-top: 0;
margin-bottom: 0;
transition: all 0.3s ease 0s;
}
.box input {
background-color: transparent;
flex-grow: 1;
height: 40px;
border: none;
outline: none;
font-size: 14px;
}
.nav-search-btn {
background: none;
border: none;
line-height: 1em;
-webkit-border-radius: 0 2px 2px 0;
width: 42px;
height: 42px;
border-radius: 0 2px 0;
right: 0;
top: 0;
bottom: 0;
cursor: pointer;
background-color: transparent;
box-shadow: none;
}
<nav id="navbarxd">
<a href="#" class="logo">
<img src="https://picsum.photos/150/50" alt="">
</a>
<div class="box">
<input type="text" name="" placeholder="Buscar productos, marcas, etc.">
<button type="submit" class="nav-search-btn">
<i class="bi-search" style="color: #AAAAAA;"></i>
</button>
</div>
<ul class="nav-links">
<li>
<a href="#">Iniciar Sesión</a>
</li>
<li>
<a href="#">Registrarse</a>
</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
您的
<nav>
的宽度为 100%,左侧和右侧的内边距均为 150 像素。所以你的导航溢出了 300px。您可以通过更改宽度和填充设置来解决此问题,以便它们不会超出屏幕宽度。
一个简单的解决方案是在
<nav>
上使用 VW(请参阅 CSS 值)。
在下面编辑的示例中,导航宽度设置为 90vw 而不是 100%,左右填充为 5vw,通过
padding: 15px 5vw;
。你最终会达到 100vw,所以不会溢出。
@font-face {
font-family: 'HelveticaDisplay';
src: url('/media/fonts/HelveticaNowDisplay-Black.otf') format('opentype');
}
* {
font-family: "HelveticaDisplay", sans-serif;
}
body {
max-width: 100%;
color: #181818;
margin: 0;
}
/*HEADER*/
nav {
display: flex;
justify-content: space-around;
align-items: center;
z-index: 1;
padding: 15px 5vw;
/*Changed here*/
transition: 0.6s;
width: 90vw;
/*Changed here*/
position: fixed;
background-color: #FFFFFF;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
}
nav.sticky {
padding: 20px 6%;
background: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
}
.logo {
color: rgb(17, 17, 17);
width: 180px;
padding: 0;
display: flex;
justify-content: center;
/* Centrar horizontalmente */
align-items: center;
/* Centrar verticalmente */
}
.logo img {
max-height: 100%;
width: 150px;
object-fit: contain;
}
.nav-links {
position: relative;
display: flex;
justify-content: center;
align-items: center;
padding: 0;
}
.nav-links li {
list-style: none;
transition: all 0.3s ease 0s;
padding: 0 5px;
text-align: center;
}
.nav-links a {
color: rgb(17, 17, 17);
text-decoration: none;
font-size: 15px;
text-transform: uppercase;
white-space: nowrap;
transition: all 0.3s ease 0s;
}
.nav-links a:hover {
color: #7feffd;
}
.burger {
display: none;
}
.burger div {
width: 25px;
height: 3px;
background-color: rgb(17, 17, 17);
margin: 5px;
transition: all 0.3s ease;
}
/* BUSQUEDA */
.box {
width: 100%;
background-color: #F6F6F6;
border: 1px solid #AAAAAA;
display: flex;
align-items: center;
justify-content: space-between;
margin: 2%;
margin-top: 0;
margin-bottom: 0;
transition: all 0.3s ease 0s;
padding: 0 0 0 15px;
box-sizing: border-box;
}
nav.sticky .box {
width: 100%;
background-color: rgb(236, 236, 236);
border: none;
border-radius: 30px;
display: flex;
align-items: center;
padding-left: 20px;
margin: 4%;
margin-top: 0;
margin-bottom: 0;
transition: all 0.3s ease 0s;
}
.box input {
background-color: transparent;
flex-grow: 1;
height: 40px;
border: none;
outline: none;
font-size: 14px;
}
.nav-search-btn {
background: none;
border: none;
line-height: 1em;
-webkit-border-radius: 0 2px 2px 0;
width: 42px;
height: 42px;
border-radius: 0 2px 0;
right: 0;
top: 0;
bottom: 0;
cursor: pointer;
background-color: transparent;
box-shadow: none;
}
<nav id="navbarxd">
<a href="#" class="logo">
<img src="media/images/icons/logoextendido.png" alt="">
</a>
<div class="box">
<input type="text" name="" placeholder="Buscar productos, marcas, etc.">
<button type="submit" class="nav-search-btn"><i class="bi-search" style="color: #AAAAAA;"></i></button>
</div>
<ul class="nav-links">
<li>
<a href="#">Iniciar Sesión</a>
</li>
<li>
<a href="#">Registrarse</a>
</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
请随意进一步研究该解决方案 在 codepen 上
当您使用较小的屏幕尺寸时,导航中的其他元素将再次开始导致溢出问题,您可能希望查看响应式设计