code icon

CSCI 1720

Intermediate Web


Emmet

Emmet is a 'plugin' that is included with Visual Studio Code. It is compatible with many other editors as well, but we're using VSC in this class, so that's where we'll focus for now

Emmet enables more rapid creation of code in, among other things, HTML and JavaScript. It also works with CSS, but CSS code is so brief already that it makes little difference
It takes a little getting used to, but - once you do - it will speed up your coding a great deal. It will also help mitigate typographical errors; the more you type by hand, the more likely you are to make mistakes!

Quick Example

Create HTML doc from scratch

Syntax

Using Emmet with HTML is pretty easy to learn. Type the name of an element and press the TAB (or Enter) key, and Emmet will do the rest:

Auto-completing elements

We can type in any element name + TAB to auto-generate the element we need

Classes

If we want to include a class (or classes) in our element's opening tag, we use the standard dot ('.') format. Chaining multiple classes just requires them to be separated by dots

Adding a class or classes

IDs

Like classes, we can shorthand IDs into our opening tags. The difference between the two is that an element can only have one ID

Adding IDs

Combining Classes & IDs

We can add both classes and IDs to our opening tags by chaining the same notation

Combining classes & IDs

Just like with vanilla HTML, the order in which we add the classes & IDs doesn't matter

Combinators

We can use the combinators we learned when we studied CSS to perform similar actions with Emmet. For example, say we wanted a div element with a nested p element:

Combining classes & IDs

Sometimes, we may need to adjust the indentation a little, but we're still saving a lot of time & typing

We can also add sibling elements within a parent element

Adding child and sibling elements

We can use cominations of combinators to create any document structure we can visualize, but these are the ones I use most

Repetition

Say we have to add an unordered list nested within a div that has five list items. We can easily do that with Emmet

Combining classes & IDs

That's the basics of Emmet. Again, it takes a little getting used to. But once you do, you'll save yourself a lot of time and maybe even a little grief

For more information about Emmet and its capabilities, check https://docs.emmet.io/. You can download a more comprehensive Emmet Cheatsheat, if you're interested