【问题标题】:CSS LESS Preprocessor adds unnecessary white spaceCSS LESS 预处理器添加了不必要的空白
【发布时间】:2013-09-07 06:06:34
【问题描述】:

我正在尝试使用 CSS LESS 预处理器将 .less 转换为 .css。但是,无论我使用什么预处理器,它们都会在输出 .css 中添加不必要的空格。

例如,考虑这个 LESS 示例:

/* Images */
img {
    /* Site logo */
    #logo {
        display: block;
        margin-left: auto;
        margin-right: auto;
        width: 100%;
        max-height: 93px;
        max-width: 500px;
    }
}

使用预处理器(在本例中为 SimpLESS)后,最终结果是这样的:

/* Images */
img {
    /* Site logo */
}

img #logo {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-height: 93px;
    max-width: 500px;
}

因为img 和#logo 之间有一个空格,所以浏览器不能使用#logo 样式。但是,如果我删除 img 和 #logo 之间的空格,浏览器会正确呈现网站。

我想知道为什么预处理器会添加不必要的空格以及为什么会导致这个奇怪的问题。

另一个例子,LESS:

table {
    .latestProjects {
        thead {
            color: @colorHeaders;
        }

        .col1 {
            width: 120px;
        }

        .col2 {
            width: 130px;
        }
}

变成:

table .latestProjects thead {
    color: #bd9d0d;
}

table .latestProjects .col1 {
    width: 120px;
}

table .latestProjects .col2 {
    width: 130px;
}

并且由于空格而不起作用。

【问题讨论】:

    标签: css less whitespace


    【解决方案1】:

    它完全按照您的指示进行渲染。

    您要求在img 标记内输入#logo id。

    如果你想要一个名为logo 的img,你需要这样说。

    /* Images */
    img {
        /* Site logo */
        &#logo {
            display: block;
            margin-left: auto;
            margin-right: auto;
            width: 100%;
            max-height: 93px;
            max-width: 500px;
        }
    }
    

    见:LESS: Nested Rules

    新增示例中的相同问题:

    table {
        &.latestProjects {
            thead {
                color: @colorHeaders;
            }
    
            .col1 {
                width: 120px;
            }
    
            .col2 {
                width: 130px;
            }
        }
    }
    

    就是你的意思。

    【讨论】:

    • 我觉得 logo 就够了吗?
    • 哎呀。是的。解决了这个问题。
    • 非常感谢您的帮助。 :)
    猜你喜欢
    • 2016-09-02
    • 2013-10-15
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 2011-09-03
    • 2012-08-09
    相关资源
    最近更新 更多