【发布时间】:2018-11-15 08:34:52
【问题描述】:
过去两天我一直在制作一个非常简单的导航栏,里面只有几个按钮,其中一个应该显示一个下拉菜单。我已经用 % 设置了我的所有填充,并且在大多数情况下它们都可以工作,除了包含显示下拉菜单的按钮的 div。
/* all fonts are using vw units as it makes it easier to spot the phantom top-padding */
header {
background-color: #101010;
}
header h1 {
color: #ffffff;
margin: 0px;
text-align: center;
font-family: "Barlow Condensed";
font-size: 7vw;
padding: 1%;
}
body {
margin: 0px;
background-color: #f0f0f0
}
ul, ol {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
content {
width: 100%;
clear: both;
}
nav {
background-color: #999999;
overflow: hidden;
font-family: "Barlow condensed";
outline: none;
border: 0px;
}
.dropdown:hover, .dropbtn {
background-color: red;
}
/* This contains the dropdown */
.dropdown {
float: left;
overflow: hidden;
padding: 1.5%;
}
/* This is the button */
.dropdown .dropbtn {
font-size: 1.2vw;
border-style: none;
border-width: 0px;
outline: none;
color: white;
padding: 1.5%;
background-color: inherit;
font-family: inherit;
margin: 0%;
}
/* Dropdown contents */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 10%;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
margin-top: 1.5%;
margin-left: -1.5%;
}
/* links inside the dropdown */
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
float: none;
font-size: 1.1vw;
padding: 2%;
}
/* background color for links inside the dropdown (hover) */
.dropdown-content a:hover {
background-color: #ddd;
}
/* shows the dropdown on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Other buttons in the nav bar, working as intended. */
.botons {
float: left;
font-size: 1.2vw;
border-style: none;
border-width: 0px;
outline: none;
color: white;
padding: 1.5%;
background-color: inherit;
font-family: inherit;
margin: 0;
text-decoration: none;
}
.botons a {
float: left;
font-size: 1.2vw;
border-style: none;
border-width: 0px;
outline: none;
color: white;
background-color: inherit;
font-family: inherit;
margin: 0;
text-decoration: none;
}
.botons:hover {
float: left;
font-size: 1.2vw;
border-style: none;
border-width: 0px;
outline: none;
color: white;
padding: 1.5%;
background-color: red;
font-family: inherit;
margin: 0;
text-decoration: none;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="estil-index.css">
<link href='https://fonts.googleapis.com/css?family=Barlow Condensed' rel='stylesheet'>
<link rel="icon" type="image/png" href="logo.png"
<meta charset="UTF-8">
<meta name="viewport" content="widht=devide-width, initial-scale=1">
</head>
<body>
<header><h1>Big header text</h1></header>
<content>
<nav>
<button class="botons"><a href="#">Home</a></button>
<div class="dropdown">
<button class="dropbtn">Dropdown
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
<a href="#">Link 4</a>
</div>
</button>
</div>
<button class="botons"><a href="#">Regular button</a></button>
<button class="botons"><a href="#">Button</a></button>
<button class="botons"><a href="#">Button</a></button>
</nav>
</body>
如您所见,.dropdown div(或者可能是里面的按钮)的行为不正常,而不是像我指定的 everywhere 那样有 % 填充,它似乎只有 px 填充在顶部,随着屏幕变小或放大,它的大小会增加。
我已经将代码发送给了几个和我一样难过的同行和朋友,尤其是幻像填充只出现在顶部这一事实。
【问题讨论】:
-
将
line-height: 0;添加到该按钮 -
在 css 中的
dropdown类中提供line-height:0为我修复了它。不过不要问我为什么:)