简体   繁体   English

是否有用于溢出的 css 伪选择器?

[英]Is there a css pseudo selector for overflow?

I'm trying to alter the style of something based on wether or not its parent div is being overflown.我正在尝试根据其父 div 是否被溢出来改变某些东西的样式。

.pDiv { display: block; width: 300px; height: 100px; border: 1px solid rgb(0,0,0); }
.cDiv { display: block; padding 4px; border-bottom: 1px solid rgb(0,0,0);
.pDiv:overflow .cDiv { border-bottom: none; }

<div class="pDiv"><div class="cDiv">child 1</div><div class="cDiv">child 2</div><div class="cDiv">child 3</div><div class="cDiv">child 4</div><div class="cDiv">child 5</div></div>

is it possible to do something like this?有可能做这样的事情吗? I would use the last-child pseudo-selector, but the number of children can vary, so I want it to remove the border-bottom of the last-child ONLY IF the parent div is being overflown.我会使用 last-child 伪选择器,但是孩子的数量可能会有所不同,所以我希望它仅在父 div 被溢出时才删除最后一个孩子的边框底部。 I want a pure CSS solution too please, no JS!我也想要一个纯 CSS 解决方案,不要 JS!

CSS无法根据任何类型的已用或计算样式进行选择,因此您运气不佳。

It seems a handy solution for this is being cooked up : https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries这似乎是一个方便的解决方案https ://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries

According to css-tricks , the feature "@container brings us the ability to style elements based on the size of their parent container."根据css-tricks的说法, “@container 使我们能够根据父容器的大小设置元素样式。”

You should already be able to use it, but beware that not every browser supports this yet .您应该已经可以使用它了,但要注意并不是每个浏览器都支持它

This way, you might (read the note) be able to get out with something like:这样,您可能(阅读注释)能够通过以下方式摆脱困境:

.parent-div {
    max-height: 10rem;
    overflow-y: auto;
    container: size;
}

@container (min-height: 10rem) {
    .parent-div:last-child {
        border-bottom: none;
    }
}   

The main idea here being that if the element reached it's maximum height, then it's all but always overflowing — so we just apply the style so long as it's at it's maximum height.这里的主要思想是,如果元素达到它的最大高度,那么它几乎总是溢出——所以我们只应用样式,只要它处于最大高度。

Unfortunately, my own browser does not support this yet, so I can't guarantee you it would work the exact way as it is written above.不幸的是,我自己的浏览器还不支持这个,所以我不能保证它会像上面写的那样工作。 But if you refer to the 2 pieces of documentation I provided, you should be able to come out on top 🤓但是,如果您参考我提供的 2 份文档,您应该能够脱颖而出🤓

Note:笔记:

The css-tricks page also mentions that "Currently, you cannot use height-based container queries, using only the block axis" . css-tricks页面还提到“目前,您不能使用基于高度的容器查询,仅使用块轴” I'm hoping this simply means using the full size axis is necessary in this case, but I'm not able to test this.我希望这仅仅意味着在这种情况下需要使用全size轴,但我无法对此进行测试。

If someone could verify whether this solution works and then leave a comment here, that would be very much appreciated.如果有人可以验证此解决方案是否有效,然后在此处发表评论,那将不胜感激。 I'd edit this answer and credit the person.我会编辑这个答案并相信这个人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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