What exactly is Emmet?
Emmet is a powerful toolkit for developers, serving as a plugin for numerous text editors and drastically enhancing HTML & CSS workflow. It's not just fast – it's lightning-fast. In fact, it boldly claims to be the "essential tool for web developers".
Emmet revolutionizes the concept of snippets by introducing expressions that are dynamically parsed. This means that you can type in abbreviations and watch as Emmet magically generates output based on your input. It's like having a coding genie at your fingertips!
The VS Code team recognized Emmet's incredible success and decided to integrate it directly into their text editor. Now, Emmet comes pre-installed in VS Code, making it a seamless part of your coding environment. Say goodbye to slow, tedious typing and hello to lightning-speed coding.
So, how exactly does Emmet boost your coding speed?
It's all about the abbreviations. With Emmet, you can whip up entire blocks of code in the blink of an eye. Once you master Emmet's abbreviations, you'll be amazed at how much your productivity soars. It's a small effort with a big payoff – the epitome of smart coding.
Let's generate HTML code with Emmet!
1- HTML template
With Emmet you can quickly create an HTML template with just one click, simply type !
and you will see that Emmet has appeared as a suggestion. Now press Enter
. There you have it, a basic blank HTML web page ready to use.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
2- Classes
Emmet uses the same way to refer to classes using a .
. To define a class along with the tag, simply add it like this:
-
div.wrapper
or .wrapper
And the result will be the following:
<div class="wrapper"></div>
It is also possible to add many classes by using the next syntax:
-
div.wrapper.center
or .wrapper.center
And as result:
<div class="wrapper center"></div>
3- ID
The IDs work in a very similar way, for example:
-
div#header-content
<div id="header-content"></div>
Up to this point, let's try the combination of everything we have already seen
-
div#header-content.wrapper.center
or #header-content.wrapper.center
And this is what is going to look like:
<div id="header-content" class="wrapper center"></div>
4- Attributes
We can also specify attributes for the HTML elements like:
img[src="dog.jpg"][alt="dog picture"]
And as result we have:
<img src="dog.jpg" alt="dog picture">
5- Content
To include content inside an HTML tag, we simply wrap the content in curly braces, { }
. For example:
-
p{Content here}
a{Content here}
Then we have:
<p>Content here</p>
<a>Content here</a>
6- Sibling and child tags
This is getting much better! We can also specify sibling and child tags using the characters +
and >
.
For sibling tags:
-
p+p
<p></p>
<p></p>
And for child tags:
-
ul>li
<ul>
<li></li>
</ul>
7- Group
If your structure is very complex, you may want to group tags together to make it more readable.
div>(header>ul>li>a)+(footer>p)
And the result is:
<div>
<header>
<ul>
<li><a href=""></a></li>
</ul>
</header>
<footer>
<p></p>
</footer>
</div>
8- Multiplication and $
We can generate multiple HTML elements by multiplying (*) and numbering elements in sequence using a dollar sign ($).
-
ul>li*5
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
-
ul>li{Item $}*3
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
9- Starting from an specific number with @
-
ul>li.item$@3*5
<ul>
<li class="item3"></li>
<li class="item4"></li>
<li class="item5"></li>
<li class="item6"></li>
<li class="item7"></li>
</ul>
10- Lorem Ipsum
"Lorem Ipsum" is dummy text used by developers to represent data on a page. Simply type lorem
and press Enter
. Emmet will generate 30 words of dummy text that you can use as filler in your project.
-
lorem
Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum, nobis? Commodi non delectus nostrum optio expedita eveniet vero nesciunt eaque facilis animi odio culpa deleniti sint, quas error ipsa ipsum.
It is also possible to define the number of words generated.
lorem10
will give you 10 words of random text:
-
lorem10
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ab, odit!
It is time to apply all what we have learned
Let's try to use everything we have described until now:
-
div>(ul.my-element>li.item$@1*5>lorem20)+(btn{Click me}.click-btn)
<div>
<ul class="my-element">
<li class="item1">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ullam recusandae consequatur nam accusantium velit atque ut, repudiandae porro iure explicabo?</li>
<li class="item2">Nemo distinctio, quam tempore amet aliquam dignissimos, officiis qui libero alias quae incidunt dolore voluptatum ipsam voluptatibus corrupti nesciunt modi.</li>
<li class="item3">Deleniti officiis provident placeat ea iure odio sit, repudiandae ipsam nihil ipsum, praesentium tenetur voluptatem. Exercitationem aperiam labore accusamus illo!</li>
<li class="item4">Eos dolorum velit, ut enim unde quam, quisquam aperiam voluptatibus, modi quae maiores minus soluta vel? Sunt quae voluptatibus quia?</li>
<li class="item5">Qui quaerat harum adipisci cumque numquam? Ducimus hic in, architecto dolore explicabo excepturi necessitatibus, repellendus, cum eaque neque dolor reprehenderit?</li>
</ul>
<button class="click-btn">Click me</button>
</div>
Conclusion
With Emmet, you can generate a highly complex HTML structure in just one line. It's truly amazing. And it also works seamlessly with CSS.
The examples presented are some of the Emmet abbreviations that I commonly use in my daily life, but of course there are many more that you may also find interesting. Don't forget to check the Emmet Cheat Sheet for a long list of what you can do with this tool.
I hope the information provided was engaging and insightful for you. Always strive to simplify your work and improve productivity by leveraging tools and smart techniques in order to make your professional life easier and more efficient.
Top comments (3)
Nice tips! I didn't know about the syntax for the content
p{something here}
.Awesome!
Glad to help!
For Emacs there is zencoding which has support for Emmet.