Skip to content

Instantly share code, notes, and snippets.

@jasonkmccoy
Last active August 29, 2015 14:16
Show Gist options
  • Save jasonkmccoy/6fb23fb7952794de2689 to your computer and use it in GitHub Desktop.
Save jasonkmccoy/6fb23fb7952794de2689 to your computer and use it in GitHub Desktop.
Sass List Functions
@charset "UTF-8";
.rss-tag:before, .call-us:before, .listen-now:before {
margin-right: 0.25em;
}
.rss-tag:before {
content: "";
}
.call-us:before {
content: "";
}
.listen-now:before {
content: "";
}
<p>Lists are how Sass represents the values of CSS declarations like <code>margin: 10px 15px 0 0</code> or <code>font-face: Helvetica, Arial, sans-serif</code>. Lists are just a series of other values, separated by either spaces or commas. In fact, individual values count as lists, too: they’re just lists with one item.</p>
<p>On their own, lists don’t do much, but the <a href="http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#list-functions">Sass list functions</a> make them useful. The <a href="http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#nth-instance_method">nth function</a> can access items in a list, the <a href="http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#join-instance_method">join function</a> can join multiple lists together, and the <a href="http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#append-instance_method">append function</a> can add items to lists. The <a href="http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#each-directive">@each rule</a> can also add styles for each item in a list.</p>
<p><a href="http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#lists">Read more on lists</a></p>
<pre lang="scss"><code>$icons: (rss &quot;\f09e&quot;) (phone &quot;\e001&quot;) (headphones &quot;\e008&quot;)
%standard-margin
margin-right: 0.25em
@each $icon in $icons
%icon-#{nth($icon, 1)}:before
@extend %standard-margin
content: nth($icon, 2)
.rss-tag
@extend %icon-rss
.call-us
@extend %icon-phone
.listen-now
@extend %icon-headphones
</code></pre>
<p><a href="http://sassmeister.com/gist/5264586">SassMeister Gist</a></p>

Lists are how Sass represents the values of CSS declarations like margin: 10px 15px 0 0 or font-face: Helvetica, Arial, sans-serif. Lists are just a series of other values, separated by either spaces or commas. In fact, individual values count as lists, too: they’re just lists with one item.

On their own, lists don’t do much, but the [Sass list functions][list-function] make them useful. The [nth function][nth-function] can access items in a list, the [join function][join-function] can join multiple lists together, and the [append function][append-function] can add items to lists. The [@each rule][each-rule] can also add styles for each item in a list.

[Read more on lists][read-more] [list-function]: http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#list-functions [nth-function]: http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#nth-instance_method [join-function]: http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#join-instance_method [append-function]: http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#append-instance_method [each-rule]: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#each-directive [read-more]: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#lists

$icons: (rss "\f09e") (phone "\e001") (headphones "\e008")

%standard-margin
  margin-right: 0.25em

@each $icon in $icons
  %icon-#{nth($icon, 1)}:before
    @extend %standard-margin
    content: nth($icon, 2)
    
.rss-tag
  @extend %icon-rss
  
.call-us
  @extend %icon-phone
  
.listen-now
  @extend %icon-headphones

SassMeister Gist

// ----
// Sass (v3.4.12)
// Compass (v1.0.3)
// ----
$icons: (rss "\f09e") (phone "\e001") (headphones "\e008")
%standard-margin
margin-right: 0.25em
@each $icon in $icons
%icon-#{nth($icon, 1)}:before
@extend %standard-margin
content: nth($icon, 2)
.rss-tag
@extend %icon-rss
.call-us
@extend %icon-phone
.listen-now
@extend %icon-headphones
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment