【发布时间】:2021-05-13 17:32:07
【问题描述】:
我有一个包含文本的元素,其中的文本可能很长。我想隐藏溢出文本,我不希望文本增加元素或其父/祖先的大小。
将以下内容添加到元素 CSS 一开始似乎有效(来自this question,但高度不是宽度):
height: 0;
min-height: 100%;
这适用于 Chrome;但是,它似乎不太适用于 Safari(移动或桌面)。 这是一个例子:
图片显示了 Chrome(左)和 Safari(右)的并排比较。 文本应该垂直居中,但可以看出,Safari 文本不是。 最终,如果页面高度太小,文本就会完全消失。
HTML 在这篇文章的底部(抱歉我没能把它写得更短)。
在 Mac 上通过 Chrome(版本 88.0.4324.146)和 Safari(版本 13.1.3)上的 devtools 检查,问题似乎是 Safari 上的 inner 元素过大。
这似乎是由 height:0;min-height:100% 技巧直接引起的,因为将这些行替换为 height:100% 会导致 Safari 中的居中问题消失(但当文本较长时,Chrome 中的布局会受到干扰)。
据我了解,min-height:100% 应该将inner 元素的高度设置为包含块的高度,其中(因为inner 具有position:relative 并且其父级card 具有display:flex),应该是父card 元素的内容框,如here 所述。
这似乎按照 Chrome 中的描述工作,但不是 Safari。我不确定 Safari 是从哪里获得 inner 元素的高度,但它显然比 card 高。
对于上下文,这是我遇到的原始问题的样子(当青色框中有很多文本时,Chrome 中的布局混乱了)。这是我试图用height:0;min-height:100% 解决的问题。对于这个屏幕截图,我删除了这些行,只输入了height:100%。看起来是这样的(同样,左侧是 Chrome,右侧是 Safari)
如您所见,橙色框已在 Chrome 中消失(左)。
重新添加height:0;min-height:100% 会导致它正确呈现(但由于上述问题,文本在 Safari 中未垂直居中 - 它看起来居中,但这只是因为溢出被隐藏,因此卡片显示为充满文本):
感谢任何见解。这只是Safari中的一个错误吗?有解决方法吗?或者(更有可能)我只是一个 CSS 笨蛋……
更新:这是它在 Safari 开发工具中的样子。父 card 元素的高度为 938px,但 inner 的高度为 1121px,并将 bottombar 推离屏幕。
<html>
<head>
<style>
html, body {
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
}
body {
position: relative;
}
.page {
background: midnightblue;
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
justify-content: center;
}
.central-column {
display: flex;
flex-direction: column;
}
.bottombar {
background: orange;
height: 80px;
z-index: 30;
display: flex;
flex-direction: column;
justify-content: space-around;
font-size: 2vh;
text-align: center;
}
.foo {
background: pink;
position: relative;
flex-grow: 1;
display: flex;
flex-direction: column;
}
.foo,
.central-column {
width: 800px;
max-width: 800px;
height: 100%;
}
.foo .topbar {
background: red;
position: relative;
display: flex;
flex-direction: row;
justify-content: space-around;
min-height: 90px;
height: 15vh;
max-height: 90px;
}
.foo .cardbar {
position: relative;
flex-grow: 1;
display: flex;
flex-direction: column;
padding: 4px 20px;
}
.foo .cardtable {
background: green;
position: relative;
flex-grow: 1;
display: flex;
flex-direction: column;
padding: 4px;
}
.foo .stack {
flex-grow: 1;
display: flex;
flex-direction: column;
justify-content: space-around;
z-index: 6;
position: relative;
padding: 4px 20px;
}
.foo .stack .card {
background: cyan;
position:relative;
max-height: 160vw;
flex-grow: 1;
display: flex;
flex-direction: column;
overflow: hidden;
border-radius: 2vh;
border-width: 1px;
border-style: solid;
}
.foo .stack .card .inner {
position: relative;
display: flex;
flex-direction: column;
justify-content: space-evenly;
height: 0;
min-height: 100%;
/*
Replacing the previous two lines with this line fixes the centering on Safari,
but then longer text (that would overflow the cyan box) breaks the layout in Chrome
height: 100%;
*/
text-align: center;
overflow: hidden;
margin: 4px;
}
</style>
</head>
<body>
<div class="page">
<div class="central-column">
<div class="foo">
<div class="topbar">
</div>
<div class="cardbar">
<div class="cardtable">
<div class="stack">
<div class="card">
<div class="inner">
<div class="content">
this text should be vertically centered in the cyan box
</div>
</div>
</div>
</div>
</div>
</div>
<div class="bottombar"></div>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
标签: html css google-chrome safari