html - 垂直对齐到父级的中间

标签 html css

我试图将 2 个内联 block 元素垂直对齐到其父元素的中间。不幸的是,当我给两个元素 vertical-align: middle 时,其中一个稍微对齐到中间,而另一个则没有。这是我的 jsFiddle .

HTML

CSS

html,
body {
  margin: 0;
}

.navContainer {
  width: 100%;
  height: 60px;
  background-color: white;
  box-shadow: 0 0px 10px 0 rgba(0, 0, 0, 0.2), 0 0px 10px 0 rgba(0, 0, 0, 0.19);
}

.navBar {
  width: 90%;
  display: block;
  margin: 0 auto;
  height: 60px;
  line-height: 60px;
}


.lineMenu {
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
}

.bar1,
.bar2,
.bar3 {
  width: 35px;
  height: 5px;
  background-color: #333;
  margin: 6px 0;
  transition: 0.4s;
}

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {
  opacity: 0;
}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
}

.logo {
  display: inline-block;
  width: 120px;
  vertical-align: middle;
  margin-left: 35px;
}

.logo img {
  width: 100%;
}

如何使两个元素都在其父元素的中间对齐?

最佳答案

你会踢自己的答案太简单了;)

您需要做的就是将 .logo img 设置为 display: block:

.logo img {
  display: block;
}

html,
body {
  margin: 0;
}

.navContainer {
  width: 100%;
  height: 60px;
  background-color: white;
  box-shadow: 0 0px 10px 0 rgba(0, 0, 0, 0.2), 0 0px 10px 0 rgba(0, 0, 0, 0.19);
}

.navBar {
  width: 90%;
  display: block;
  margin: 0 auto;
  height: 60px;
  line-height: 60px;
}


/*Line menu CSS*/

.lineMenu {
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
}

.bar1,
.bar2,
.bar3 {
  width: 35px;
  height: 5px;
  background-color: #333;
  margin: 6px 0;
  transition: 0.4s;
}

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {
  opacity: 0;
}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
}

.logo {
  display: inline-block;
  width: 120px;
  vertical-align: middle;
  margin-left: 35px;
}

.logo img {
  width: 100%;
  display: block;
}
<div class='navContainer'>
  <div class='navBar'>
    <div class="lineMenu" onclick="menuToggle(this)">
      <div class="bar1"></div>
      <div class="bar2"></div>
      <div class="bar3"></div>
    </div>
    <div class='logo'>
      <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/2000px-Google_2015_logo.svg.png'>
    </div>
  </div>
</div>

我创建了一个工作 fiddle 来展示这个 here .

希望对您有所帮助! :)

关于html - 垂直对齐到父级的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616728/

相关文章:

javascript - HTML5 Canvas 笔划宽度不会增量变化

android - 在 Android 手机中以 OGG 或 MP3 格式录制语音

html - 垂直对齐 : middle - why must the first item be aligned middle?

css - 文本大小取决于 CSS 中的窗口宽度

html - jRecorder 未显示在页面上

php - sifr 在浏览器中不工作,但可以在 Dreamweaver 预览中看到它

Javascript 元素标签名称未定义

html - 如何在 html 中将 <img> 正好放在另一个 <img> 下?

html - 在嵌套列表中定位 html 元素

html - 如何添加自定义图像背景以跨越 Bootstrap 按钮?