如何检测div是否有多行?我的意思是这样的:
div :multiline {
   ...
}
Run Code Online (Sandbox Code Playgroud)
我想格式化有关行数(1或更多)的内容
我不会把这个解决方案称为可能导致解决方案的东西.我觉得你的问题很有趣; 所以我决定尝试一下CSS.
它使用内部的当前宽度span(通过inline-block显示给出)来确定缩进宽度.我们使用calc()缩进100%减去其父宽度的200px最后一个缩进添加到它(30px).
如果单行内容未接近容器的整个宽度,则效果最佳.
快速笔记:
calc()(支持列表):before伪类(支持列表)inline-block元素来跟踪内容宽度.HTML代码:
<h1>Success Cases</h1>
<p><span class="indent">Single line Single line</span></p>
<p><span class="indent">The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</span></p>
<h1>Failure Cases</h1>
<p><span class="indent">Single line Single line Single 1</span></p>
<p><span class="indent">Single line Single line Single line</span></p>
Run Code Online (Sandbox Code Playgroud)
CSS代码:
p {
    background-color: #ccc;
    margin: 10px;
    width: 200px;
}
p > .indent {
    display: inline-block;
}
p > .indent:before {
    background-color: red;
    content: "";
    float: left;
    width: calc(100% - 200px + 30px);
    height: 1em;
}
Run Code Online (Sandbox Code Playgroud)
        如果您愿意使用 Javascript/JQuery,您可以检查 div 的高度,看看它是否大于仅一行文本的高度。当然,这并不是非常防错,但我不知道还有其他方法。