html - 更改标题字体大小会使 div 不对齐

标签 html css

<分区>

我最近开始做前端,但遇到了一些问题。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.header {
  background: white;
  padding: 0 10%;
  height: 40px;
}

.head-title {
  display: inline-block;
  text-align: center;
  font-size: 1.3em; /* Changing this to 1em fixes the problem */
  width: 260px;
  height: 40px;
  margin: 0 10px;
}

.head-button {
  display: inline-block;
  width: 80px;
  height: 40px;
  background-color: red;
}

body {
  background-color: #24272E;
}
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <div class=header>
      <div class="head-title"><p>Amazing Title</p></div>
      <div class="head-button"><p>Foo</p></div>
      <div class="head-button"><p>Bar</p></div>
    </div>
  </body>
</html>

您可以运行代码 here .问题在于将红色按钮与导航栏 (header) 对齐。这些按钮应该从导航栏的顶部延伸到底部,但它们没有与顶部对齐。这是由 head-title 元素引起的。如果字体大小设置为 1em,那么问题就消失了。为什么会这样?感谢您的帮助。

最佳答案

.head-button 上设置 vertical-align: top;:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.header {
  background: white;
  padding: 0 10%;
  height: 40px;
}

.head-title {
  display: inline-block;
  text-align: center;
  font-size: 1.3em;
  /* Changing this to 1em fixes the problem */
  width: 260px;
  height: 40px;
  margin: 0 10px;
}

.head-button {
  display: inline-block;
  width: 80px;
  height: 40px;
  background-color: red;
  vertical-align: top;
}

body {
  background-color: #24272E;
}
<div class=header>
  <div class="head-title">
    <p>Amazing Title</p>
  </div>
  <div class="head-button">
    <p>Foo</p>
  </div>
  <div class="head-button">
    <p>Bar</p>
  </div>
</div>

关于html - 更改标题字体大小会使 div 不对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590986/

上一篇:html - 联系人部分不会集中

下一篇:javascript - Internet Explorer 呈现问题。 (HTML CSS JS)

相关文章:

javascript - DOM:使用 onclick 事件中新创建的删除按钮删除新创建的 'article' 元素?

html - 段落内的 anchor 内的图形 : is it legal in HTML5?

javascript - Angular 2 : Programmatically to position inside div overlay

javascript - 获取 HTML 元素

html - 向动态创建列的表添加水平滚动

css - 如何使用CSS删除左上角未登录用户的Wordpress图标

html - 导航栏的位置 div

html - 结合 :nth-child with :not() selectors

javascript - jQuery 克隆元素并避免采用父节点

html - 为什么单击图标时导航栏不会折叠/展开?