Is there a CSS way to know if an element has wrapped to a new line?

好久不见. 提交于 2020-01-01 10:52:36

问题


If I have a number of inline-block elements on a line, I want to be able to style the elements that have wrapped to the next line differently.

e.g.

<div class='wrapper'>
    <div style='display: inline-block; width: 500px'>...</div>
    <div style='display: inline-block; width: 500px'>...</div>
</div>

So, when the browser width < 1000, the second child <div> can have different styling.

I know I can do it in js, but I am trying to avoid it. I also know I can use media queries, but I am concerned that the min-width cutoff may not be exactly the same as when the <div>'s wrap (I do not know their exact sizes).


回答1:


CSS only provides a ::first-line pseudo-element for identifying the first line in a run of line boxes. It does not provide any pseudo-elements for any other lines, nor does it provide any pseudo-classes for matching inline-level elements based on the line they are on.

You can use ::first-line to style the first line of elements differently (i.e. instead of styling the second line of elements, apply those styles to all lines and style the first line as originally intended), but this only works for a limited set of properties.



来源:https://stackoverflow.com/questions/36944347/is-there-a-css-way-to-know-if-an-element-has-wrapped-to-a-new-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!