隐藏溢出为什么会破坏元素的垂直对齐?

huangapple go评论55阅读模式
英文:

Why does hiding overflow break element vertical alignment?

问题

为什么在我的示例中,“Foo”和“Bar”没有垂直对齐?

如果我停止隐藏“Bar”父级的溢出(点击按钮) - 突然溢出的元素“Baz”与“Foo”对齐。

当溢出时,“Foo”和“Bar”在检查时具有完全相同的高度,它们只是以不同的高度呈现,我无法弄清楚原因。

JS 与此无关,只是帮助演示问题。

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-js -->

    const target = document.querySelector("#second")
    const button = document.querySelector("button")

    toggle = true
    button.addEventListener("click", () => {
      if (toggle) {
        target.style.overflow = "initial"
      } else {
        target.style.overflow = "hidden"
      }
      toggle = !toggle
    })

<!-- 语言: lang-css -->

    * {
      margin: 0;
      padding: 0;
      line-height: 1em;
      box-sizing: border-box;
    }
    main {
      background: #eee;
      padding: 1em;
      font-size: 8vw;
    }
    #first {
      background: red;
      display:inline-block;
      height: 1em;
    }
    #second {
      background: blue;
      display:inline-block;
      height: 1em;
      overflow: hidden;
    }

<!-- 语言: lang-html -->

    <button>切换 #second 的溢出</button>
    <main>
      <span id="first">
        FOO[
      </span><span id="second">
        <div>]BAR</div>
        <div>]BAZ</div>
      </span>  
    </main>

<!-- 结束代码片段 -->

请注意,我没有翻译代码部分,只提供了代码的中文注释。

英文:

Why are "Foo" and "Bar" not aligned vertically in my example?

If I stop hiding the overflow from the parent of "Bar" (click the button) - suddenly the overflowed element "Baz" does align with "Foo".

When overflowed, "Foo" and "Bar" have the exact same height when inspected, they are simply rendered at a different height and I can't figure out why.

The JS is not relevant, just helps demonstrate the problem.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const target = document.querySelector(&quot;#second&quot;)
const button = document.querySelector(&quot;button&quot;)

toggle = true
button.addEventListener(&quot;click&quot;, () =&gt; {
  if (toggle) {
    target.style.overflow = &quot;initial&quot;
  } else {
    target.style.overflow = &quot;hidden&quot;
  }
  toggle = !toggle
})

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  line-height: 1em;
  box-sizing: border-box;
}
main {
  background: #eee;
  padding: 1em;
  font-size: 8vw;
}
#first {
  background: red;
  display:inline-block;
  height: 1em;
}
#second {
  background: blue;
  display:inline-block;
  height: 1em;
  overflow: hidden;
}

<!-- language: lang-html -->

&lt;button&gt;Toggle overflow for #second&lt;/button&gt;
&lt;main&gt;
  &lt;span id=&quot;first&quot;&gt;
    FOO[
  &lt;/span&gt;&lt;span id=&quot;second&quot;&gt;
    &lt;div&gt;]BAR&lt;/div&gt;
    &lt;div&gt;]BAZ&lt;/div&gt;
  &lt;/span&gt;  
&lt;/main&gt;

<!-- end snippet -->

答案1

得分: 1

那些是默认沿其基线对齐的内联块。添加 vertical-align: top; 即可:

#second {
  background: blue;
  display:inline-block;
  height: 1em;
  overflow: hidden;
  vertical-align: top;
}

请注意,这是关于CSS的代码部分的翻译。

英文:

Those are inline-blocks which by default are aligned along their baseline. Add vertical-align: top; and you'll be set:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const target = document.querySelector(&quot;#second&quot;)
const button = document.querySelector(&quot;button&quot;)

toggle = true
button.addEventListener(&quot;click&quot;, () =&gt; {
  if (toggle) {
    target.style.overflow = &quot;initial&quot;
  } else {
    target.style.overflow = &quot;hidden&quot;
  }
  toggle = !toggle
})

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  line-height: 1em;
  box-sizing: border-box;
}
main {
  background: #eee;
  padding: 1em;
  font-size: 8vw;
}
#first {
  background: red;
  display:inline-block;
  height: 1em;
}
#second {
  background: blue;
  display:inline-block;
  height: 1em;
  overflow: hidden;
  vertical-align: top;
}

<!-- language: lang-html -->

&lt;button&gt;Toggle overflow for #second&lt;/button&gt;
&lt;main&gt;
  &lt;span id=&quot;first&quot;&gt;
    FOO[
  &lt;/span&gt;&lt;span id=&quot;second&quot;&gt;
    &lt;div&gt;]BAR&lt;/div&gt;
    &lt;div&gt;]BAZ&lt;/div&gt;
  &lt;/span&gt;  
&lt;/main&gt;

<!-- end snippet -->

答案2

得分: 0

好的,这是你的翻译代码部分不要翻译:

* {
  margin: 0;
  padding: 0;
  line-height: 1em;
}

/* To help understand what is going on, uncomment below - note that this does slighty break the animation */
/*
{ border: 0.1vw solid red; }
*/

body {
  display: grid;
  place-items: center;
  margin: 0;
  height: 80vh;
  background: lightpink;
}

.container {
  background: white;
  font-size: 5vw; /* This determines the main size! */
  padding: 1em;
  display: flex;
}
.before {
  display: flex;
  height: 1.5em;
  align-items: center;
}
.animate {
  display: flex;
  flex-direction: column;
  height: 1.5em;
  overflow: hidden;
  position: relative;
}
.animate span {
  background:lightpink;
  display: flex;
  flex-direction: column;
  position: relative;
  line-height: 1.5em;
  animation: scroll 6s infinite linear;
}
@keyframes scroll {
  /* Found with experimentation, not math */
  0% { top: -4.48em } 
  100% { top: -9em; }
}

这部分内容并没有要翻译的文字。

英文:

Ok, so looking at your example. I think you can solve this with flex containers. for sure you can cleanup this a bit. But this removes the need to correct the alignment with margin.

* {
  margin: 0;
  padding: 0;
  line-height: 1em;
}

/* To help understand what is going on, uncomment below - note that this does slighty break the animation */
/*
{ border: 0.1vw solid red; }
*/

body {
  display: grid;
  place-items: center;
  margin: 0;
  height: 80vh;
  background: lightpink;
}

.container {
  background: white;
  font-size: 5vw; /* This determines the main size! */
  padding: 1em;
  display: flex;
}
.before {
  display: flex;
  height: 1.5em;
  align-items: center;
}
.animate {
  display: flex;
  flex-direction: column;
  height: 1.5em;
  overflow: hidden;
  position: relative;

}
.animate span {
  background:lightpink;
  display: flex;
  flex-direction: column;
  position: relative;
  line-height: 1.5em;
  animation: scroll 6s infinite linear;
}
@keyframes scroll {
  /* Found with experimentation, not math */
  0% { top: -4.48em } 
  100% { top: -9em; }
}

Here a codepen

huangapple
  • 本文由 发表于 2023年2月23日 22:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定