
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cory Schires &#187; LESS</title>
	<atom:link href="http://coryschires.com/tag/less/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryschires.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 14 Feb 2012 22:45:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Dynamic CSS3 Buttons with LESS</title>
		<link>http://coryschires.com/dynamic-css3-buttons-with-less/</link>
		<comments>http://coryschires.com/dynamic-css3-buttons-with-less/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 20:03:39 +0000</pubDate>
		<dc:creator>Cory Schires</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[LESS]]></category>

		<guid isPermaLink="false">http://coryschires.com/?p=334</guid>
		<description><![CDATA[Lean how to make your buttons dynamic using LESS - an awesome new CSS abstraction language.]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s been a lot of buzz surrounding CSS3 buttons &#8211; including some <a href="http://www.webdesignerwall.com/tutorials/css3-gradient-buttons/">really great tutorials</a>. The concept behind a basic button, however, is pretty simple:</p>
<ol class="block_list">
<li>Apply a subtle background gradient</li>
<li>Add some rounded corners</li>
<li>Add text shadows &#8211; optionally</li>
<li>Add a thin border &#8211; optionally</li>
<li>Add a box shadow &#8211; optionally</li>
<li>Repeat this process for active and hover states</li>
</ol>
<p>For sure, making good buttons means doing these steps well. But programmatically, this is pretty damn simple. The difficultly lies in design not code.</p>
<p>With that in mind, let&#8217;s look at how we might use a CSS abstraction language, like <a href="http://fadeyev.net/2010/06/19/lessjs-will-obsolete-css/">LESS</a>, to boil these steps down and make our button code super flexible. But first, let&#8217;s take a look at CSS abstraction languages more generally.</p>
<h3>What&#8217;s a CSS abstraction language?</h3>
<p>An abstraction language is a wrapper that adds powerful functionally to the standard CSS language. Have you ever wanted to set a variable in your stylesheet or do some math? Abstraction languages make this possible.</p>
<p>Now, if you&#8217;re like me, you might be thinking: &#8220;If I need to do multiplication in a stylesheet something&#8217;s gone terribly wrong.&#8221; Keep reading. Maybe you&#8217;ll change your mind.</p>
<h3>How do I use a CSS abstraction language?</h3>
<p>Normally, abstraction languages require a supporting back-end language. This supporting language parses your abstracted code and generates normal, static stylesheets at runtime. Ruby, for example, does exactly this to turn SASS into CSS.</p>
<p>That&#8217;s cool, but for most projects we don&#8217;t need a powerful back-end framework. And there&#8217;s certainly no reason start using one just to make your stylesheets more dynamic. This means, by and large, abstraction languages just aren&#8217;t viable except in large, complicated projects.</p>
<p>But <a href="http://fadeyev.net/2010/06/19/lessjs-will-obsolete-css/">LESS</a> is about to change all that. <a href="http://cloudhead.io/">Alexis Sellier</a> has recently developed a pure JavaScript implementation. This means no back-end language is required. Just follow these simple steps:</p>
<ol class="block_list">
<li>Give your stylesheet a mime type of <span class="code_text">.less</span> rather than <span class="code_text">.css</span></li>
<li>Add an attribute of <span class="code_text">rel=&quot;stylesheet/less&quot;</span> rather than <span class="code_text">rel=&quot;stylesheet&quot;</span>
    </li>
<li>Include the <span class="code_text">less.js</span> script <span class="italic">below</span> all stylesheets in your header.</li>
</ol>
<p>With that finished, your header should include something like:</p>
<div class="code_block">
<pre class="brush: css;">
        &lt;link rel="stylesheet/less" href="buttons.less" type="text/css" charset="utf-8" &gt;
        &lt;script src="less-1.0.30.min.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
    </pre>
</div>
<p>And now we have access to some pretty awesome features. Like setting variables and performing operations:</p>
<div class="code_block">
<pre class="brush: css;">
        @pagewrap_width: 970px
        @accent_color: #2BBF26;        

        section {
          @pagewrap_width - 300px
          color: @accent_color - #f0f0f0;
        }
    </pre>
</div>
<p>And that&#8217;s just a few of the awesome features. I encourage you to checkout the <a href="http://fadeyev.net/2010/06/19/lessjs-will-obsolete-css/">details</a> and <a href="http://lesscss.org/">documentation</a>.</p>
<h3>How can we use this to make better buttons?</h3>
<h4><span>1</span> Get a nice looking button.</h4>
<p>Making a nice button can be tricky, so I recommend using the code posted in one of these <a href="http://www.smashingmagazine.com/2009/12/02/pushing-your-buttons-with-practical-css3/">excellent tutorials</a>. But if you&#8217;d rather roll your own, I suggest this dead simple <a href="http://css-tricks.com/examples/ButtonMaker/">button making tool</a>.</p>
<p>Whatever your method, just get some CSS and paste it into your <a href="http://fadeyev.net/2010/06/19/lessjs-will-obsolete-css/">LESS</a> equipped stylesheet. In this example, I&#8217;ll be using this code:</p>
<div class="code_block">
<pre class="brush: css;">
        /* styles for button color */
        .button {
          color: #fef4e9;
          border: solid 1px #da7c0c;
          background: #f78d1d;
          background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20));
          background: -moz-linear-gradient(top,  #faa51a,  #f47a20);
        }
        .button:hover {
          background: #f47c20;
          background: -webkit-gradient(linear, left top, left bottom, from(#f88e11), to(#f06015));
          background: -moz-linear-gradient(top,  #f88e11,  #f06015);
        }
        .button:active {
          background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a));
          background: -moz-linear-gradient(top,  #f47a20,  #faa51a);
        }

        /* basic styles for button size, padding, etc. */
        .button {
          display: inline-block;
          outline: none;
          cursor: pointer;
          text-align: center;
          text-decoration: none;
          font-size: @font_size;
          line-height: 100%;
          font-family: Helvetica, Verdana, Arial, sans-serif;
          padding: .55em 2em .55em;
          text-shadow: 0 1px 1px rgba(0,0,0,.3);
          -webkit-border-radius: 0.5em;
          -moz-border-radius: 0.5em;
          border-radius: 0.5em;
          -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2);
          -moz-box-shadow: 0 1px 2px rgba(0,0,0,.2);
          box-shadow: 0 1px 2px rgba(0,0,0,.2);
        }
        .button:hover {
          text-decoration: none;
        }
        .button:active {
          position: relative;
          top: 1px;
        }
    </pre>
</div>
<p>And our buttons will look like:</p>
<p><img src="http://coryschires-blog-post-images.s3.amazonaws.com/dynamic_buttons/blue_buttons.png" /></p>
<h4><span>2</span> Extract the important stuff into variables.</h4>
<p>That&#8217;s a lot of CSS but let&#8217;s focus on what actually matters: font color, border radius, border color, padding, and background gradients. </p>
<p>Let&#8217;s move these values into variables. It&#8217;s a good convention to place them at the top of your stylesheet like so:</p>
<div class="code_block">
<pre class="brush: css;">
        /* basic button variables */
        @background_color: #f78d1d;
        @hover_background_color: #f47c20;
        @border_radius: 0.5em;
        @font_size: 14px;
        @font_color: #fef4e9;
        @button_padding: 0.55em 2em;
        @border_color: #da7c0c;

        /* background color gradients for each state */
        @normal_background_top: #faa51a;
        @normal_background_bottom: #f47a20; 

        @hover_background_top: #f88e11;
        @hover_background_bottom: #f06015; 

        @active_background_top: #f47a20;
        @active_background_bottom: #faa51a;

        /* ...previous css with original values substituted for variables... */
    </pre>
</div>
<p>Now, already that&#8217;s a nice improvement. We can easily make changes to our button styles without having to wade through all that CSS. Want rounder corners? Just increase <span class="code_text">@border_radius</span>.</p>
<h4><span>3</span> Make a good thing better with high school math.</h4>
<p>While we&#8217;ve made an improvement, that still feels like too many configuration options for a simple button. Wouldn&#8217;t it be nice, for example, if we didn&#8217;t have to worry with all those color variations (i.e. border color, background colors, gradients, etc). It would be much better if we could consolidate all those values into a single variable (e.g. <span class="code_text">@button_color</span>).</p>
<p>Let&#8217;s do just that. First, let&#8217;s change <span class="code_text">@background_color</span> to <span class="code_text">@button_color</span>. We&#8217;ll hold this value constant and set all of our other color-related values relative to <span class="code_text">@button_color</span>. This means we&#8217;ll be able to alter one variable &mdash; <span class="code_text">@button_color</span> &mdash; and changes will automatically cascade through related variables. </p>
<p>In case you&#8217;re confused, I&#8217;ll break it down a bit so you can see what&#8217;s going on. We&#8217;re just doing some simple math with hexcodes:</p>
<div class="code_block">
<pre class="brush: css;">
        /* we've already set these variables */
        @background_color: #f78d1d;
        @border_color: #da7c0c;

        /* if we know that: */
            @button_color - @border_color = #1D1111
        /* then high school algebra says: */
            @border_color = @button_color - #1D1111
    </pre>
</div>
<p>So we can use this concept to set all our color values relative <span class="code_text">@button_color</span>. Now our configuration options are looking much leaner:</p>
<div class="code_block">
<pre class="brush: css;">
        /* button configuration options */
        @button_color: #f78d1d;
        @border_radius: 0.5em;
        @font_size: 14px;
        @font_color: #fef4e9;
        @button_padding: 0.55em 2em;

        /* color variables set relative to @button_color.
        You probably don't need to change these. */
        @hover_background_color: @button_color - #031100;
        @border_color: @button_color - #1D1111;

        @normal_background_top: @button_color + #031800;
        @normal_background_bottom: @normal_background_top - #062B00; 

        @hover_background_top: @button_color + #010100;
        @hover_background_bottom: @hover_background_top - #082E08; 

        @active_background_top: @normal_background_bottom;
        @active_background_bottom: @normal_background_top;   

        /* ...previous css with original values substituted for variables... */
    </pre>
</div>
<p>So now we only need to concern ourselves with five clearly named variables. And we can easily change our style to suit different themes. Change the value of <span class="code_text">@button_color</span> to <span class="code_text">#ffb515</span> and you get yellow buttons complete with CCS3 gradients:</p>
<p><img src="http://coryschires-blog-post-images.s3.amazonaws.com/dynamic_buttons/yellow_buttons.png" /></p>
<p>Or change <span class="code_text">@button_color</span> to <span class="code_text">#2daebf</span> and <span class="code_text">@font_size</span> to <span class="code_text">22px</span>:</p>
<p><img src="http://coryschires-blog-post-images.s3.amazonaws.com/dynamic_buttons/orange_buttons.png" /></p>
<p>You get the idea. Changing styles couldn&#8217;t be easier.</p>
<h3>But this is about more than just buttons.</h3>
<p>This is just a trivial example of how <a href="http://fadeyev.net/2010/06/19/lessjs-will-obsolete-css/">LESS</a> can make our stylesheets more dynamic. Imagine, for example, WordPress themes where non-developers could alter the entire color scheme by changing a single variable. Or tweak the layout through something like <span class="code_text">@sidebar_width</span>.</p>
<p>When done well, layouts could even feel a bit like jQuery plugins. Just download the files, change a few configuration options, and you&#8217;re ready to roll. This would be especially useful for stylesheets that aren&#8217;t too application specific &mdash; like wireframe layouts or admin/cms interfaces. </p>
<p>Maybe designers will even start open-sourcing layouts the way developers share plugins? Maybe.</p>
<p><a class="download" href="http://github.com/coryschires/CSS3-Buttons-with-LESS">Download the project from GitHub</a></p>
]]></content:encoded>
			<wfw:commentRss>http://coryschires.com/dynamic-css3-buttons-with-less/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

