How to Format Your Code

One thing you need to know about browsers is that they ignore white spaces in the HTML and CSS code.

For example, I can put all these elements on a single line, and everything still works.

Let me show you.

<!DOCTYPE html><html><head…


This content originally appeared on DEV Community and was authored by Karl Esi

One thing you need to know about browsers is that they ignore white spaces in the HTML and CSS code.

For example, I can put all these elements on a single line, and everything still works.

Let me show you.

<!DOCTYPE html><html><head><title>My First Web Page</title><style>img{width: 100px;border-radius: 50px;float: left;margin-right: 10px;}.username{font-weight: bold;}</style></head><body><img src="Images/altumcode.jpg"><p class="username">@KarlgustaAnnoh</p><p>I love to teach you HTML!</p></body></html>

All the elements in one line in VS Code

Now, save the changes.

Back in the browser, we have the same results as before:

The browser showing the same results

Because browsers don't care how we format our code. But, formatting is very important when it comes to reading and maintaining code. We want our code to be beautiful and perfectly formatted just like an article.

This is where we can use the Prettier plugin to help us out.

With Prettier, we can format our code in a consistent fashion. This is super important when you are part of a team, because different people have different ways of formatting their code. But if everyone on the team uses the Prettier plugin, the code will be consistently formatted.

So, let me show you how to use Prettier.

At the top, you go to the view menu, and click on the shortcut for the command palette. On a Mac, it is Shift + Command + P On Windows, it is Shift + Control + P.

Shift + Control + P to open the Command Palette on VS Code

Let's bring up the command palette. There we search for format document. Press ENTER.

On the command palette, we search for format document

OK. Now, Prettier has formatted my code, so none of those elements are on the same line.

<!DOCTYPE html>
<html>
    <head>
        <title>My First Web Page</title>
        <style>
            img {
                width: 100px;
                border-radius: 50px;
                float: left;
                margin-right: 10px;
            }
            .username {
                font-weight: bold;
            }
        </style>
    </head>
    <body>
        <img src="Images/altumcode.jpg"/>
        <p class="username">@KarlgustaAnnoh</p>
        <p>I love to teach you HTML!</p>
    </body>
</html>

Prettier has formatted the code on VS Code

This is a lot better. But, let me show you a trick!

We don't want to format our code every time we make a change. So, we can configure VS Code to automatically format the code the moment we save the changes.

So, at the top, let's go to the code menu, then Preferences, then Settings.

VS Code Settings

Here, search for format on save.

On the settings page, we search for format on save

Here is the setting:

The format on Save setting

Let's enable this setting.

Enabling the Format on Save setting

Now, every time we save the changes, VS Code automatically formats our code, using Prettier.

See you on the next one!

P.S. 🚀 Ready to kickstart your career in web development? Join our free starter course to master HTML, CSS, JavaScript, React, and more! Build real-world projects, learn from industry experts, and connect with a supportive community. Enroll now!


This content originally appeared on DEV Community and was authored by Karl Esi


Print Share Comment Cite Upload Translate Updates
APA

Karl Esi | Sciencx (2024-08-16T11:03:00+00:00) How to Format Your Code. Retrieved from https://www.scien.cx/2024/08/16/how-to-format-your-code/

MLA
" » How to Format Your Code." Karl Esi | Sciencx - Friday August 16, 2024, https://www.scien.cx/2024/08/16/how-to-format-your-code/
HARVARD
Karl Esi | Sciencx Friday August 16, 2024 » How to Format Your Code., viewed ,<https://www.scien.cx/2024/08/16/how-to-format-your-code/>
VANCOUVER
Karl Esi | Sciencx - » How to Format Your Code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/16/how-to-format-your-code/
CHICAGO
" » How to Format Your Code." Karl Esi | Sciencx - Accessed . https://www.scien.cx/2024/08/16/how-to-format-your-code/
IEEE
" » How to Format Your Code." Karl Esi | Sciencx [Online]. Available: https://www.scien.cx/2024/08/16/how-to-format-your-code/. [Accessed: ]
rf:citation
» How to Format Your Code | Karl Esi | Sciencx | https://www.scien.cx/2024/08/16/how-to-format-your-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.