Nib

CSS3 extensions for Stylus

Nib is a small and powerful library for the Stylus CSS language, providing robust cross-browser CSS3 mixins to make your life as a designer easier.

        
body {
  background: linear-gradient(top, white, black);
}
      
        
body {
  background: -webkit-gradient(linear,
    left top,
    left bottom,
    color-stop(0, #fff),
    color-stop(1, #000));
  background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
  background: -moz-linear-gradient(top, #fff 0%, #000 100%);
  background: linear-gradient(top, #fff 0%, #000 100%);
}
      

Installation

To get started you'll first want to add nib to your package.json file, along with stylus. Once installed you can use Stylus and Nib with your Connect or Express application as shown in the following snippet. The simple .use(nib()) call is all that is required to expose itself to Stylus

        
var connect = require('connect')
  , stylus = require('stylus')
  , nib = require('nib');

var app = connect();

function compile(str, path) {
  return stylus(str)
    .set('filename', path)
    .set('compress', true)
    .use(nib());
}

app.use(stylus.middleware({
    src: __dirname
  , compile: compile
}));

app.listen(3000);
      

From within a .styl file you can then @import nib, or a portion of nib:

        
@import 'nib'
@import 'nib/gradients'
@import 'nib/buttons'
      

Rather than manually @import-ing nib within your Stylus source you can import it via the JavaScript API as well:

        
return stylus(str)
  .set('filename', path)
  .set('compress', true)
  .use(nib())
  .import('nib');
      

Gradients

Nib's gradient support is by far the largest feature it provides, not only is the syntax extremely similar to what you would normally write, it's more forgiving, expands to vendor equivalents, and can even produce a PNG for older browsers with node-canvas.


body {
  background: linear-gradient(top, white, black);
}

yields:

body {
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
  background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
  background: -moz-linear-gradient(top, #fff 0%, #000 100%);
  background: linear-gradient(top, #fff 0%, #000 100%);
}

Any number of color stops may be provided:


body {
  background: linear-gradient(bottom left, white, red, blue, black);
}

Units may be placed before, or after the color:


body {
  background: linear-gradient(left, 80% red, #000);
  background: linear-gradient(top, #eee, 90% white, 10% black);
}

Position mixins

The position mixins absolute, fixed, and relative provide a shorthand variant to what is otherwise three CSS properties. The syntax is as follows:

      
fixed|absolute|relative: top|bottom [n] left|right [n]
    

The following example will default to (0,0):

      
#back-to-top {
  fixed: bottom right;
}
    
yielding:
      
#back-to-top {
  position: fixed;
  bottom: 0;
  right: 0;
}
    

You may also specify the units:

      
#back-to-top {
  fixed: bottom 10px right 5px;
}
    
yielding:
      
#back-to-top {
  position: fixed;
  bottom: 10px;
  right: 5px;
}
    

Clearfix

The clearfix mixin currently takes no arguments, so it may be called as shown below:


.clearfix {
  clearfix();
}

yielding:

.clearfix {
  zoom: 1;
}
.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
}

Border radius

Nib's border-radius supports both the regular syntax as well as augmenting it to make the value more expressive.


button {
  border-radius: 1px 2px / 3px 4px;
}

button {
  border-radius: 5px;
}

button {
  border-radius: bottom 10px;
}

yielding:

button {
  -webkit-border-radius: 1px 2px/3px 4px;
  -moz-border-radius: 1px 2px/3px 4px;
  border-radius: 1px 2px/3px 4px;
}
button {
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
button {
  -moz-border-radius-topleft: 10px;
  -webkit-border-top-left-radius: 10px;
  border-top-left-radius: 10px;
  -moz-border-radius-bottomright: 10px;
  -webkit-border-bottom-right-radius: 10px;
  border-bottom-right-radius: 10px;
}

Responsive

The image mixin allows you to define a background-image for both the normal image, and a doubled image for devices with a higher pixel ratio such as retina displays. This works by using a @media query to serve an "@2x" version of the file.


#logo {
  image: '/images/branding/logo.main.png'
}

#logo {
  image: '/images/branding/logo.main.png' 50px 100px
}

yields:

#logo {
  background-image: url("/images/branding/logo.main.png");
}
@media all and (-webkit-min-device-pixel-ratio: 1.5) {
  #logo {
    background-image: url("/images/branding/logo.main@2x.png");
    background-size: auto auto;
  }
}
#logo {
  background-image: url("/images/branding/logo.main.png");
}
@media all and (-webkit-min-device-pixel-ratio: 1.5) {
  #logo {
    background-image: url("/images/branding/logo.main@2x.png");
    background-size: 50px 100px;
  }
}

Ellipsis

The overflow property is augmented with a "ellipsis" value, expanding to what you see below.


button {
  overflow: ellipsis;
}

yielding:

button {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Reset

Nib comes bundled with Eric Meyer's style reset support, you can choose to apply the global or any specifics that you wish. To view the definitions view reset.styl

Miscellaneous properties

The following properties follow vendor expansion much like border-radius, however without augmentation, as well as some aliases such as whitespace instead of white-space.

Fork me on GitHub