0%

Usage of Emmet

Usage of Emmet

Emmet is a good tool when coding that can greatly improve your HTML&CSS workflow. Here’s some commonly used command from Emmet Documentation.

Initial HTML

1
2
3
'!' or 'html:5':  generate a HTML5 document;
'html:xt': generate a XHTML document;
'html:4s': generate a HTML4 document;

Child: >

You can use > operator to nest elements inside each other:

1
div>ul>li

…will produce

1
2
3
4
5
<div>
<ul>
<li></li>
</ul>
</div>

Sibling: +

Use + operator to place elements near each other, on the same level:

1
div+p+bq

…will output

1
2
3
<div></div>
<p></p>
<blockquote></blockquote>

Climb-up: ^

With > operator you’re descending down the generated tree and positions of all sibling elements will be resolved against the most deepest element:

1
div+div>p>span+em 

…will be expanded to

1
2
3
4
<div></div>
<div>
<p><span></span><em></em></p>
</div>

With ^ operator, you can climb one level up the tree and change context where following elements should appear:

1
div+div>p>span+em^bq

…outputs to

1
2
3
4
5
<div></div>
<div>
<p><span></span><em></em></p>
<blockquote></blockquote>
</div>

You can use as many ^ operators as you like, each operator will move one level up:

1
div+div>p>span+em^^^bq

…will output to

1
2
3
4
5
<div></div>
<div>
<p><span></span><em></em></p>
</div>
<blockquote></blockquote>

Multiplication: *

With * operator you can define how many times element should be outputted:

1
ul>li*5

…outputs to

1
2
3
4
5
6
7
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>

Grouping: ()

Parenthesises are used by Emmets’ power users for grouping subtrees in complex abbreviations:

1
div>(header>ul>li*2>a)+footer>p

…expands to

1
2
3
4
5
6
7
8
9
10
11
<div>
<header>
<ul>
<li><a href=""></a></li>
<li><a href=""></a></li>
</ul>
</header>
<footer>
<p></p>
</footer>
</div>

If you’re working with browser’s DOM, you may think of groups as Document Fragments: each group contains abbreviation subtree and all the following elements are inserted at the same level as the first element of group.

You can nest groups inside each other and combine them with multiplication * operator:

1
(div>dl>(dt+dd)*3)+footer>p

…produces

1
2
3
4
5
6
7
8
9
10
11
12
13
<div>
<dl>
<dt></dt>
<dd></dd>
<dt></dt>
<dd></dd>
<dt></dt>
<dd></dd>
</dl>
</div>
<footer>
<p></p>
</footer>

With groups, you can literally write full page mark-up with a single abbreviation, but please don’t do that.

ID and CLASS

In CSS, you use elem#id and elem.class notation to reach the elements with specified id or class attributes. In Emmet, you can use the very same syntax to add these attributes to specified element:

1
div#header+div.page+div#footer.class1.class2.class3

…will output

1
2
3
<div id="header"></div>
<div class="page"></div>
<div id="footer" class="class1 class2 class3"></div>

Custom attributes

You can use [attr] notation (as in CSS) to add custom attributes to your element:

1
td[title="Hello world!" colspan=3]

…outputs

1
<td title="Hello world!" colspan="3"></td>

You can place as many attributes as you like inside square brackets.
You don’t have to specify attribute values:

1
td[colspan title]

will produce

1
<td colspan="" title=""> 

with tabstops inside each empty attribute (if your editor supports them).

You can use single or double quotes for quoting attribute values.

You don’t need to quote values if they don’t contain spaces:

1
td[title=hello colspan=3] 

will work.

Item numbering: $

With multiplication * operator you can repeat elements, but with $ you can number them. Place $ operator inside element’s name, attribute’s name or attribute’s value to output current number of repeated element:

1
ul>li.item$*5

…outputs to

1
2
3
4
5
6
7
<ul>
<li class="item1"></li>
<li class="item2"></li>
<li class="item3"></li>
<li class="item4"></li>
<li class="item5"></li>
</ul>

You can use multiple $ in a row to pad number with zeroes:

1
ul>li.item$$$*5

…outputs to

1
2
3
4
5
6
7
<ul>
<li class="item001"></li>
<li class="item002"></li>
<li class="item003"></li>
<li class="item004"></li>
<li class="item005"></li>
</ul>

Changing numbering base and direction
With @ modifier, you can change numbering direction (ascending or descending) and base (e.g. start value).

For example, to change direction, add @- after $:

1
ul>li.item$@-*5

…outputs to

1
2
3
4
5
6
7
<ul>
<li class="item5"></li>
<li class="item4"></li>
<li class="item3"></li>
<li class="item2"></li>
<li class="item1"></li>
</ul>

To change counter base value, add @N modifier to $:

1
ul>li.item$@3*5

…transforms to

1
2
3
4
5
6
7
<ul>
<li class="item3"></li>
<li class="item4"></li>
<li class="item5"></li>
<li class="item6"></li>
<li class="item7"></li>
</ul>

You can use these modifiers together:

1
ul>li.item$@-3*5

…is transformed to

1
2
3
4
5
6
7
<ul>
<li class="item7"></li>
<li class="item6"></li>
<li class="item5"></li>
<li class="item4"></li>
<li class="item3"></li>
</ul>

Text: {}

You can use curly braces to add text to element:

1
a{Click me}

…will produce

1
<a href="">Click me</a>

Note that {text} is used and parsed as a separate element (like, div, p etc.) but has a special meaning when written right after element. For example, a{click} and a>{click} will produce the same output, but a{click}+b{here} and a>{click}+b{here} won’t:

1
2
3
4
5
<!-- a{click}+b{here} -->
<a href="">click</a><b>here</b>

<!-- a>{click}+b{here} -->
<a href="">click<b>here</b></a>

In second example the <b> element is placed inside <a> element. And that’s the difference: when {text} is written right after element, it doesn’t change parent context. Here’s more complex example showing why it is important:

1
p>{Click }+a{here}+{ to continue}

…produces

1
<p>Click <a href="">here</a> to continue</p>

In this example, to write Click here to continue inside <p> element we have explicitly move down the tree with > operator after p, but in case of a element we don’t have to, since we need <a> element with here word only, without changing parent context.

For comparison, here’s the same abbreviation written without child > operator:

1
p{Click }+a{here}+{ to continue}

…produces

1
2
<p>Click </p>
<a href="">here</a> to continue

Notes on abbreviation formatting
When you get familiar with Emmet’s abbreviations syntax, you may want to use some formatting to make your abbreviations more readable. For example, use spaces between elements and operators, like this:

1
(header > ul.nav > li*5) + footer

But it won’t work, because space is a stop symbol where Emmet stops abbreviation parsing.

Many users mistakenly think that each abbreviation should be written in a new line, but they are wrong: you can type and expand abbreviation anywhere in the text.

This is why Emmet needs some indicators (like spaces) where it should stop parsing to not expand anything that you don’t need. If you’re still thinking that such formatting is required for complex abbreviations to make them more readable:

Abbreviations are not a template language, they don’t have to be “readable”, they have to be “quickly expandable and removable”.
You don’t really need to write complex abbreviations. Stop thinking that “typing” is the slowest process in web-development. You’ll quickly find out that constructing a single complex abbreviation is much slower and error-prone than constructing and typing a few short ones.

“Lorem Ipsum” generator

“Lorem ipsum” dummy text is used by many web-developers to test how their HTML templates will look with real data. Often, developers use third-party services to generate “Lorem ipsum” text, but now you can do that right in your editor. Just expand lorem or lipsum abbreviations to get the following snippet:

1
2
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eligendi non quis exercitationem culpa nesciunt nihil aut nostrum explicabo reprehenderit optio amet ab temporibus asperiores quasi cupiditate. Voluptatum ducimus voluptates voluptas?
lorem is not just a normal snippet—it’s actually a generator. Every time you expand it, it will generate a 30-words dummy text, splitted into a few sentences.

You can specify how many words should be generated right in the abbreviation. For example, lorem100 will generate a 100-words dummy text.

Repeated “Lorem ipsum”

You can use lorem generator inside repeated elements to create tags filled with completely random sentences. For example, p*4>lorem abbreviation would generate something like this:

1
2
3
4
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui dicta minus molestiae vel beatae natus eveniet ratione temporibus aperiam harum alias officiis assumenda officia quibusdam deleniti eos cupiditate dolore doloribus!</p>
<p>Ad dolore dignissimos asperiores dicta facere optio quod commodi nam tempore recusandae. Rerum sed nulla eum vero expedita ex delectus voluptates rem at neque quos facere sequi unde optio aliquam!</p>
<p>Tenetur quod quidem in voluptatem corporis dolorum dicta sit pariatur porro quaerat autem ipsam odit quam beatae tempora quibusdam illum! Modi velit odio nam nulla unde amet odit pariatur at!</p>
<p>Consequatur rerum amet fuga expedita sunt et tempora saepe? Iusto nihil explicabo perferendis quos provident delectus ducimus necessitatibus reiciendis optio tempora unde earum doloremque commodi laudantium ad nulla vel odio?</p>

Also, lorem generator utilizes the implicit tag name resolver when lorem element is self-repeated so you can shorten your abbreviations:

1
ul.generic-list>lorem10.item*4

…will produce

1
2
3
4
5
6
<ul class="generic-list">
<li class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nam vero.</li>
<li class="item">Laboriosam quaerat sapiente minima nam minus similique illum architecto et!</li>
<li class="item">Incidunt vitae quae facere ducimus nostrum aliquid dolorum veritatis dicta!</li>
<li class="item">Tenetur laborum quod cum excepturi recusandae porro sint quas soluta!</li>
</ul>