How To Use Sass Variables - Vanseo Design |
Posted: 13 Oct 2015 05:30 AM PDT Think of all the times you might add the same color to different elements or classes in your CSS file and then think of all the fun you have changing everything when you decide another color would work better. Wouldn't you prefer to make the change once and have the rest update automatically?
Sass Variables are a way to store information that you want to reuse throughout your stylesheet. Variables are coming to CSS, but there's not much support at the moment and it's going to be awhile before you can use them. Fortunately they're available in Sass right now and they're easy to use. A Simple Example Using VariablesTo set a variable in Sass you give the variable a name beginning with a $.
All three are examples of variable names. Due to historical considerations and backwards compatibility, dashes and underscores are interchangeable. In other words $font-size and $font_size are the same variable and you can use either and even mix and match dashes and underscores throughout your document. Setting the value of a variable is just as easy.
The variable names above were arbitrary choices on my part and I'm not suggesting any are good names. My goal was to show you a few different ways you could name variables. You can use single words, dashes, underscores, and camel case notation. Referencing variables in the rest of your code is also quite easy.
All you have to do is use the variable where you would normally add a value. The Sass above will compile to:
If you later decide that Times New Roman would be a better font choice for your main copy, you just change the variable.
When your .scss file (or .sass if you prefer the original syntax) compiles to .css, the value will be updated wherever you used the $primary-type-size variable. In the examples here, I've only used each variable once, but I'm guessing you can see the power of variables given how often we all tend to use the same values throughout a stylesheet. Allowed Characters in Variable NamesI haven't found any definitive information about what characters you can and can't use in variable names. The best I found comes from this Stack Overflow thread and it suggests the following are all allowed.
My advice would be to stick with simple names with simple characters. Use letters, numbers, dashes, and underscores and leave it at that. Name variables like you would classes and ids. Make them semantic and understandable to real people working in the code. While I can't definitively tell you which characters are and aren't allowed, I can definitively say, I've never had a problem with any of the names I've chosen following the advice in the last paragraph. Variable ScopeThe variables in the previous examples have been global variables and they could be used anywhere throughout your Sass file. You can also define variables inside a selector and make them local variables. When a variable is defined locally it's only available within the level of nested selectors where it's defined.
Here I've defined the variable $background in two places, once globally (outside of any selector) and once locally (inside the body selector). I've given each variable a different value. The global $background is white (#fff) and the local $background is a light gray (#ccc). In this example, the body uses the local $background and gets a CSS background color of light gray (#ccc). The local variable overrides the global variable within the scope of the selector. The article selector uses the global $background and gets a background color of white (#fff). You can define a global variable within a selector using
Here the $background variable is defined as global inside the body, which overwrites the global variable set initially. Both body and article will get a light gray background with this code. Default Values in VariablesIf you aren't sure if a variable has an assigned value you can make use of !default.
The idea is if there isn't yet a variable $main-font-size with an assigned value, then it will now be 1em, however, if the variable was previously assigned a value, use that value instead.
Normally when you list the same variable twice with different values, the one that comes second would be the one used. The !default flag, however, says to set the value on the second only if the variable doesn't yet have a value. Since the $main-font-size variable already had a value, Sass will use the initial value and compile to:
!default is useful in mixins (which I'll get to in the next round of this series), because it allows you to write a mixin with a !default value if no value is given, but it also allows that default to be overridden when a value is given. Edwin Morris offers a use case that doesn't involve mixins, though it does involve @imports, which I'll also cover in the next round of this series. Variable InterpolationInterpolation is way to use the value of a variable as part of the name of a selector or property. You define the variable the same way, but you reference it using a different syntax.
Like !default, interpolation is more useful with some things we haven't yet talked about, but here's a simple, if not exactly useful, example so you can see how interpolation works.
Here I defined two variables ($name–1, and $name–2) and then referenced them using interpolation syntax. The Sass compiles to:
You can see that the values of each variable replaces the interpolation syntax. Without the interpolation syntax, the values could only replace the variable where CSS values are normally allowed. Hugo Giraudel offers some realistic use cases for interpolation in an article he wrote for Tuts+. It does talk about some Sass features, we haven't yet talked about here, but you can take a look if you want to know when you might use interpolation. Closing ThoughtsVariables are one my favorite features of Sass. I think they're easy enough to understand and they're also easy work with. The hardest part is deciding what to name them. Variables save time by keeping important values in a single location instead of spread throughout your stylesheet. They help make maintenance simple and they can help you set up prototypes that are quick and easy to change based on client feedback. I'll show you how in a future post. Without question they're the main reason I stopped writing CSS and switched to writing Sass and I think they'll become a favorite of yours as well. Let's leave Sass for now, though I'll come back with another round of posts soon. For the next few weeks I want to get back to SVG and talk about how you can work with text rendered as an SVG. Download a free sample from my book Design Fundamentals. The post How To Use Sass Variables appeared first on Vanseo Design. 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 |
Tidak ada komentar:
Posting Komentar