Skip to content

Instantly share code, notes, and snippets.

@jbrains
Created May 31, 2014 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbrains/32a7874da44ef830751f to your computer and use it in GitHub Desktop.
Save jbrains/32a7874da44ef830751f to your computer and use it in GitHub Desktop.
How do I remove the duplication in these SCSS rules?
a.comment-link {
/* Superimposed image */
$icon-comment-height: 10px; /* Must match the size of the graphic */
$icon-comment-width: 10px; /* Must match the size of the graphic */
background: $salmon url(../images/comment.gif) no-repeat $icon-comment-height $icon-comment-width;
color: $salmon;
&:hover {
background: $dark-salmon url(../images/comment.gif) no-repeat $icon-comment-height $icon-comment-width;
color: $dark-salmon;
text-decoration: none;
}
}
@jbrains
Copy link
Author

jbrains commented May 31, 2014

Interpolation? Function? Something else? I don't see it yet.

@jbrains
Copy link
Author

jbrains commented May 31, 2014

I used a mixin. It can become even more context independent by parameterising the image info (URL, height, width).

    a.comment-link {
      @mixin superimposed-comment-image($color) {
        $icon-comment-height: 10px; /* Must match the size of the graphic */
        $icon-comment-width: 10px; /* Must match the size of the graphic */
        background: $color url(../images/comment.gif) no-repeat $icon-comment-height $icon-comment-width;
        color: $color; 
      }

      /* Superimposed image */
      @include superimposed-comment-image($salmon);

      &:hover {
        @include superimposed-comment-image($dark-salmon);
        text-decoration: none; 
      }
    }

@thatrubylove
Copy link

http://krasimirtsonev.com/blog/article/SASS-interpolation-in-a-name-of-variable-nest-variables-within-variables

That is how I do it, although you could use a sass function to return an interpolated string as well.

@thatrubylove
Copy link

ahh, a bit late on the comment, but excellent choice ;)

@jbrains
Copy link
Author

jbrains commented May 31, 2014

Thanks. Evernoted for future reference.

@jbrains
Copy link
Author

jbrains commented May 31, 2014

I suppose I can extract the deeper pattern into a mixin, as well. Let me try that.

@jni-
Copy link

jni- commented May 31, 2014

Not sure why the sass bit is needed here. I find the css-only version simpler here (aside from the nesting).

a.comment-link {
  background: $salmon url(../images/comment.gif) no-repeat 10px 10px;
  color: $salmon; 

  &:hover {
    background-color: $dark-salmon;
    color: $dark-salmon;
    text-decoration: none; 
  }
}

Another thing I like to do is abstract what a color is:

$link-color: $salmon;

a.comment-link {
  background: $link-color url(../images/comment.gif) no-repeat 10px 10px;
  color: $link-color; 

  &:hover {
    background-color: darken($link-color, 10%);
    color: darken($link-color, 10%);
    text-decoration: none; 
  }
}

Not syntax-checked and I'm not sur 10% is the correct darkened %.

@jbrains
Copy link
Author

jbrains commented May 31, 2014

Thanks for the example. I like the notion of abstracting the color; I'm refactoring, so I'm not there yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment