Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Sass and Compass Designer's Cookbook

You're reading from   Sass and Compass Designer's Cookbook Over 120 practical and easy-to-understand recipes that explain how to use Sass and Compass to write efficient, maintainable, and reusable CSS code for your web development projects

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher Packt
ISBN-13 9781783286935
Length 436 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Bass Jobsen Bass Jobsen
Author Profile Icon Bass Jobsen
Bass Jobsen
Stuart Robson Stuart Robson
Author Profile Icon Stuart Robson
Stuart Robson
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Getting Started with Sass FREE CHAPTER 2. Debugging Your Code 3. Variables, Mixins, and Functions 4. Nested Selectors and Modular CSS 5. Built-in Functions 6. Using Compass 7. Cross-Browser CSS3 Mixins 8. Advanced Sass Coding 9. Building Layouts with Sass 10. Building Grid-based Layouts with Susy and Sass 11. Foundation and Sass 12. Bootstrap and Sass 13. Meeting the Bourbon Family 14. Ruby on Rails and Sass 15. Building Mobile Apps 16. Setting up a Build Chain with Grunt Index

Writing Sass or SCSS

Attentive readers have possibly already noticed that the SCSS code has been used to write Sass code. SCSS is the newer and more CSS-like syntax for Sass. This book uses SCSS code in lieu of the older indented Sass syntax. In this recipe, you will learn the differences between the Sass and SCSS syntaxes and why this book prefers SCSS.

Getting ready

For this recipe, you will need only a text editor and the Ruby Sass gem installed. In the Installing Sass for command line usage recipe of this chapter, you can read about how to install the Ruby Sass gem.

How to do it...

Use the following steps to learn how to convert SCSS code into the indented Sass syntax and find out that both syntaxes compile in exactly the same CSS code:

  1. Use your text editor to create a simple SCSS file called test.scss. This file should contain the following SCSS code:
    $color: orange;
    
    p {
     color: $color;
    }
  2. Then, run the following command in your console:
    sass-convert test.scss test.sass
    
  3. The command from the previous step will create a new file named test.sass. Now, open the test.sass file with your text editor and you will find that its content looks as follows:
    $color: orange
    
    p
      color: $color
  4. Now, you can run both the sass test.sass and the sass test.scss commands in your console. Both will give the following output:
    p {
      color: orange; }

How it works...

Currently, two syntaxes are available to write Sass: the original indented syntax, mostly called Sass, and also the newer Sassy CSS (SCSS) syntax, which is an extension of CSS3.

When running the sass test.sass and the sass test.scss commands, the compiler uses the file extension to decide which syntax should be used. In your project, you can import partials in either syntax without any problem. Again, the compiler knows what to compile based on the file's extension. In the Working with partials recipe of this chapter, partials are discussed in more detail.

The sass-convert command-line tool used in this recipe ships with the Ruby Sass gem. After installing the gem, you can directly run the sass-convert command. This command looks at the file extensions too; .sass is converted into .scss or vice versa.

There's more...

As you already know, Sass has been written in Ruby. Ruby is the programming language. You can also use Ruby with Ruby on Rails. Ruby on rails is the Ruby framework for creating web apps. Ruby uses both Sass and HAML. HAML is a markup language used to clearly and simply describe the HTML of any web document without the use of inline code. HAML is a template system for HTML that tries to avoid repetition of code and unnecessary characters by relying on indentation and not the text to determine where the elements and blocks of code begin and end.

The original Sass syntax used the same method of declaration and coding that Ruby does and relies on indentation, as HAML does. This syntax is an excellent fit in Ruby (on rails) and feels already familiar for Ruby developers.

The newer SCSS focuses on the proposal of Sass in being an extension of CSS. Not only does SCSS look like CSS, but any valid CSS code is valid SCSS code too. Because this book is not only intended for Ruby developers, but for anyone who wants to learn Sass, this book uses the newer SCSS syntax for Sass.

See also

  • In the Playing on SassMeister recipe of Chapter 2, Debugging your code, you can read about online test environments for Sass. Also, SassMeister has an option of switching easily between the SCSS and Sass syntaxes.
  • You can read more about HAML at http://haml.info/.
  • In Chapter 2, Debugging your code, you can read about how to use Sass with Ruby on rails.
  • Visit http://sass-lang.com/documentation/file.INDENTED_SYNTAX.html if you want to learn more about the indented syntax.
You have been reading a chapter from
Sass and Compass Designer's Cookbook
Published in: Apr 2016
Publisher: Packt
ISBN-13: 9781783286935
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image