css - last-child 和 last-of-type 在 SASS 中不起作用

标签 css sass css-selectors

您将如何编写它以符合 SASS 标准?

.fader { display: inline-block; }
.fader img:last-child {
    position: absolute;
    top: 0; 
    left: 0;
    display: none;
}​

基本上,我只是复制了这个例子,即一张图片在另一张图片上淡入淡出 (found here.)

他的 JFiddle 示例:http://jsfiddle.net/Xm2Be/3/

但是他的示例是纯 CSS,我正在 SASS 中处理一个元素,不确定如何正确翻译它。

我的代码

请注意,在我下面的示例中,img 悬停无法正常工作(两个图像都显示并且没有发生翻转淡入操作)

我的 CodePen: http://codepen.io/leongaban/pen/xnjso

我试过了

.try-me img:last-child & .tryme img:last-of-type

但是 : 抛出 SASS 编译错误,下面的代码有效

.try-me img last-of-type {
    position: absolute;
    top: 0; 
    left: 0;
    display: none;
}

但是它吐出的 CSS 对我没有帮助:

.container .home-content .try-me img last-of-type {
    position: absolute;
    top: 0;
    left: 0;
    display: none;
}

更新:工作代码笔:

http://codepen.io/leongaban/pen/xnjso

最佳答案

Sass 不需要嵌套。如果不需要拆分选择器,则不要觉得有义务这样做。

.try-me img:last-of-type {
    position: absolute;
    top: 0; 
    left: 0;
    display: none;
}

如果您将样式应用到图像,然后将特定样式应用到最后一个类型,那么当您嵌套它时它会是这样的:

.try-me img {
    // styles

    &:last-of-type {
        position: absolute;
        top: 0; 
        left: 0;
        display: none;
    }
}

关于css - last-child 和 last-of-type 在 SASS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16905061/

相关文章:

css - 为什么@media 查询不更改特定屏幕尺寸的元素?

javascript - 在不使用 javascript 的情况下,div 是否使图像高度与溢出匹配?

ruby - Jekyll 2.0 中的 SCSS 生成错误

css - VueJS 中的全局样式与局部样式

grails - 如何在Grails Assets 管道 Controller 操作上放置@Cacheable批注?

html - 试图将 FieldSet 居中

jquery - 如何更改列表中只有一个用户的状态?

css - 哪些字符在 CSS 类名/选择器中有效?

python - 无法找到元素 - 多个元素似乎具有相似且最少的信息,但我无法正确找到

javascript - 如何使用 CasperJS 获取下拉框的值