英文:
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("#second")
const button = document.querySelector("button")
toggle = true
button.addEventListener("click", () => {
if (toggle) {
target.style.overflow = "initial"
} else {
target.style.overflow = "hidden"
}
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 -->
<button>Toggle overflow for #second</button>
<main>
<span id="first">
FOO[
</span><span id="second">
<div>]BAR</div>
<div>]BAZ</div>
</span>
</main>
<!-- 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("#second")
const button = document.querySelector("button")
toggle = true
button.addEventListener("click", () => {
if (toggle) {
target.style.overflow = "initial"
} else {
target.style.overflow = "hidden"
}
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 -->
<button>Toggle overflow for #second</button>
<main>
<span id="first">
FOO[
</span><span id="second">
<div>]BAR</div>
<div>]BAZ</div>
</span>
</main>
<!-- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论