YUI 3.x Home -

YUI Library Examples: CSS Grids: Using Units

CSS Grids: Using Units

YUI Grids comes with a number of predefined units for subdividing your layout. These units are percentage-based, allowing for nesting and resizing.

Creating a layout using grids requires a yui3-g container and any number of yui3-u-* units. To create a layout that splits the units 50/50, use yui3-u-1-2 units.

Note

The only child elements (e.g. direct descendants) of a yui3-g should be yui-3-u-* elements. Any elements within a yui3-g need to be wrapped in a yui3-u-* of some kind, otherwise you may experience side-effects due to the layout system being used.

Basic Markup Structure

  1. <div class="yui3-g">
  2. <div class="yui3-u-1-2"></div>
  3. <div class="yui3-u-1-2"></div>
  4. </div>
<div class="yui3-g">
    <div class="yui3-u-1-2"></div>
    <div class="yui3-u-1-2"></div>
</div>

Provide a Content Container

Styling should be applied to a container within the unit, rather than the unit itself. This allows you to set borders, padding, margins (gutters), etc. without worrying about breaking the layout. For this demo we will give the content a class of content, but can be called whatever you like.

  1. <div class="yui3-g">
  2. <div class="yui3-u-1-2">
  3. <div class="content">
  4.  
  5. </div>
  6. </div>
  7. <div class="yui3-u-1-2">
  8. <div class="content">
  9.  
  10. </div>
  11. </div>
  12. </div>
<div class="yui3-g">
    <div class="yui3-u-1-2">
        <div class="content">
 
        </div>
    </div>
    <div class="yui3-u-1-2">
        <div class="content">
 
        </div>
    </div>
</div>

Adding a Gutter

All units align edge to edge, with zero space in between. You can add space ("gutter"), by simply adding a margin to your content. This is where additional content styling, such as borders, padding, colors, etc. can be applied as well.

  1. <style>
  2. .yui3-g .content {
  3. border: 2px solid #000;
  4. margin-right:10px; /* "column" gutters */
  5. padding: 1em;
  6. }
  7. </style>
<style>
.yui3-g .content {
    border: 2px solid #000;
    margin-right:10px; /* "column" gutters */
    padding: 1em;
}
</style>

Available Units

Class Description
.yui3-u shrinks to fit content (unless given an explicit size)
.yui3-u-1 fills entire width of container
.yui3-u-1-2 fills 1/2 the container
.yui3-u-1-3 fills 1/3 the container
.yui3-u-2-3 fills 2/3 the container
.yui3-u-1-4 fills 1/4 the container
.yui3-u-3-4 fills 3/4 the container
.yui3-u-1-5 fills 1/5 the container
.yui3-u-2-5 fills 2/5 the container
.yui3-u-3-5 fills 3/5 the container
.yui3-u-4-5 fills 4/5 the container
.yui3-u-1-6 fills 1/6 the container
.yui3-u-5-6 fills 5/6 the container
Note:

Because CSS examples are susceptible to other CSS on the page, this example is only available in a new window at the above link.

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings