html - 最大高度不会降低文本的高度

标签 html css

我试图截断 div 元素内某些文本的高度,但问题是 max-height 不起作用,而 max-width 工作正常。

我的 HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page</title>
</head>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
    <div class="text">
            Improve ashamed married expense bed her comfort pursuit mrs. Four time took ye your as fail lady. Up greatest am exertion or marianne. Shy occasional terminated insensible and inhabiting gay. So know do fond to half on. Now who promise was justice new winding. In finished on he speaking suitable advanced if. Boy happiness sportsmen say prevailed offending concealed nor was provision. Provided so as doubtful on striking required. Waiting we to compass assured. 
    </div>
</body>
</html>

我的 CSS 代码:

.text{
    color: red;
    max-height: 10%;
    max-width: 80%;
}

这会减小文本的宽度但不会减小高度。

最佳答案

% 计算从父高度/宽度开始。 .text parent 是 body,body 没有应用高度。所以实际上 max-height: 10%; 不会被正确计算。尝试添加一些包装器并在 px 中添加一些高度以查看差异。例如:

.wrapper {
  height: 400px;
  outline: 1px solid red;
}

.text{
    outline: 1px solid green;
    color: red;
    max-height: 10%;
    max-width: 80%;
}
<div class=wrapper>
      <div class="text">
            Improve ashamed married expense bed her comfort pursuit mrs. Four time took ye your as fail lady. Up greatest am exertion or marianne. Shy occasional terminated insensible and inhabiting gay. So know do fond to half on. Now who promise was justice new winding. In finished on he speaking suitable advanced if. Boy happiness sportsmen say prevailed offending concealed nor was provision. Provided so as doubtful on striking required. Waiting we to compass assured. 
    </div>
</div>

关于html - 最大高度不会降低文本的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58266966/

相关文章:

python - 从 Python 中的字符串中去除 HTML

html - @font-face 只对内联字体系列不起作用

html - 任何人都可以向我解释什么(内容 : "\f01a"; ) on css means? 以及如何使用它?

javascript - Safari Mobile 应用程序横幅更改视口(viewport)高度

javascript - 第二次点击后旋转回div

javascript - 如何感知 javascript 加载值变化的时间

javascript - 在 native JavaScript 中等待其他事情完成

php - 在请求播放之前,如何阻止嵌入的声音片段下载到浏览器缓存中?

javascript - div无法正确对齐

jquery - float 页脚 jquery 插件? (不是常见的 "how to make a footer stick to the bottom of the page")