html - 一个元素的垂直对齐方式根据相邻元素的内容而变化

标签 html css

这是我无法重新创建的内容:enter image description here

link to jsfiddle for reference

这是我遇到的问题,“BBC”似乎会根据下一个 div block 中是否包含登录而上下跳动。我无法让边框保持在 40 像素以内,也无法让“登录”或“BBC”文本居中或 BBC 居中。我花了几个小时测试各种元素,我无法让它工作——如果你能解释代码和正在发生的行为(为什么 HTML 呈现它的方式),我将不胜感激).为什么 BBC 和 Sign In 的左边框会溢出 40px 高度?

html, body, p {
  margin: 0;
  padding: 0;
}
.container {
  width: 1000px;
  margin: 0 auto;
}
.top-nav-bar {
  height: 40px;
  border-bottom: 1px firebrick solid;
  margin-bottom: 30px;
}
.top-nav-logo-area {
  display: inline-block;
  padding-right: 25px;
}
.logo {
  background-color: black;
  color: white;
  font-family: monospace;
  font-size: 25px;
}
.top-nav-link-div {
  display: inline-block;
  border-left: 1px #cccccc solid;
  height: 40px;
}
<!-- WITH CONTENT NEXT TO LOGO -->

<div class="container top-nav-bar">

  <div class="top-nav-logo-area">
    <span class="logo">B</span>
    <span class="logo">B</span>
    <span class="logo">C</span>
  </div>

  <div class="top-nav-link-div">
    hello
  </div>
</div>


<!--WITHOUT CONTENT NEXT TO LOGO -->

<div class="container top-nav-bar">

  <div class="top-nav-logo-area">
    <span class="logo">B</span>
    <span class="logo">B</span>
    <span class="logo">C</span>
  </div>

  <div class="top-nav-link-div">
    <!-- empty -->

  </div>
</div>

最佳答案

您正在处理 inline-block 元素。

适用于行内级元素的 vertical-align 属性的默认值为 baseline。这采用 Logo 容器 (.top-nav-logo-area) 并将其基线(或底部边距边缘,如果没有基线)与父框的基线 ( .top-nav-bar).

除此之外,您已经使用 height: 40px 限制了父级的高度(自然高度为 46px)。

这会强制 Logo 框溢出父级的底部。

无论父级的高度是否缩短,vertical-align 属性都应该在 Logo 容器上起作用。

来自规范:

vertical-align

This property affects the vertical positioning inside a line box of the boxes generated by an inline-level element.

baseline

Align the baseline of the box with the baseline of the parent box. If the box does not have a baseline, align the bottom margin edge with the parent's baseline.

middle

Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.

* { box-sizing: border-box; }

html, body, p {
    margin: 0;
    padding: 0;
}

.container {
    width: 1000px;
    margin: 0 auto;
}

.top-nav-bar {
    height: 40px;             /* height constrained; natural height 46px */
    border-bottom: 1px firebrick solid;
    margin-bottom: 30px;
}

.top-nav-logo-area {
    display: inline-block;
    padding-right: 25px;
    border: 1px dashed red;
    vertical-align: top;     /* other options include `baseline`, `bottom` and `middle` */
}

.logo {
    background-color: black;
    color: white;
    font-family: monospace;
    font-size: 25px;
}

.top-nav-link-div {
    display: inline-block;
    border-left: 1px #cccccc solid;
    height: 40px;
}
<!-- WITH CONTENT NEXT TO LOGO -->
<div class="container top-nav-bar">
    <div class="top-nav-logo-area">
        <span class="logo">B</span>
        <span class="logo">B</span>
        <span class="logo">C</span>
    </div>
    <div class="top-nav-link-div">
        hello
    </div>
</div>

<!--WITHOUT CONTENT NEXT TO LOGO -->
<div class="container top-nav-bar">
    <div class="top-nav-logo-area">
        <span class="logo">B</span>
        <span class="logo">B</span>
        <span class="logo">C</span>
    </div>
    <div class="top-nav-link-div"><!-- empty --></div>
</div>

jsFiddle

关于html - 一个元素的垂直对齐方式根据相邻元素的内容而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37511596/

相关文章:

jquery 图像保持移动

javascript - 谷歌地图地理位置是否需要安全的 ssl 连接?

WebSphere Portal 的新布局模板中未添加 Css 类

html - Bootstrap 布局中的列可视化

javascript - 根据屏幕大小和父 div 位置定位子 div

javascript - 在完成 javascript css 后重复动画

javascript - jCarousel 垂直滚动不起作用

jQuery - 更改 CSS3 按钮文本?

css - SVG 剪辑路径不适用于 Safari

css - Relative padding 是相对于什么?