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 | '!' or 'html:5': generate a HTML5 document; |
Child: >
You can use >
operator to nest elements inside each other:
1 | div>ul>li |
…will produce
1 | <div> |
Sibling: +
Use +
operator to place elements near each other, on the same level:
1 | div+p+bq |
…will output
1 | <div></div> |
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 | <div></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 | <div></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 | <div></div> |
Multiplication: *
With *
operator you can define how many times element should be outputted:
1 | ul>li*5 |
…outputs to
1 | <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 | <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 | <div> |
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 | <div id="header"></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 | <ul> |
You can use multiple $
in a row to pad number with zeroes:
1 | ul>li.item$$$*5 |
…outputs to
1 | <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 | <ul> |
To change counter base value, add @N
modifier to $
:
1 | ul>li.item$@3*5 |
…transforms to
1 | <ul> |
You can use these modifiers together:
1 | ul>li.item$@-3*5 |
…is transformed to
1 | <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 | <!-- a{click}+b{here} --> |
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 | <p>Click </p> |
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 | 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? |
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 | <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> |
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 | <ul class="generic-list"> |