LESS CSS嵌套类 [英] LESS CSS nesting classes

查看:191
本文介绍了LESS CSS嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LESS来改进我的CSS,并试图在类中嵌套一个类。有一个相当复杂的层次结构,但由于某种原因,我的嵌套不工作。
我有这个:

I'm using LESS to improve my CSS and am trying to nest a class within a class. There's a fairly complicated hierarchy but for some reason my nesting doesn't work. I have this:

.g {
    float: left;
    color: #323a13;
    .border(1px,#afc945);
    .gradient(#afc945, #c8da64);
    .common;
    span {
        .my-span;
        .border-dashed(1px,rgba(255,255,255,0.3));
    }
    .posted {
         .my-posted;
         span {
            border: none;
         }
    }
}

.g.posted 工作。它只是显示 .g 位。
如果我这样做很好:

I can't get the .g.posted to work. it just shows the .g bit. If i do this it's fine:

.g {
    float: left;
    color: #323a13;
    .border(1px,#afc945);
    .gradient(#afc945, #c8da64);
    .common;
    span {
        .my-span;
        .border-dashed(1px,rgba(255,255,255,0.3));
    }
}

.g.posted {
         .my-posted;
         span {
            border: none;
         }
    }

我想嵌套 .posted .g 。任何想法?

I'd like to nest the .posted in .g though. Any ideas?

推荐答案

LESS不工作。

.class1.class2 {} - 在同一个DOM节点上定义两个类,但

.class1.class2 {} - defines two classes on the same DOM node, but

.class1 {
    .class2 {}
}

定义嵌套节点。 .class2 仅适用于 class1 的节点的子节点。

defines nested nodes. .class2 will only be applied if it is a child of a node with the class class1.

我也对此感到困惑,我的结论是LESS需要一个这个关键字:)。

I've been confused with this too and my conclusion is that LESS needs a this keyword :).

由于我没有任何评论或论证,我感到不得不对我的感觉作出反应

As I have been downvoted without any comment or justification, I feel obliged to react to what I feel like might be the cause of the downvote.

& 字符具有这个关键字,实际上(一个我在写答案的时候不知道的东西)。可以写成:

The & character has the function of a this keyword, actually (a thing I did not know at the moment of writing the answer). It is possible to write:

.class1 {
    &.class2 {}
}

,生成的CSS将如下所示:

and the generated CSS will look like this:

.class.class2 {}

grobitto是第一个发布这条信息的人。

For the record, @grobitto was the first to post this piece of information.

这篇关于LESS CSS嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆