demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my projects

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

Accelerate Your HTML and CSS Production With Emmet

tools / editors

Estimated reading time: 4 minutes

Emmet (formerly Zen Coding) is best described as a set of macros and keyboard shortcuts for rapid web development. Available as a free download and plugin for over a dozen popular editors, Emmet potentially doubles the production speed of web pages if used consistently. In this article, I’ll take you through installing, using and customizing Emmet to gain maximum results.

Emmet logoMost modern code editors already have built in macros of one kind or another, auto-closing tags and pre-formatting them with typical attributes; Emmet just takes that several steps further. It also has the advantage of being consistent across all platforms and editors: rather than learning a new set of shortcuts when you move from one application to another, you can consistently use Emmet syntax for the quick generation of code.

Installation

Emmet is supported on 14 different IDEs, with slightly different installation for each, so I’ll concentrate on just one: Coda. Simply download the associated file, rename it to Emmet.codaplugin and double-click on it. (Note that you won’t get the usual install window popping up).

Using Emmet For Production

Once Emmet is installed, it’s good to go. Aside from the easy-to-remember shortcut syntax, you have to keep just two things in mind:

  • Adding spaces in Emmet syntax will cause shortcuts to fail
  • You must use the appropriate keyboard shortcut to expand Emmet syntax after you complete typing the code (Control-E)

All right. Let’s say that you’ve started a page with the boilerplate or template of your choice. First, you want to work on the navigation. Typically this would involve typing out a series of nested tags: in HTML5, a <nav> element, <ul> and series of <li> tags with links inside them. With Emmet, we can just type this:

nav>ul>li*5

Which, after pressing Control + E, Emmet magically expands to:

<nav>
  <ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
	   <li></li>
  </ul>
</nav>

Pretty neat, right?  Emmet intelligently uses CSS-style in your HTML code to generate nested elements, with the addition of multipliers for recursion.

You can use the same technique to generate tables very quickly:

table>tr*3>td*5

… which will create a three row, five column table.

But wait, we can do better! By grouping elements in Emmet syntax, we can nest tags together:

nav>ul>(li>a)*5

Expands to:

<nav>
	<ul>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
	</ul>
</nav>

Or try this:

table>(thead>tr>th*5)(tbody>tr>td*5)

…which will produce a table with a header and data row.

We can also use the equivalent of the CSS adjacent sibling selector to create a series of elements one after the other:

header+main+footer

Creates:

<header></header>
<main></main>
<footer></footer>

There are many more possible combinations, but the bottom line is this: you can create the base markup for a page in just a few keystrokes, rather than laboriously entering tags.

Generating filler content

You can use Emmet to quickly generate filter content. Simply expand the keywords lorem or ipsum to create lorem ipsum filler text. Braces denote specialized filler text: for example, to fill a series of null links with the word “Link”:

nav>ul>(li>a[href=#]{Link})*5

Creates:

<nav>
	<ul>
		<li><a href="#">Link</a></li>
		<li><a href="#">Link</a></li>
		<li><a href="#">Link</a></li>
		<li><a href="#">Link</a></li>
		<li><a href="#">Link</a></li>
	</ul>
</nav>

CSS For The Super-Lazy

If you’re working in a stylesheet, Emmet can make life much easier. For example, rather than typing:

margin: 2rem;

With Emmet installed, we can type:

m2r

Which, after applying the Emmet keyboard combo, expands to the same thing.

There are hundreds of such shortcuts for Emmet. The nice part is that you don’t have to use them all at once, or even any of them: the plugin will remain inactive until you use a shortcut and call on it.

Select tags rather than text

Selection options in many IDEs is more akin to word processors than the needs of code editors. Emmet boasts of number of features to make selection of HTML and CSS content easier: rather than gripping and moving a mouse, a few repeated keystrokes can select units of code for editing, copying or deletion.

What’s the difference between Emmet and pre-processors like SASS?

Pre-processors and frameworks map to a target language, but with one important difference: in a pre-processor, the original code is kept, and interpreted/expanded only at runtime. In other words, you work on a SASS document as SASS: it doesn’t automagically expand to CSS as you type. This provides some advantages: it’s easier to go back to the beginning and make a small alteration to the original shortcut code that produces sigificant changes to the output. This power usually comes with some added complication and performance issues.

It might be fair to think of Emmet as a gateway to pre-processors: a way of abstracting the creation of markup and CSS without the more complex setup demanded by something like SASS.

Conclusion

After working in Emmet for some time, I can’t believe I ever lived without it. Emmet’s macros are not a shortcut for learning or : you still have to go through the process of understanding what the languages are, and their syntax. But if you’ve already been coding for a year or so and want to increase your efficiency significantly without battling a learning curve, you can easily grasp the basics of Emmet from the excellent online documentation in an hour or two. After that point, it’s just a question of using the new shortcuts to dramatically cut your time in delivering new code.

comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.