Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class='display'>
  <h1>Sprites Mixin For Sass</h1>
  <div class="steps">

    <div class="step">
      <h4>1. Create a sprite of square images </h4>
      <p>
<ol>
  <li>If the icon is not square, save it inside a square anyway.</li>
  <li>should be pixel perfect!</li>
</ol>  
        

      </p>
      <div class='sprite-demo'></div>
    </div>

    <div class="step">
      <h4>2. Define sass map with the specified keys </h4>
      <code>
        $mySprite:(<br/>  <bold>url:</bold> '../images/mySprite.png', <br/>
        <bold>names:</bold>('facebook', 'twitter', 'youtube', 'instagram', ... 'facebookGray','twitterGray' ...),<br/>
        <bold>columns:</bold> 7,<br/>
        <bold>rows:</bold> 2<br/>
        );
    </code>
    </div>

    <div class="step">
      <h4>3. Use it like this: </h4>
      <code>.selector{<br/>
        <bold>@include</bold> backgroundImageBySprite( $mySprite, 'linkedinGray', 30px)<br/>     
        }<br/>
      </code>
      <p>
        <h5>arguments: </h5> sprite map, <span class='gray'> ($mySprite)</span>
        <br/> name of icon,<span class='gray'> (one of $mySprite.names)</span>
        <br/> desired size <span class='gray'>(if it's in percentage, it should be set in a square parent)</span>
      </p>
    </div>

    <div class="step">
      <h4>4. Result: </h4>
      <div class="frame">
        <div class="selector"></div>
        <div class="selector2"></div>
        <div class="selector3"></div>
      </div>
    </div>

  </div>
</div>
              
            
!

CSS

              
                //======== mixin and functions =========//
//(initializtion below)

@function divideEscape0($a,$b){
  @if ($b ==0){@return 0}
  @return $a/$b;
}
@function getImagePositionFromSprite($iconName,$sprite-name,$columns,$rows){
  $index: index($sprite-name,$iconName);
  $row: ceil($index/$columns);
  $column: $index % $columns;
  @return percentage(divideEscape0(1,($columns - 1))*($column - 1)) percentage(divideEscape0(1,($rows - 1))*($row - 1));
}

@mixin backgroundImageBySprite($sprite,$name,$size){
  background-image: url(map_get($sprite,url));
  background-position: getImagePositionFromSprite(
          $name,
          map_get($sprite,names),
          map_get($sprite,columns),
          map_get($sprite,rows)
  );
  height: $size;
  width: $size;
  background-size: auto $size * (map_get($sprite,rows));
}

//end of mixin//
//===================================//



//===================================//
//initializtion// 

//1. create a sprite of square images
$spriteImageExample:'http://www.caltronics.info/images/unused/social-icons.example.png'; 
//is should be pixel-perfect!!


//2. define sass map with the keys specified:
$mySprite:(
    url:$spriteImageExample, 
    names: (
      'facebook', 'twitter', 'youtube','instagram','linkedin','tumblr','google+','facebookGray', 'twitterGray', 'youtubeGray','instagramGray','linkedinGray','tumblrGray','google+Gray'
    ),  //names of all of the icons in sprite
    columns: 7,   //sprite structure
    rows: 2
);
 
//3. use it like this:
.selector{
  @include backgroundImageBySprite($mySprite,'linkedinGray',60px)    
    //try changing to other icons
}

//you can create a mixin for more convenience:

@mixin social-icon($name,$size){
  @include backgroundImageBySprite($mySprite,$name,$size)
}

 .selector2{
  @include social-icon('google+',30px)    
}
.selector3{
  @include social-icon('facebook',80px)     
}
//=======================================//



//======= some styling for code pen ========//
html{
  background-color:rgb(255, 230, 241);
    color:  #023581;
}
.display{
  h1{
      text-align: center;
  color: rgb(255, 230, 241);
  background-color: rgb(115, 171, 255);
  line-height: 60px;
  }
}
.steps{
  height: 100%;
  display: flex;
  .step{
    flex-basis: 25%; 
    overflow: auto;
      margin-right: 15px;
  padding: 13px;
  background-color: rgba(115, 171, 255, 0.5);
    h5{
      margin: 0;
      text-decoration: underline;
    }
  }
}
.sprite-demo{
  width: 300px;
  height: 85px;
  background-size: auto 85px;
  background-image: url($spriteImageExample);
    display: inline-block;
}
code{
  background-color:rgb(222, 222, 222);
  color:black;
  padding:5px;
  display:block;
  bold{
  font-weight: 900;
  margin-left: 20px;
}
} 
.gray{
  color:gray;
}
.frame{
  background-color: #000;
  padding: 20px;
}
.selector3,.selector2{
  margin-top: 10px;
}
 

              
            
!

JS

              
                
              
            
!
999px

Console