CSS 中的垂直居中和溢出 Excel 样式?
有没有办法在固定大小的 div 中执行可变大小的多行内容的垂直居中,并隐藏溢出?
目的是重现您在 Excel 单元格中看到的内容:当内容适合容器时,它应该垂直居中,当容器较大时,溢出的部分应该被隐藏(并且内容仍然垂直对齐),就像相邻单元格不为空的 Excel 单元格。
我知道如何使用 CSS 垂直居中,我知道如何在内容不垂直居中时隐藏溢出,但我不知道如何同时执行这两个操作... Javascript 是唯一的答案吗?
诀窍在于 CSS 定位方法不适用于可变大小的内容(我的内容是动态文本),并且当您使用 display:table-cell
时,它会有效地禁用 CSS 溢出控制(并且容器会增大以容纳内容)。
Is there a way to perform a vertical centering of a variable-sized multi-line content within a fixed-size div, with hidden overflow?
The aim would be to reproduce what you can see in Excel cells: when the content fits the container, it should be vertically centered, when it is larger, the parts that overflow should be hidden (and the content still vertically aligned), like in an Excel cell whose neighbours aren't empty.
I know how to vertically center using CSS, I know how to hide overflow when the content isn't vertically centered, but I've no idea how to do both at the same time... Is Javascript the only answer?
The trick is that CSS positioning approaches don't work with variable-sized content (my content is dynamic text), and when you use display:table-cell
, it effectively disables CSS overflow control (and the container grows to accomodate the content).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该使所有单元格高度为 65 像素,并使单元格的文本显示在中间。当文本太多时,文本就会消失。
我相信这就是你想要的?
CSS:
HTML:
您可以在 JSFiddle 上尝试。
This should make all the cells 65px high, and make the cells' text show up in the middle. When it's too much text, the text disappears bellow.
I believe it's what you want?
CSS:
HTML:
You can try it on JSFiddle.