Skip to content

Element types

Amadej Glasenčnik edited this page Dec 23, 2023 · 1 revision

Element types

In HTML and XML documents, when you expand abbreviations, all abbreviation parts are transformed on-the-fly into HTML/XML tags. But certain elements like a or img are transformed into elements with predefined attributes: <a href=""></a> and <img src="" alt="" />. How does Emmet know when to add those attributes?

All Emmet elements definitions are stored in the options page for that language. You can edit the snippets in the options page.

Snippets

Snippets are just blocks of plain code, just like in all programmers’ editors. You can type anything there and it will be outputted “as-is”, without any transformation.

Abbreviations

Abbreviations are actually building blocks with some data hints. Since Emmet is mostly used for writing HTML/XML tags, abbreviation definition uses XML format to describe element.

Emmet parses abbreviation definition and retrieves the following data:

  • element name;
  • default attributes;
  • attributes’ order;
  • attributes’ default values;
  • should element contain closing tag.

Let’s take a closer look on HTML abbreviations’ definitions above. The link element is defined as <link rel="stylesheet" href="" /> (double quotes should be escaped in JSON; or use single quotes instead). This definition says that tag, generated for link abbreviation, should be named link and should contain two attributes: rel with default value “stylesheet” and href with empty value (exactly in this order), and generated element should not contain closing tag.

When the link abbreviation is expanded, you’ll receive the following output for HTML syntax:

<link rel="stylesheet" href="">

You can override default attribute values and add new ones as well:

link[rel=prefetch title="Hello world"]

...expands to

<link rel="prefetch" href="" title="Hello world">

You can add child elements as well, which forces Emmet to output closing tag:

link>xsl:apply-templates

...will output

<link rel="stylesheet" href="">
    <xsl:apply-templates></xsl:apply-templates>
</link>

Aliases

In the snippets page of the language's options you can also define aliases: a short-hands for commonly used abbreviations. Aliases can be used to define:

  • short names for long tag names;
  • referencing commonly used abbreviations.

In options page, you can find the following definitions:

In the example above, when you expand bq abbreviation, Emmet will look for blockquote abbreviation’s definition. If it doesn’t exist, it will simply output <blockquote></blockquote> element. The ol+ abbreviation actually outputs the same result as ol>li does.

The ol+ definition may look ambiguous since it contains + at the end which is also a sibling operator. Emmet correctly expands such abbreviations and the plus sign is left here for historical reasons. Just remember that you don’t need to use plus sign to create abbreviation alias.