html - Flexbox 重叠
问题描述
我正在尝试使用 flexbox 构建导航栏,但在较小的屏幕上内容重叠,我知道媒体查询可以避免这种情况,但我试图在不使用这些媒体查询的情况下找到解决方案。
body {
margin: 0;
padding: 0;
}
/* start of nav */
nav {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 1em;
}
.search-box {
display: flex;
align-items: stretch;
border-radius: 0.5em;
height: 1.75em;
width: 30%;
}
.search-box input {
border: none;
background: transparent;
outline: none;
border: 1px solid #00b8e6;
border-right: none;
border-radius: 0.5em 0 0 0.5em;
padding: 0.25em;
flex: 1;
}
.fa-search {
display: flex;
align-items: center;
cursor: pointer;
border: 1px solid #00b8e6;
border-left: none;
border-radius: 0 0.5em 0.5em 0;
padding: 0.25em 0.2em;
color: #00ccff;
}
.fa-search:hover {
background: #ccf5ff;
}
.nav-login-btns {
border: 1px solid black;
display: flex;
justify-content: space-between;
width: 17%;
font-size: 1.2em;
}
.nav-login-btns a {
text-decoration: none;
}
/* end of nav */
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://kit.fontawesome.com/8370b7a799.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="styles.css" />
<title>Ecommerce</title>
</head>
<body>
<nav class="nav">
<i class="fas fa-bars"></i>
<img src="/img/logo.png" alt="" />
<div class="search-box">
<input type="text" />
<i class="fas fa-search"></i>
</div>
<div class="nav-login-btns">
<a href="#">Sign In</a>
<a href="#">Login</a>
</div>
<i class="fas fa-shopping-cart"></i>
</nav>
</body>
</html>
我怎样才能避免这种情况?我期待像一个新的行可能?我也尝试了 flex-wrap: wrap 但仍然没有结果。它与最小宽度有关吗?
解决方案
只需将 .search-box 宽度更改为 min-width 并将 flex-wrap 应用于 nav 元素。问题是设置为搜索框的宽度比实际宽度短。
推荐阅读
- python - OSError: [WinError 998] 使用 pyinstaller 时对内存位置的访问无效
- reactjs - 为什么 React 应用程序不从 AWS S3 存储桶中获取图像?
- java - 访问 Thymeleaf 模板中的变量并将它们原封不动地发送回控制器
- xamarin.ios - 在哪里下载旧版本的 Xamarin.iOS?在这种特定情况下,Xamarin.iOS 14.8.0.3
- javascript - 如何将 JavaScript 中的当前 URL 发送到第二个 PHP 页面?
- jsgrid - 突出显示新添加的行 - jsGrid
- php - PHP 代码在标头位置完成之前运行
- python-3.x - 具有自适应解决方案的 scipy solve_ivp
- java - Mapstruct 继承@Mappings
- svelte - 如何通过第三方 Svelte 组件转发事件?