How to Boost Your Coding Life by Powerful VS Code Emmet
One tap, Big solution…🥳🥳🥳
“While others were dreaming about it — I was getting it done.”
― Nathan W. Morris
Programmers are usually very lazy🥱 but they also have a deadline to submit their task in time. That’s why to make them more lethargic but efficient, emmets are discovered😎. Didn’t hear about that??? Don’t worry, hold tight…I am giving you all the details of emmet in VS code.
What is Emmet?
As the official website of emmet says, “Emmet is a plugin for many popular text editors which greatly improves HTML & CSS workflow”. But we will discuss the emmets in VS code only as according to wakatime 2021 stats, it is way more ahead of other competitors in case of worldwide usage. Also though Emmet abbreviation and snippet expansions are enabled by default in html
, haml
, pug
, slim
, jsx
, xml
, xsl
, css
, scss
, sass
, less
and stylus
files, as well as any language that inherits from any of the above like handlebars
and php
, we’ll focus on mainly HTML and CSS. Using emmets, we can quickly write complex code in a short time with simple abbreviations.
Enabling and Settings:
All emmet snippets and expansions are always installed in VS code. So no extension is required.
But you can always change your settings for your own customisation by editing your settings.json file, which can be found at the following path —
C:\Users\UserName\AppData\Roaming\Code\User
Expanding using Tab:
Usually, emmet suggestions pop up, which expand on pressing Enter. But if you want to use Tab for expanding the emmets, you can add the following code in the settings.json file —
Order of Emmets Suggestion:
If you want that emmet suggestions will always be on the top of the suggestion list, you can add the following code in the settings.json file —
To Disable Emmet Suggestion:
If you don’t want to see the emmet abbreviations in your suggestion list (I don’t think you wanna do😉), you can use the following code in the settings.json file —
Common Emmet Abbreviations in HTML:
To start an HTML program:
!
Output:
!!!
Output:
Nesting:
To generate nested elements, we will use the ‘>’ operator.
nav>div>ul>li>a
Output:
Multiplication:
We can repeat one code by using the ‘*’ operator.
ul>li*4>a
Output:
Sibling:
To generate siblings, we can use +
operator.
header+section+footer
Output:
Adding Text:
If we want to add text inside an element, we can use the ‘{}’ operator.
p{This is an example!}
Output:
Adding Class:
We can create a class name using the ‘dot(.)’.
div.div-example>header>h1.h1-example+nav.nav-example
Output:
Multiple Class Name:
We can use multiple class names on a single element. Suppose we want to type four classes like .red, . yellow, .orange, .black in a single element, then we can type
.red.yellow.orange.black
Output:
Adding id:
We can create an id name by using the ‘#’.
div#div-example
Output:
We can use multiple classes in a tag, but we can't use multiple ids in a tag. If we specify two ids using ‘#’, the second id will override the first.
div#red#yellow
Output:
Adding Attribute:
We can add attributes by using ‘[]’.
input[type=email].mail
Output:
If we have a source image ‘ image.png’ with ‘ image’ as alt tag, then we can type
img[src=image.png alt=image]
Output:
Grouping:
We can group code together by using ‘()’.
ul>(li>a)*5
Output:
Let’s try a complex example —
(table>tr>td)+ul>li
Output:
Climb up:
The climb up ‘^’operator is used to move up one level in the tree structure. This can come in handy when using the child operator ‘>’.
section>div>h2>a^p
Output:
Automatic Numbering:
We can automatically add numbers in HTML by using ‘$’.
ul>li$*4>a
Output:
Random Text:
If we want a random only lorem ipsum placeholder text, then we can type ‘Lorem’.
Lorem
Output:
If we want some text with 20 words long from lorem ipsum, then we can type ‘Lorem20’.
Lorem20
Output:
We can also use the multiplication operator with lorem ipsum:
Lorem*3
Output:
Link Generate:
We can add CSS files faster using emmet abbreviation.
link:css
Output:
We can use the ‘+’ sign to add CSS links to generate faster resources.
head>link:css+link:favicon
Output:
Expansions of HTML tags:
Some HTML tags which we use repeatedly, emmets help to write those tags very fast. Let’s see those emmet abbreviations.
script:src
Output:
input:t
Output:
input:c
Output:
a:link
Output:
Wrap with abbreviations:
Here are the steps to follow:
Step 1: You will need to place your cursor on the tag you want to wrap
Step 2: Open your command palette with ‘CTRL + SHIFT +P’
Step 3: Find ‘Emmet: Wrap with Abbreviation’
Step 4: Type in your Emmet abbreviation that will wrap your current tag
Common Emmet Abbreviations in CSS:
Height & Width:
We can easily define height and width in a CSS document by typing the first letter of height and width. Also, we can give numbers to specify the width and height of any unit.
body{
w
}
Output:
body{
w100
}
Output:
body{
w100%
}
Output:
body{
w100vw
}
Output:
body{
w:a
}
Output:
Margin & Padding:
If we need margin and padding of 30px on all sides, then we should type
*{
p30-30-30-30
}
body{
m30-30-30-30
}
Output:
Text:
There are some easy to use text property symbols and will be generated with a default value. ‘ta’ will generate ‘text-align’ with ‘left’ value.
body{
ta
}
Output:
But if we want to overwrite the default value, then we should type
body{
ta:r
}
Output:
‘td’ will generate ‘text-decoration’ with the default ‘none’ value.
body{
td
}
Output:
But if we want to overwrite the default value to change the ‘text-decoration’, then we should type
body{
td:o
}
Output:
‘tt’ will become ‘text-transform’ with ‘uppercase’ default value.
body{
tt
}
Output:
But if we want to overwrite the default value to change the ‘text-transform’, then we should type
body{
tt:l
}
Output:
Font Face:
We can type ‘@f’ for a basic ‘@font-face’ property.
body{
@f
}
Output:
For a complete list of ‘@font-face’ property then type:
@ff
Output:
Background:
We can type ' bg' to add a simple ‘background’.
body {
bg
}
Output:
To add ‘background-image’, we can type ‘bgi’.
body {
bgi
}
Output:
To give ‘background-color’, we can type ‘bgc’.
body {
bgc
}
Output:
To add ‘no-repeat’ in the background, we can use ‘bgr’.
body {
bgr
}
Output:
Challenges for You🤓
- Create this HTML code using emmets —
The answer is —
div>h1^div>(table>(tr>td*2)*4)+ul>li*3
- Create the following HTML code using emmets —
The emmet abbreviation for the above code will be —
div.a>div#b>(table>(tr>(td>a)*4)*2)^div.c+div.d+img[src=image.png alt=image]+lorem
References:
Thank you for reading.
I hope you found this “How to Boost Your Coding Life By Powerful VS Code Emmets” article interesting. Please clap, share and follow if you like and please leave any comment to let me know your thoughts.