简体   繁体   中英

How to enable VS Code HTML error checking / validation?

I have just switched to Visual Studio Code for web development from Netbeans and am finding my way around. In Netbeans, if I forget the closing bracket on a tagname it will indicate my error with a red squiggly underline, and the alert in the left margin.

I would have thought error checking to be a fundamental function in a web dev editor like vs code. Maybe I am just not finding the right option or extension.

How can I achieve this same html, css error checking behaviour in VS Code?

VSCode doesn't have HTML validation by default. But it allows you to add extensions and enable these features.

To add HTML validation (linting), Open VSCode, then press Ctrl + P and then paste ext install HTMLHint in it, and press enter. It will install a HTML validator. You may need to reload VSCode to load the extension.

Now if you open the same html document you had the syntax error, you should see there's an issue shown at the status bar at the bottom :) and it will also show you the errors in those lines.

VS Code default support code formatting and it track the syntactical error. If you create a new file and directly try to write the code than VS Code could not able to understand which language or type of syntax user want to format/correcting. So, first need to save the new file with the proper extension than visual studio properly identify the syntax.

The code formatting is available in VS Code through the following shortcuts:

  • On Windows Shift + Alt + F
  • On Mac Shift + Option + F
  • On Ubuntu Ctrl + Shift + I

You can add Auto Close Tag from VS Code marketplace.

Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.

  1. Automatically add HTML/XML close tag, same as Visual Studio IDE or Sublime Text

     ext install auto-close-tag 
  2. VS Code integration for HTMLHint - A Static Code Analysis Tool for HTML

     ext install HTMLHint 
  3. Provides CSS class name completion for the HTML class attribute based on the CSS files in your workspace. Also supports React's className attribute.

     ext install html-css-class-completion 

The VS Code supports this by default. It also has Emmet support enabled by default which means you don't need to worry about close HTML tags. It automatically adds closing tag.

This is not a builtin functionality of VsCode...However, it has a lot of plugins available. I would recommend you the HTMLHint plugin. That's what I have been using.

You can install it using: ext install HTMLHint command

There is an extension for VS Code which is HTMLHint.
You can install that by following the below
1. To open the extensions:marketplace press Ctrl + Shift + X
2. Type HTMLHint in the search extension box
3. Click on the install button in the search result showing HTMLHint developed by "Mike Kaufman"

I found that this extension (while not letting you extend the built in rules) works better than HTMLHint for checking HTML validity.

Name: W3C Validation

Id: umoxfo.vscode-w3cvalidation

Description: Adds W3C validation support to Visual Studio Code.

Version: 2.3.0

Publisher: Umoxfo VS

Marketplace Link: https://marketplace.visualstudio.com/items?itemName=umoxfo.vscode-w3cvalidation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM