Sass: @-Rules and Directives - Vanseo Design |
Posted: 12 Jan 2016 05:30 AM PST While CSS isn't a programming language, it includes some programming-like features. For example @-rules provide a simple control mechanism for running one block of code instead of another. Sass as you would guess extends these rules and provides a few of its own.
A few months ago I published a series meant to be an introduction to Sass. The series covered the very basics and discussed topics like nesting and variables. If you missed the previous series or would like a Sass refresher, you can find all the posts below.
This time around I want to focus on @-rules and directives in Sass. Today I'll offer some general thoughts before looking at a few specific @-rules. Over the following weeks I'll talk in more detail about some other directives you're likely to use when working with Sass. First a quick refresher about @-rules in CSS CSS @-rulesCSS @-rules are instructions or directives to the CSS parser. One directive you're likely familiar with is @media which provides conditional statements about the size and orientation of the screen among other things. Using @media we can deliver specific sets of styles to different devices.
Here the font-size of the body depends on the conditions of the @media rule. In addition to @media, there are a number of other CSS @-rules you might have used or be familiar with. @charset, @font-face, @import, @namespacing, @supports, and @keyframes for example. Sass supports all CSS @-rules and it adds extra features to some. It also supports a few Sass specific directives that aren't available in CSS alone. Let's look at a few. The @at-root DirectiveThe @at-root directive tells the compiler to move nested code to the root of the stylesheet. It allows you to nest rules inside a selector for the purpose of maintenance while Sass movies the code back to the root of the stylesheet when it compiles to CSS. An example will probably make this clearer. Say you have a 2-column layout with a main content area and a sidebar and both are wrapped with a container div. Your HTML could look like this.
You wouldn't do this in practice, but for this example let's assume it's easier for you to maintain your code if you decide to nest styles for the .main and .sidebar divs inside the ruleset of the .container div.
That means on compile your selectors will have an extra level of specificity as in
Wrapping an @at-root rule around the .main and .sidebar styles moves both selectors back at the root after compiling to CSS.
The @at-root directives also works with multiple levels of nesting. Here an h1 selector is nested two levels deep.
When the code compiles the h1 selector will be at the root.
Notice that the h1 moved to the root of it's most distant ancestor (.container), not it's nearest one (.main). Since .main doesn't use the @at-root directive it remains nested inside .container. A single @at-root directive can also contain more than one selector. Here I added a second heading inside the @at-root.
When the Sass is compiled to CSS both the h1 and h2 selectors are moved to the root of the document.
You may be wondering why you'd want to do any of this and you'd be right to wonder. There probably won't be many times where you want nested Sass to compile to the root of your stylesheet. Keyframe animation is one use case and David Conner offered three more use cases for using @at-root.
Check the previous link for details and David's examples. (without: directive-name) and (with: directive-name)By default anything inside an @at-root directive will bubble up through all its parent selectors. You can also direct @at-root to bubble up outside of nested directives as well. You do this by using (without: directive-name). Here's an example where the h1 is nested inside both a selector (.container) and a directive (@media).
Here's how the Sass is compiled to CSS.
Because the @at-root directives contained (without: media), the h1 bubbled out of the media query. Here's the same Sass after removing (without: media).
The Sass will compile to:
Without the (without: media) part of the directive, the h1 styles have bubbled out of the .container class, which is its furthest ancestor, but it remains inside the @media directive. If you want to break out of multiple directives, you can include both directives inside the (without: directive) separated by a space as in:
There's a corresponding (with: directive-name) to list those directives that should be included instead of excluded.
Finally, there are two special values than can be used with either (without: ) or (with : ). The value "rule" (without: rule) is the same as an @at-root without any query. The value "all" (without: all) moves the styles outside of both directives and all ancestor selectors. The @debug, @warn, and @error DirectivesThis next group of directives are related to each other as you can probably guess by their names. The @debug, @warn, and @error directives can help you correct issues with your Sass code. @debug prints the value of a SassScript expression to the standard error output stream. For example if you add the following line of code (a color function which I'll cover in a future series) to the top of your .scss file:
you'll get back:
In my case the standard error output stream comes through in CodeKit. You might see it in a similar app or your browser. The line number matches the line number where you added the @debug code. While I used the darken function here, you can use any Sass expression (again, more for the next series).
@warn is similar to @debug in that it prints the value of an expression to the standard error output stream. There are two differences when using @warn.
Because you can turn off warnings, if you absolutely must see the feedback, you're better off using @error. @error throws the value of a SassScript expression as a fatal error and includes a trace stack. You would use @error more when working with mixins and functions, but as I haven't covered either in this series yet, I'll hold off on an example. Unfortunately there's no way currently to catch errors. Closing ThoughtsHopefully that wasn't too steep an entry into Sass' @-rules and directives. They're no more difficult to use than CSS @-rules like @media or @font-face. My guess is you won't use @at-root too much and even @debug, @warn, and @error won't find their way into your code until and unless you start writing more complex @mixins and functions. Next week I'll take a look at @media and the features Sass adds to what I assume is a familiar directive. In the weeks that follow I'll talk about @import, @extend, and @mixin, all of which I expect you'll use regularly in your Sass. Download a free sample from my book Design Fundamentals. Join me as I share my creative process and journey as a writer. This posting includes an audio/video/photo media file: Download Now |
You are subscribed to email updates from Vanseo Design. To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 1600 Amphitheatre Parkway, Mountain View, CA 94043, United States |