Is there a way to set a common image path for LESS files?

前端 未结 6 1355
面向向阳花
面向向阳花 2020-12-05 03:58

I am using the LESS styling language.

Consider the following CSS:

.side-bg
{
    background:url(../img/layout/side-bg.jpg) top no-repeat;    
}
         


        
6条回答
  •  眼角桃花
    2020-12-05 04:28

    I was searching for the same question and found this page. Thought I would post my solution as someone else might find it useful...

    @iconpath: '/myicons/';
    
    .icon (@icon) {
        background: no-repeat url('@{iconpath}@{icon}');
    }
    
    .icon-foo { .icon('foo.png'); }
    .icon-bar { .icon('bar.png'); }
    .icon-spuds { .icon('spuds.png'); }
    

    which compiles to (used http://winless.org/online-less-compiler)

    .icon-foo {
      background: no-repeat url('/myicons/foo.png');
    }
    .icon-bar {
      background: no-repeat url('/myicons/bar.png');
    }
    .icon-spuds {
      background: no-repeat url('/myicons/spuds.png');
    }
    

提交回复
热议问题