How can I detect if a div has more than one line? I mean like this:
div :multiline {
...
}
I'd like to format the content regarding the number of lines (1 or more)
I wouldn't call this a solution so much as something that could potentially lead to a solution. I thought your question was interesting; so I decided to play around with CSS for a little bit.
This is what I came up with.
It uses the current width of the internal span
(given through inline-block
display) to determine the indent width. We use calc()
to make the indent 100%
minus the parent width of 200px
which has our final indent added to it (30px
).
It works best if the single-line content does not get near the full width of the container.
Quick Notes:
calc()
(list of support):before
pseudo class (list of support)inline-block
element to track content width.HTML Code:
<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>
CSS Code:
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;
}
If you're willing to use Javascript/JQuery, you could check the height of the div and see if it is greater than it would be with only one line of text. Of course, this isn't very error-proof, but I wouldn't know another way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With