html - 如何强制我的导航和按钮保持恒定高度?
问题描述
我有以下代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/*header*/
.header {
padding: 2.5vw;
position: relative;
background: linear-gradient(to right, #EE7752 25%, #E73C7E 50%, #23A6D5 75%, #23D5AB 100%);
background-size: 200% 200%;
animation: Gradiente 15s ease infinite;
font-size:15px;
}
@keyframes Gradiente {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
.header p {
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
/*navbar*/
.flex {
display: flex;
flex-direction: row;
width: 100%;
}
.option {
display: flex;
width: 24.7%;
color: white;
background-color: grey;
justify-content: center;
align-items: center;
height: 4vw;
margin-left:0.25vw;
}
#menu{
display:none;
}
</style>
</head>
<body>
<section class="container1">
<div class="header">
<h1><p>Examen B 2DAW</p></h1>
</div>
<nav>
<div class="flex">
<div id="menu">Menú</div>
<div class="option"><a href="#"></a>Opcion 1</div>
<div class="option"><a href="#"></a>Opcion 2</div>
<div class="option"><a href="#"></a>Opcion 3</div>
<div class="option"><a href="#"></a>Opcion 4</div>
</div>
</nav>
</section>
</body>
</html>
收缩时它的高度会降低,这是我不想发生的事情,因为我希望它保持恒定的高度
这就是我的看起来缩小的样子
但它应该看起来像这样(保持它的高度),我尝试用 px 和 vw 设置高度,但它没有任何区别。我尝试使用不同的字体大小,但我无法解决这个问题。我需要它保持高度,同时水平变小,为什么没有发生?
解决方案
我认为问题在于您的填充设置为视口宽度(vw),这将相应地改变值。如果您希望高度是静态的,请使用 px 作为填充或高度。如果需要,您可以同时使用高度和填充,但请记住,两者会相加。
.header {
padding-top: 20px;
padding-bottom: 20px;
position: relative;
background: linear-gradient(to right, #EE7752 25%, #E73C7E 50%, #23A6D5 75%, #23D5AB 100%);
background-size: 200% 200%;
animation: Gradiente 15s ease infinite;
font-size: 15px;
}
或者
.header {
height: 40px;
position: relative;
background: linear-gradient(to right, #EE7752 25%, #E73C7E 50%, #23A6D5 75%, #23D5AB 100%);
background-size: 200% 200%;
animation: Gradiente 15s ease infinite;
font-size: 15px;
}
推荐阅读
- r - 表 R Markdown qwraps2 和 knitr::kable。缺少列
- macos - aws2 命令在写入 .command 文件并使用 crontab 计划时不起作用
- android - android ANR BillingClient.queryPurchases
- jquery - 重力形式的 Wordpress 水平字段
- c# - 如何修复 C# 中 XPathDocument 使用中的 XXE 漏洞?
- python - 我想将 db 句柄从我的 perl 脚本传递给我的 python 脚本
- reactjs - 在 react 中将字符串转换为 jsx 字符串 | 如何在反应中将 Unicode 转义序列转换为 Unicode 字符
- c++ - 有没有办法在 jsoncpp 中使用变量作为键?
- angular - Angular 8 侧面导航以加载具有不同数据的相同组件
- python - Python - 关闭所有活动文档而不保存