【问题标题】:One of my divs is haunted by a phantom top padding, seemingly ignoring my css file我的一个 div 被幻影顶部填充所困扰,似乎忽略了我的 css 文件
【发布时间】: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 为我修复了它。不过不要问我为什么:)

标签: html css


【解决方案1】:

解释起来有点棘手,但基本上这是因为按钮没有像a 元素那样浮动,并且默认的line-height 定义了行框的高度,当字体时会更大-size 变小。所以 phatom padding 是由line-height 定义的行框的高度。

一个简单的修复它使按钮像链接一样浮动,因此它将是一个块级元素,而不是一个内联级元素。

/* 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%;
  float: left;
}


/* 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;
}
<link href='https://fonts.googleapis.com/css?family=Barlow Condensed' rel='stylesheet'>

<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>

或者减少line-height并将vertical-align调整到顶部:

/* 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%;
  line-height: 0;
}


/* 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%;
  vertical-align:top;
}


/* 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;
}
<link href='https://fonts.googleapis.com/css?family=Barlow Condensed' rel='stylesheet'>

<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>

【讨论】:

  • 我认为这或多或少是正确的。另一种选择是将font-size:1.2vw从按钮移动到其div容器,并将按钮的行高设置为inherit,以便div的支柱的行高与按钮的行高保持相同.
猜你喜欢
  • 2016-12-05
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2016-09-30
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
相关资源
最近更新 更多