Permalink
Browse files

Added English episode 334.

  • Loading branch information...
1 parent 8870aa7 commit acea92dda7298eda2d8af36d48ad9f782a360759 @eifion committed Mar 25, 2012
Showing with 252 additions and 0 deletions.
  1. +252 −0 episodes/334 - Compass & CSS Sprites/en.html
@@ -0,0 +1,252 @@
+<style type="text/css">.asciicast .imageWrapper { width: 900px; overflow: auto; text-align: center; }</style>
+
+<p><a href="http://compass-style.org/">Compass</a> makes it more convenient to generate stylesheets with SASS by providing useful mixins, functions and more. The new <a href="https://github.com/Compass/compass-rails">CompassRails gem</a> makes it easier than ever to integrate it with Rails&rsquo; asset pipeline. Below is a simplified version of the Railscasts site design. We&rsquo;re going use Compass to improve the underlying styling for this application while maintaining the same look.</p>
+
+<div class="imageWrapper">
+ <img src="http://asciicasts.com/system/photos/1086/original/E334I01.png" width="950" height="500" alt="The page from the Railscasts site."/>
+</div>
+
+<p>To add Compass to a Rails app we just need to add the CompassRails gem to the <code>assets</code> group in the gemfile and then run <code>bundle</code>.</p>
+
+``` /Gemfile
+# Gems used only for assets and not required
+# in production environments by default.
+group :assets do
+ gem 'sass-rails', '~> 3.2.3'
+ gem 'coffee-rails', '~> 3.2.1'
+
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
+ # gem 'therubyracer'
+
+ gem 'uglifier', '>= 1.0.3'
+ gem 'compass-rails'
+end
+
+gem 'jquery-rails'
+```
+
+<p>Next we&rsquo;ll need to rename our <code>application.css</code> file to <code>application.css.scss</code>. This turns it into a SASS-interpreted file and it&rsquo;s necessary to do this so that we have a location where we can load Compass and share it across our application&rsquo;s SASS files. Sprockets can&rsquo;t give us this functionality so we&rsquo;ll remove its manifest from this file and instead use SASS to import the necessary files. Our application has a <code>layout.css.scss</code> file so we&rsquo;ll import that as well as the <code>compass</code> file.</p>
+
+``` /app/assets/application.css.scss
+@import "compass";
+@import "layout";
+```
+
+<p>As we&rsquo;ve added a gem to our application we&rsquo;ll need to restart the server. When we reload the page after doing that our page looks just as it did before and that&rsquo;s good. The only difference is that we can now use Compass to improve our SASS code.</p>
+
+<h3>Using Compass in Our Application</h3>
+
+<p>Adding the <code>compass</code> file to our application gives us access to Compass&rsquo;s new functionality. Compass makes it easier to work with CSS3 and with it we can add CSS features without needing to specify browser vendor prefixes for gradients, box shadows or border radiuses. The Railscasts application uses a gradient and these are documented in the <a href="http://compass-style.org/reference/compass/css3/images/">images section</a>. We&rsquo;ll use Compass to improve the code in the navigation bar. This styling is handled by the layout file.</p>
+
+``` /app/assets/stylesheets/layout.css.scss
+#nav_bar {
+ position: relative;
+ padding: 8px 100px;
+ margin-bottom: 15px;
+ border-top: solid 1px #FFF;
+ border-bottom: solid 3px #DE9F00;
+ background-color: #333;
+ background-image: -webkit-linear-gradient(#5c5c5c, #111111);
+ background-image: -moz-linear-gradient(#5c5c5c, #111111);
+ background-image: -ms-linear-gradient(#5c5c5c, #111111);
+ background-image: linear-gradient(#5c5c5c, #111111);
+
+ .nav {
+ float: right;
+ padding-top: 2px;
+ li {
+ padding-left: 25px;
+ font-size: 14px;
+ color: #BBB;
+ a { color: #FFF; }
+ }
+ }
+}
+```
+
+<p>In this file we define the gradients four times to allow for different vendor prefixes. With Compass we don&rsquo;t need to do this. Instead we can use just one line of code.</p>
+
+``` /app/assets/stylesheets/layout.css.scss
+@include background-image(linear-gradient(#5c5c5c, #111111));
+```
+
+<p>This will generate all the browser-specific prefixes for us. When we reload the page now it still looks the same but if we look at the generated CSS we&rsquo;ll see the all the gradients that Compass has generated.</p>
+
+``` /assets/application.css
+/* line 69, ../../app/assets/stylesheets/layout.css.scss */
+#nav_bar {
+ position: relative;
+ padding: 8px 100px;
+ margin-bottom: 15px;
+ border-top: solid 1px #FFF;
+ border-bottom: solid 3px #DE9F00;
+ background-color: #333;
+ background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #5c5c5c), color-stop(100%, #111111));
+ background-image: -webkit-linear-gradient(#5c5c5c, #111111);
+ background-image: -moz-linear-gradient(#5c5c5c, #111111);
+ background-image: -o-linear-gradient(#5c5c5c, #111111);
+ background-image: -ms-linear-gradient(#5c5c5c, #111111);
+ background-image: linear-gradient(#5c5c5c, #111111);
+}
+```
+
+<h3>Using CSS Sprites</h3>
+
+<p>If all we want from Compass are the CSS3 conveniences then we could use <a href="http://thoughtbot.com/bourbon/">Bourbon</a> instead as we showed in <a href="http://railscasts.com/episodes/330-better-sass-with-bourbon">episode 330</a>. Compass provides much more, though, including CSS sprites. CSS sprites are a technique for combining a number of small images into one larger image and then using CSS to show just a part of each image. On our page we have a row of icons that let you subscribe to Railscasts in a variety of ways. Each of these icons is a separate image which means that a separate HTTP request will be made to get each one. It&rsquo;s good practice to reduce the number of requests that a page makes. It we merge all these icons into one file we can use CSS to display a selected part of that image for each icon.</p>
+
+<div class="imageWrapper">
+ <img src="http://asciicasts.com/system/photos/1086/original/E334I01.png" width="950" height="500" alt="The page from the Railscasts site."/>
+</div>
+
+
+<p>CSS sprites can be a pain to implement manually but Compass makes it easy. The icons are currently being handled by calls to <code>background-image</code>.</p>
+
+``` /app/assets/stylesheets/layout.css.scss
+#subscribe {
+ position: absolute;
+ bottom: 10px;
+ right: 100px;
+ li {
+ position: relative;
+ margin-left: 8px;
+ font-size: 12px;
+ }
+ a {
+ display: block;
+ width: 34px;
+ height: 34px;
+ }
+ .itunes { background-image: image-url("icons/itunes.png"); }
+ .twitter { background-image: image-url("icons/twitter.png"); }
+ .facebook { background-image: image-url("icons/facebook.png"); }
+ .rss { background-image: image-url("icons/rss.png"); }
+}
+```
+
+<p>To use Compass&rsquo;s sprites we first need to take all the images we want to combine and put them in the same directory. Ours are already in <code>/app/assets/images/icons</code> so we don&rsquo;t need to do this. We can load the images into Compass by calling <code>@import</code> and passing in the path.</p>
+
+``` /app/assets/stylesheets/layout.css.scss
+@import "icons/*.png";
+```
+
+<p>This will load up all the images in that directory and make a single sprite image from them. There are couple of different ways that we can use this. The more manual approach is to replace the call to <code>background-image</code> for each icon.</p>
+
+``` /app/assets/stylesheets/layout.css.scss
+.itunes { @include icons-sprite(itunes); }
+.twitter { @include icons-sprite(twitter); }
+.facebook { @include icons-sprite(facebook); }
+.rss { @include icons-sprite(rss) }
+```
+
+<p>Again the page looks the same when we reload it but all the icons are now merged down into a single image so that they can all be downloaded in a single request.</p>
+
+<div class="imageWrapper">
+ <img src="http://asciicasts.com/system/photos/1087/original/E334I02.png" width="408" height="279" alt="The icons combined into a single image by Compass."/>
+</div>
+
+<p>Compass provides a more convenient way to do this. Instead of loading the sprites individually into a separate CSS class we can do it all through Compass. We can remove the CSS for the four classes that represent the icons and then replace them with this.</p>
+
+``` /app/assets/stylesheets/layout.css.scss
+@import "icons/*.png";
+@include all-icons-sprites;
+```
+
+<p>This will automatically make a separate CSS class for each of these images. We&rsquo;ll need to change the HTML so that it references them. Each icon is displayed in a list like this.</p>
+
+``` /app/views/layouts/application.html.erb
+<ul id="subscribe" class="horizontal">
+ <li><a class="itunes" href="/itunes"></a></li>
+ <li><a class="twitter" href="/twitter"></a></li>
+ <li><a class="facebook" href="/facebook"></a></li>
+ <li><a class="rss" href="/rss"></a></li>
+</ul>
+```
+
+<p>We just need to prefix each class with the name of the directory, in this case <code>icons</code>, and a dash.</p>
+
+``` /app/views/layouts/application.html.erb
+<ul id="subscribe" class="horizontal">
+ <li><a class="icons-itunes" href="/itunes"></a></li>
+ <li><a class="icons-twitter" href="/twitter"></a></li>
+ <li><a class="icons-facebook" href="/facebook"></a></li>
+ <li><a class="icons-rss" href="/rss"></a></li>
+</ul>
+```
+
+<p>The page still looks the same now but the generated CSS references the single image and uses <code>background-position</code> to show the relevant part of it for each icon.</p>
+
+``` /assets/application.css
+/* line 66, icons/*.png */
+.icons-sprite, .icons-facebook, .icons-itunes, .icons-rss, .icons-twitter {
+ background: url(/assets/icons-s1286bd1660.png) no-repeat;
+}
+
+/* line 60, ../../../.rvm/gems/ruby-1.9.3-p125/gems/compass-0.12.1/frameworks/compass/stylesheets/compass/utilities/sprites/_base.scss */
+.icons-facebook {
+ background-position: 0 -102px;
+}
+```
+
+<p>There&rsquo;s more information about sprites in the <a href="http://compass-style.org/help/tutorials/spriting/">&ldquo;Spriting With Compass&rdquo; tutorial</a>.</p>
+
+<h3>Simple Horizontal Lists</h3>
+
+<p>All of Compass&rsquo;s documentation is good and it&rsquo;s well worth taking the time to browse through the Reference and Help sections to get an idea as to what else Compass provides. There are many useful utility functions and <a href="http://compass-style.org/reference/compass/typography/lists/horizontal_list/">one of these</a> makes it much easier to generate horizontal lists. Lists are often used for rendering navigation menus and we can use the <code>horizontal-list</code> mixin to simplify this. The Railscasts site has a <code>.horizontal</code> CSS class which does just that and its SASS code looks like this:</p>
+
+``` /app/assets/stylesheets/layout.css.scss
+ul.horizontal { list-style: none;
+ margin: 0;
+ padding: 0;
+ li {
+ margin: 0;
+ padding: 0;
+ float: left;
+ }
+}
+```
+
+<p>We can replace this with a call to that mixin.</p>
+
+`` /app/assets/stylesheets/layout.css.scss
+ul.horizontal {
+ @include horizontal-list;
+}
+```
+
+<p>Each horizontal list on the page is still rendered horizontally now, although we may need to adjust the margins or padding a little for each one.</p>
+
+<h3>Compass Reset</h3>
+
+<p>Compass Reset is another useful feature. Browsers have different default values for sizing and spacing different elements. Compass Reset will set everything to zero so that we can build styles back up consistently across different browsers. To use it we just need to import it in our <code>application.css.scss</code> file.</p>
+
+``` /app/assets/stylesheets/application.css.scss
+@import "compass";
+@import "compass/reset";
+@import "layout";
+```
+
+<p>This is one of the few compass commands that will insert CSS into our layout so we need to be aware of this when using it. With it in place the &ldquo;Welcome&rdquo; text which is in an <code>h1</code> element is much smaller.</p>
+
+<div class="imageWrapper">
+ <img src="http://asciicasts.com/system/photos/1088/original/E334I03.png" width="800" height="500" alt="The text in the h1 element has had its styling changed by Compass Reset."/>
+</div>
+
+<p>Most sites will look worse after the Reset CSS is added as most designs rely on the browsers&rsquo; default styles to some degree. With the Reset in place, though, we can now build the styles up in a way that&rsquo;s consistent across different browsers. For example we can now specifically declare how we want <code>h1</code> elements to be rendered.</p>
+
+``` /app/assets/stylesheets/layout.css.scss
+h1 {
+ font-size: 28px;
+ font-weight: bold;
+ margin: 18px 0;
+}
+```
+
+<p>Now our page looks like it did before.</p>
+
+<p>CSS Resets aren&rsquo;t a good thing all the time, but if it&rsquo;s important that your design is consistent across browsers then it&rsquo;s worth considering using one. For an alternative to Compass Reset take a look at <a href="http://necolas.github.com/normalize.css/">normalize.css</a>. This sets defaults that are closer to the browsers&rsquo; defaults which means that we don&rsquo;t need to rebuild every element up from scratch.</p>
+
+<h3>Further Reading</h3>
+
+<p>That&rsquo;s it for this episode. There are a number of features we haven&rsquo;t covered so be sure to take a look at the web site for more information. One part of Compass that we haven&rsquo;t covered is the <a href="http://compass-style.org/reference/blueprint/">blueprint module</a>. This is an entire CSS framework for building grid-based designs. It&rsquo;s also worth reading the README for the CompassRails for some alternative approaches to integrating them together. If you&rsquo;re using an earlier version of Rails there&rsquo;s some useful information there on getting it working with CompassRails.</p>
+
+<p>Although Compass is a large framework it is very modular. This means that we can include just the parts we need for our application.</p>

0 comments on commit acea92d

Please sign in to comment.