Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Preprocessors

From: CSS Fundamentals

Video: Preprocessors

As powerful as CSS is, there are going to be times when you want to be able to do something that the language just won't let you do. Let me give you a quick example. Let's say that you use this color, which is a burgundy, throughout your entire site. Complex style sheets can contain hundreds of rules. Now, let's say that your client decides to use this flavor of burgundy instead. At this point, you'd better have a good find-and-replace option in your CSS editor, or you're going to be doing a lot of work by hand.

Preprocessors

As powerful as CSS is, there are going to be times when you want to be able to do something that the language just won't let you do. Let me give you a quick example. Let's say that you use this color, which is a burgundy, throughout your entire site. Complex style sheets can contain hundreds of rules. Now, let's say that your client decides to use this flavor of burgundy instead. At this point, you'd better have a good find-and-replace option in your CSS editor, or you're going to be doing a lot of work by hand.

Wouldn't it be easier if you could simply declare a variable for the color at the top of your styles and then reference that variable throughout your code? To change the color site-wide, you could just simply change the color of the variable. Well, that would be great, but you can't do that with CSS--at least not yet. Well, that's where CSS preprocessors come in. CSS preprocessors are programs created to extend the functionality of CSS and make it easier for authors to write their code. They work by allowing authors to write styles using the preprocessor's extended syntax.

This code is then processed by JavaScript or server-side languages, like PHP and Rails, and then generates styles for the browser. Most of the features of a CSS preprocessor are a little beyond the scope of this title, as they begin to move CSS away from a strictly presentational syntax to more of a processing language. However, I do want to give you a brief overview of some of the things that preprocessors allow us to do. We've already mentioned variables, and most preprocessors add those to the mix.

Others allow you to nest rules in much the same way that HTML does, which can make writing styles a bit faster and a lot easier. You can also take class selectors and reuse them in other styles. This ability to mix in styles from one rule to another is an incredibly powerful way to write more efficient styles. Some preprocessors even allow you to pass values into a class, so that the class covers the basic styling of an element, while different values allow you to tweak it in each rule that references it.

In most cases, you can also use operators. Now these allow us to do things like have the margin of one element equal to the margin of another element, plus another number. Now, that way, if the first element changes, the second element would maintain its spacing. Other preprocessors allow you to write simplified code for the new CSS3 features, adding the appropriate vendor prefixes and support for older browsers for you, after the code is processed. While CSS preprocessors are amazingly powerful, they aren't for everyone.

Now first, you have to learn how they work and the syntax necessary for the preprocessor to work. In many ways, it's like learning a new language all over again. Second, your CSS is abstracted from you in a way that's not always acceptable. The CSS is processed and served to the browser when requested, meaning that the author doesn't always see the end result. The resulting CSS is often much less efficient than it would be if carefully authored by a designer. In many cases, JavaScript is required to process the CSS.

In the event that a user has JavaScript disabled, your styles could end up malformed, or not even served at all. Perhaps the final point about preprocessors has really nothing to do with them. Many of the capabilities that preprocessors add to CSS are currently being added through CSS3. Eventually we should be able do the bulk of what preprocessors help us do without using them at all. I also want to caution any new designers out there: preprocessors and their syntax can be very complex.

If you don't understand CSS or are just learning it, I wouldn't recommend a preprocessor until you're a little bit more comfortable with the native syntax of CSS. Of course, as with any tool, you need to decide for yourself if a preprocessor is right for your projects or not. Now, researching and experimenting with them is perhaps the best way to judge for yourself how effective they would be in your own workflow. So, let's take a look. Of the available CSS preprocessors, LESS and SASS are by far the most popular.

LESS, which you can find at lesscss.org, uses syntax that's very similar to CSS's existing syntax, making it very popular with designers and easier to learn for those that might know CSS but maybe aren't as fluent with JavaScript or other scripting languages. SASS, which you can find at sass-lang.com, actually has two syntaxes that you can use: the older default syntax which has more in common with scripting languages, or the new Sassy CSS syntax that's designed to be a bit more like CSS.

After checking out those two, you might want to spend some time exploring Turbine, Switch CSS, CSS Cacheer, CSS Preprocessor, and PCSS. Each has its own unique focus, and they come in varying degrees of complexity. Now CSS preprocessors certainly aren't for everyone, and they usually require a fair amount of experience to implement effectively. They are, however, extremely powerful tools designed to extend CSS and make writing it more efficient.

That alone makes them worth exploring.

Show transcript

This video is part of

Image for CSS Fundamentals
CSS Fundamentals

36 video lessons · 75098 viewers

James Williamson
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS Fundamentals.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.