Skip to content

Instantly share code, notes, and snippets.

@krisbulman
Created September 12, 2012 04:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisbulman/3704236 to your computer and use it in GitHub Desktop.
Save krisbulman/3704236 to your computer and use it in GitHub Desktop.
Example for font-icons

Here is a quick project setup and usage tutorial:

Compass Project Creation

  1. Install font-icons
sudo gem install font-icons --pre
  1. Create a new bare bones compass project using the compass install instructions
compass create yourproject --bare --sass-dir "sass" --css-dir "css"

See here for other project creation options with Compass: http://compass-style.org/install/

  1. Go into the project directory "yourproject" and modify the config.rb to do the following:
  • Require the font-icons extension
  • Set the fonts_dir
  • Set relative assets to TRUE by uncommenting that line

What the first few lines of your config.rb should look like..

# Require any additional compass plugins here.

require 'font-icons'

# Set this to the root of your project when deployed:
http_path = "/"
css_dir = "css"
fonts_dir = "css/fonts"
sass_dir = "sass"
images_dir = "images"
javascripts_dir = "javascripts"

# To enable relative paths to assets via compass helper functions. Uncomment:
relative_assets = true

HTML/Sass & Font-Icons

  1. In the directory root, create a simple html template for the test project (see font-icons-test.html gist below)

  2. In the Sass folder, create a file called style.scss and enter the Sass needed to create an icon (see style.scss gist below)

  3. Run the font-icons command for your required font. In this case, we will deploy the icon font set called 'Entypo', notice that the directory and fonts automatically get created.

$ compass install -r font-icons font-icons/Entypo

directory css/fonts/ 
   create css/fonts/entypo.eot 
   create css/fonts/entypo.svg 
   create css/fonts/entypo.ttf 
   create css/fonts/entypo.woff 

Sweet, FontIcons is now installed. You can start using the mixins in your sass project. 

See the included example project for mixin usage.

CSS Generation

  1. Compile your Stylesheets and open the html file in a browser to see the icon in action.
$ compass compile

TIPS

You can pass parameters to the font-icon-base() mixin

// Defaults
@include font-icon-base($family:"iconic", $font-size: 1em, $width: 1em, $margin: 0.8em)

// Example
@include font-icon-base(entypo, 2em, 1em, 0.8em)

In the above example, we only imported one font family.. via compass install -r font-icons font-icons/entypo This copies the actual font files you need into your project's css/fonts folder.

Alternatively, you can import all font families (currently only iconic and entypo) at once into your project using:

compass install -r font-icons font-icons

You can use multiple font families in the same HTML list (or to whatever piece of content you want), see the-alternate.scss

@todo modify the mixin to accept lists of fonts and create less css when multiple fonts are included.


Preview the icon first at: http://krisbulman.github.com/font-icons/

The placeholder names used to include the icons are as close as possible to the original projects with the exception of the font name in front to avoid name collisions.

The following locations will give you the icon placeholder name to @extend in your Sass:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
</head>
<body>
<section class="content">
<h2>Font-Icons</h2>
<ul class="font-icons-example">
<li class="menu-item-001">The Magic Compass</li>
</ul>
</section>
</body>
</html>
// Import font-icons compass extension
@import "font-icons";
// Include the icon font you want
@include font-icon-family(entypo);
ul.font-icons-example {
list-style: none;
list-style-image: none;
li {
// Set the base css to display icons
@include font-icon-base(entypo);
&.menu-item-001 {
// Include the placeholder for the icon
@extend %entypo-compass;
}
}
}
// Import font-icons compass extension
@import "font-icons";
// Include the icon fonts you want
@include font-icon-family(entypo);
@include font-icon-family(iconic);
ul.font-icons-example {
list-style: none;
list-style-image: none;
li {
@include font-icon-base(entypo); // Set the base css to display icons
@include font-icon-base(iconic); // Set the base css to display icons
// Include the placeholder for the icon
&.menu-item-001 { @extend %entypo-compass; }
&.menu-item-002 { @extend %iconic-lightbulb; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment