html - 将文本限制在 div 内的 2 行并限制所有文本溢出?

标签 html css

<分区>

我需要有一个父 div,里面有一个包含嵌套文本的子 div。子 div 应该只允许 2 行文本的空间,并且任何额外的文本都应该像在 textOverflow=ellipsis 中一样被切断,最后带有“...”。是这样的: enter image description here

你能告诉我最有效的 css 和 html 吗?这是我想出的(这是行不通的):

outerContainer: {
            padding: theme.spacing.unit * 0.5,
            display: 'block',
            maxHeight: "50px",
            flexGrow: 1,
            width: "100%",
            overflow: 'hidden',
            boxSizing: 'border-box',
    },
        innerContainer: {
            padding: theme.spacing.unit * 0.5,
            display: 'inline-block',
            width: "100%",
            height: 'auto',
            boxSizing: 'border-box',
            textOverflow: 'ellipsis',
            wordBreak: 'break-all',
            fontSize: "0.8rem",
            fontFamily: theme.typography.fontFamily,
        },
    ...
    <div className={classes.outerContainer}>
                    <div className={classes.innerContainer}>
                        <span>{"The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.</span>
                    </div>
</div>

最佳答案

使用溢出:隐藏;。基本上,任何超过 div 高度的东西都会被截断(隐藏)。确保您拥有适合您的 2 行高度。

Overflow

.inner {
  border: 2px solid red;
  color: red;
  height: 40px;
  overflow: hidden;
}
<div class='outer'>
  <div class='inner'>The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.</div>
</div>

关于html - 将文本限制在 div 内的 2 行并限制所有文本溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976867/

上一篇:css - element.style 任意增加字体大小

下一篇:css - 如何在悬停时展开菜单上的背景颜色高度

相关文章:

javascript - 通过 Kinetic.js 文本元素中的字符串索引获取单词/字符屏幕位置

html - 用于类似表格布局的最佳 HTML/CSS 模型

html - 从 typescript 调用 CSS 样式将其附加到 html 文件

css - 如何将 Material-UI 托管样式应用于非 Material-UI、非 React 元素?

css - 如何从 css 选择器中打开一个单独的 div

html - 我的行内 block 元素没有正确排列

javascript - 使用 jQuery 自定义动态幻灯片

javascript - 如何在邮件签名中使用位智导航链接

css - Facebook-like-box 中的图片帖子对于容器来说太大了

javascript - 如何在父div中水平居中多个div