我很困惑这是怎么回事有人能告诉我到底发生了什么吗?

body,
html {
  padding: 0;
  margin: 0;
  height: 100vh;
  width: 100%;
}

.chat-break {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;

}

.chat-break .line {
  border-bottom: 1px solid #ddd;
  width: 100%;
}

.chat-break .date {
  color: #B5B5B5;
  position: absolute;
  background-color: #fff;
  padding-left: 8px;
  padding-right: 8px;
  
}
<div class="chat-break">
    <div class="line">
    </div>
    
    <p class="date">Today</p>
</div>
  

我的理解是:

  • .chat-break flexbox有两个元件.line.date.
  • ...但是在.date上使用position: absolute之后,它不再是flexbox中的元素了吗?
  • 那为什么"今天"的主题是线?

推荐答案

前言:

  • toprightbottomleft属性统称为"方框插入"属性.
  • Keep in mind that all CSS properties that aren't explicitly set will have default values that are either "initial" values or inherited values.
  • CSS有很多令人惊讶和违反直觉的行为.

Why <div class="line"> fills the width of the page:

  • <div class="chat-break"> has display: flex;, which makes it a flex-parent.
    • flex父元素的所有直接子元素(除了某些例外)都是flex-items,它们有position: static个.

    • 因此,<div class="line">是一个灵活的项目.

    • 因此<p class="date">not,因为它有position: absolute;.

      • (一般来说)position: absolute;的元素是not subject to their layout-container's layout rules,用绝对坐标重新定位,原点在their-closet-ancestor-element-without-101(是的,这是一口).
  • 因此,<div class="chat-break">是一个灵活的父母with only one flex-item,尽管有两个元素的子元素.
  • Also,因为<div class="chat-break">display: flex;(而不是display: inline-flex),这意味着<div class="chat-break">block-level element,所以它填充其容器的宽度,即<body>,它填充视口的宽度.

Why <p class="date"> is centered:

  • As <p class="date"> uses auto for all its box inset properties (i.e. top, right, etc) with position: absolute; then the computed value of those properties is the same as if <p class="date"> was position: static;.
    • However, if <p class="date"> was position: static; then it would be a flex-item and would share its flex row with <div class="line"> - which would mean it would be located somewhere off to the right of the line (due to justify-content: center;).
      • 但它实际上是以中心为中心的,因为这是一个特例场景,是specifically specified in the specification...

        https://www.w3.org/TR/css-flexbox-1/#abspos-items

        4.1. 绝对定位的Flex children

        由于它不流动,flex容器的绝对定位子容器不会参与flex布局.

        假设flex容器is determined such that the child is positioned as if it were the sole flex item in the flex containerabsolute位置的子容器和柔性容器都是固定大小的盒子,则static位置的子容器为flex容器is determined such that the child is positioned as if it were the sole flex item in the flex container.为此,自动利润率被视为零.

这一节的其余部分很值得一读,因为它还涵盖了其他大脑拉伸场景,比如"如果使用align-self: center;怎么办"?

Css相关问答推荐

Css:有没有更快捷的方式来编写多媒体查询?

如何在CSS中平滑线性平移动画

如何在Safari中隐藏CSS的offset-path属性?CSS支持规则不按预期工作

动态覆盖 Vuetify 类

有没有办法在 CSS 字体速记中使用数字字体粗细?

热门制作渐变半红/半蓝?

flex 容器中的两个滚动块

CSS:如何在嵌套计数器中有多个计数器

CSS网格列随着长内容改变大小

CSS Grid 2 列和半高

CSS:菜单图标动画

您如何设置 Google Places Autocomplete API 的下拉菜单样式?

CSS nth-child 表示大于和小于

仅针对直接子代而不是其他相同后代的 CSS Select 器

输入/按钮元素不会在 flex 容器中缩小

HTML 元素默认为 display:inline-block?

在 Firefox 中删除额外的按钮间距/填充

flex-wrap 如何与 align-self、align-items 和 align-content 一起使用?

在 CSS 中表示 100% 的 1/3 的最佳方式是什么?

如何将文本向左旋转 90 度并根据 html 中的文本调整单元格大小