简体   繁体   English

:first-child和:last-child同时

[英]:first-child and :last-child simultaneously

This may be very easy as i have a feeling i'm missing a basic point here. 这可能很容易,因为我觉得我在这里没有基本要点。

Situation: 情况:

.singleOrder:first-child {
    border-radius: 5px 0 0 0;
}

.singleOrder:last-child {
    border-radius: 0 5px 0 0;
}

Works really well until there is only one child. 在只有一个孩子之前,效果非常好。 In that case the second declaration will overwrite the first one and the desired effect will not be achieved. 在这种情况下,第二个声明将覆盖第一个声明,并且无法达到预期的效果。

What is the most short and elegant way to solve this? 解决这个问题的最简短,最优雅的方法是什么?

Split it: 拆分它:

.singleOrder:first-child {
    border-top-left-radius: 5px;
}

.singleOrder:last-child {
    border-bottom-left-radius: 5px;
}

Or write an extra rule: 或写一条额外的规则:

.singleOrder:first-child:last-child {
    border-radius: 5px 5px 0 0;
}

Use :only-child : 使用:only-child

.singleOrder:only-child {
    border-radius: 5px 5px 0 0;
}

Update: Yogu is right. 更新:Yogu是正确的。 I forgot to mention. 我忘了提。 This should come after your statements. 这应该在您声明之后。

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

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