简体   繁体   中英

CSS selector if sibling div overflows?

I have the following html

<!-- initial height is enough to show one line of text -->
<div id="div1"> some dynamic text </div>

   .
   .
   .

<!-- on clicking uses JS to set height of div1 to fit content
     only show if div1 has text overflowing -->
<div id="div2"> expand div1 </div>

Now, I want to show div2 only if div1 has enough text that it needs to be expanded, that is, if the content in div1 is overflowing. I know it can be achieved using JavaScript, but I want to know if it can be done using CSS exclusively.

Any suggestions?

Edit:
The text is dynamically fetched when a page is rendered. I can also not use media rules since the width at which I hide the expansion div is not fixed (the width threshold depends on the length of dynamic text which changes on each render)

As CBroe mentioned there is no selector for overflow detection. What you try to do is currently not possible using CSS only.

Additionally there is a GitHub issue with proposal to add such selector, but probably it will be closed with WON'T FIX

Mentioned GitHub issue: link

Related stack question: link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM