Nov
23
2009

.less CSS – Dynamic CSS for .Net

More than once in the past 6 months I have wanted variables, inheritance, and class chaining in CSS. I was never able to find something that did was I was looking for, and I thought about creating my own, but who has the time? Now I don’t have to, because yesterday I discovered .less.

With .less (pronounced dot-less) you write CSS as you are used to, but you can mix it up a bit to make things a little more clear and concise. For example, instead of having to duplicate the same hex color in a dozen or so selectors, you can define a variable as that color and just reference that.

@brand_color: #4D926F;

    #header {
    color: @brand_color;
    }

    h2 {
    color: @brand_color;
    }

 

If you want to chain your classes together you can simply include one in the other.

.rounded_corners {
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;
    border-radius: 8px;
    }


    #header {
    .rounded_corners;
    }


    #footer {
    .rounded_corners;
    }

 

You can nest your rules to make inheritance a little more clear.

#header {
    color: red;
    a {
    font-weight: bold;
    text-decoration: none;
    }
    }

 

And lastly you can include some basic operators to give complex relationships to your properties.

@the-border: 1px;
    @base-color: #111;

    #header {
    color: @base-color * 3;
    border-left: @the-border;
    border-right: @the-border * 2;
    }


    #footer {
    color: (@base-color + #111) * 1.5;
    }

 

If that isn’t all, you can configure .less to cache and minify the output. Something I will be taking advantage of for sure.

Add comment




biuquote
Loading


AdSense

Software engineer by hobby and trade. When I am not sitting in front of a computer, you can find me playing with my kids. I am lucky enough to be married to my best friend and high school sweetheart. Life couldn't be better!

 

All content is mine, not my employers

Chronology

Tweets

what is the easiest way to *just* the last changeset number from git?
21 hours ago via Silver Bird
@ParasValecha so it is a virus some marketing guy manage to win an award for? ;)
3 days ago via Silver Bird
@ParasValecha none. I think they are one of the worse "viruses" you can get. Thrashing your disk and hogging CPU any chance they get
3 days ago via Silver Bird
...i have gone 5 years without one and been fine
3 days ago via Microsoft
... and provide little value on a day to day basis. Don't go to random content, and don't open stuff you don't understand...
3 days ago via Microsoft
I wholeheartedly believe that virus scanners are one of the worst things to install. they really slow down your computer...
3 days ago via Microsoft
RT @CodeWisdom: "When your hammer is C++, everything begins to look like a thumb." - Steve Haflich #fb
5 days ago via rowi for Windows Phone
Follow me on Twitter