Emmet uses syntax similar to CSS selectors for describing elements’ positions inside generated tree and elements’ attributes.

Elements

You can use elements’ names like div or p to generate HTML tags. Emmet doesn’t have a predefined set of available tag names, you can write any word and transform it into a tag: div → <div></div>foo → <foo></foo> and so on.

Nesting operators

Nesting operators are used to position abbreviation elements inside generated tree: whether it should be placed inside or near the context element.

Child: >

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

…will produce

Sibling: +

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

…will output

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:

…will be expanded to

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

…outputs to

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

…will output to

Multiplication: *

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

…outputs to

Grouping: ()

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

…expands to

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:

…produces

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

Attribute operators

Attribute operators are used to modify attributes of outputted elements. For example, in HTML and XML you can quickly add class attribute to generated element.

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:

…will output

Custom attributes

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

…outputs

  • You can place as many attributes as you like inside square brackets.
  • You don’t have to specify attribute values: td[colspan title] will produce <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: 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:

…outputs to

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

…outputs to

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 $:

…outputs to

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

…transforms to

You can use these modifiers together:

…is transformed to

Text: {}

You can use curly braces to add text to element:

…will produce

Note that {text} is used and parsed as a separate element (like, divp 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:

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:

…produces

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:

…produces

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:

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:
https://docs.emmet.io/abbreviations/syntax/

发表评论

电子邮件地址不会被公开。 必填项已用*标注