html - 我在制作按钮时遇到问题

标签 html css button

我正在通过制作一个网站来练习我的 HTML,我正在制作一个带有按钮的标题。 我试图使按钮达到标题的整个高度,但由于某种原因它超出了标题,并且没有到达顶部。

#header {
  background-color: #1564B3;
  color: #fff;
  height: 70px;
  position: fixed;
  width: 100%;
  top: 0%;
  left: 0%;
}
#header-a {
  width: 100px;
  background-color: #555555;
  display: inline-block;
  text-align: center;
  margin-top: 0px;
  height: 100%;
}
#header-h {
  display: inline-block;
  margin-top: 20px;
}
<div id="header">
  <h2 id="header-h">Header text</h2>
  <div id="header-a">
    <a href="index.html">Home</a>
  </div>
</div>

最佳答案

您可以在需要时重置 inline-block 元素上的 vertical-align(默认为基线) 值。这里 vertical-align:top; 会很好:

#header {
  background-color: #1564B3;
  color: #fff;
  height: 70px;
  position: fixed;
  width: 100%;
  top: 0%;
  left: 0%;
}
#header-a {
  width: 100px;
  background-color: #555555;
  display: inline-block;
  text-align: center;
  margin-top: 0px;
  height: 100%;
  vertical-align:top;
}
#header-h {
  display: inline-block;
  margin-top: 20px;
}
<div id="header">
  <h2 id="header-h">Header text</h2>
  <div id="header-a">
    <a href="index.html">Home</a>
  </div>
</div>

对于覆盖 div 的 a,您还可以使用高度或最终使用行高:

#header {
  background-color: #1564B3;
  color: #fff;
  height: 70px;
  position: fixed;
  width: 100%;
  top: 0%;
  left: 0%;
}
#header-a {
  width: 100px;
  background-color: #555555;
  display: inline-block;
  text-align: center;
  margin-top: 0px;
  height: 100%;
  vertical-align:top;
}
#header-a a {
  display:block;
  line-height:70px;/* will size it up to 70px height for each line */
  }
#header-h {
  display: inline-block;
  margin-top: 20px;
}
<div id="header">
  <h2 id="header-h">Header text</h2>
  <div id="header-a">
    <a href="index.html">Home</a>
  </div>
</div>

关于html - 我在制作按钮时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40490819/

相关文章:

javascript - 使用 Firestore 显示简单的博客文章

javascript - 跳过单页站点的导航链接

html - 为什么 django 会在我的 html 文件中插入填充和字符?

c# - "Disabled"对图像的影响

html - 在包装器 div 中居中放置 3 个 float div

javascript - MutationObserver 仅在所有资源已加载时回调

css - 将 Sass 用于动态颜色

jquery - 根据该 anchor jquery中的特定文本向 anchor 添加一个类

c# - C# WPF 代码隐藏中的按钮处理程序

css - 按钮边框半径和光标