Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS to Sass Refactoring with Sass Font Stack and Asset Variables

Victor Gil Borrego
Victor Gil Borrego
13,435 Points

Using a variable for border-radius, instead of a placeholder?

Hi,

in the video you replace the border-radius: 10px with a variable for 10px, why don't you use a placeholder to replace the whole rule?

What are the pros and cons of using a variable instead of a placeholder for things like that?

4 Answers

Brad Lacke
Brad Lacke
7,438 Points

Not sure if you're still stewing over this one, Victor, but what I gather the utilities of variables vs. @extends are is that with variables, you're leaving the rule essentially in place and just swapping out the value, like in this example you're basically saying "I want a consistent border radius on my page, and right now I'm thinking 10px is it". This way you can see what's happening in the code, and know that you have one place to go to adjust the value if you need to.

@extends, on the other hand, are more useful when you have several repeating rules with exactly the same values, and you're just trying to chunk all that repetitive code out and have it in just one place. You're not necessarily going to fiddle with these rules as much, you just don't want to see them over and over again in the code. I'm a little baffled by the single-rule extends that Guil is using in this course - I'll go out on a limb and say that they're more for the purposes of demonstration, because I don't know that they'd be at all useful out in the wild.

Great explanation, Brad.

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Victor Gil Borrego

Variables are best used for property values -- like a color that you want to use in multiple properties, such as border, color, background-color, and so on.

Use placeholders for one or more declarations (property and value pairs). Placeholders are great for assigning the same set of declarations to more than one style. Here's an example of Sass using placeholders:

%icon {
  transition: background-color ease .2s;
  margin: 0 .5em;
}

.error-icon {
  @extend %icon;
  background-color: red;
}

.info-icon {
  @extend %icon;
  background-color: green;
}

And here's the final CSS that's generated:

.error-icon, .info-icon {
  transition: background-color ease .2s;
  margin: 0 .5em;
}

.error-icon {
  background-color: red;
}

.info-icon {
  background-color: green;
}

The placeholder creates a group selector .error-icon, .info-icon with the common properties shared by the two styles. This means you don't need to repeat that same code in two different places.

Garrett Levine
Garrett Levine
20,305 Points

Victor,

I imagine you're asking whether or not you should make this into a class vs. styling each element individually. I think this falls back on the concept of 'DRY' programming. If you notice that you're writing a ton of code, giving EVERYTHING the same border radius style, then it is best to write it ONE place, and apply a class to the elements you want styled with a class, or a specific selector. For example, if you're styling a ton of images with a border, instead of going through your HTML and giving every image a class, you can just select files that end/begin with certain words. This would look something like this;

img[src$=".jpg"] {
  border-radius: 10px;
}

the above would target every image ending in .jpg and apply the desired border to it. If you're fuzzy on selectors, take a look at the selectors CSS course, as it is pretty in depth on the cool selectors you can use to make your CSS a little less 'class' heavy.

Victor Gil Borrego
Victor Gil Borrego
13,435 Points

Hi Garrett,

the thing that I was thinking about was more something between these:

%b-r:{
    border-radius: 10px;
}

vs

.class{
   border-radius: $b-r;
}

I get where you going with this, but I think that Brad sums it up well. It seems that a placeholder isn't as succinct as a variable in this case and that defining a simple variable in Sass for the value of this particular border-radius is more efficient.