Skip to content Skip to sidebar Skip to footer

Format Html Code In Visual Studio Code Such That Attributes Are On Separate Lines?

There seems to be a lack of formatting settings for vscode. I want to be able to format html such that my html shows up as:

Solution 1:

VSCode added a way to do this now. You can edit your settings.json (ctrl+shift+p) and then add the following for the desired effect:

"html.format.wrapAttributes":"force-aligned"

--or--

"html.format.wrapAttributes":"force"

force-aligned will also add indents to align it with the attribute on the line where tag was opened.

Visit this link for more details or updates.

Solution 2:

My extended html was being limited by the "Prettier: Print width" setting, I thought I'd put a random value as 0, but then all the tags start breaking the attributes. So he put 10,000. This solved my problem.

Solution 3:

There are differnt "formatter" extensions, but one I found does a decent job of this exact formatting that you are looking for. It's called "vscode-tidyhtml".

https://marketplace.visualstudio.com/items?itemName=anweber.vscode-tidyhtml

  1. Click on the Extensions icon on the left hand side
  2. Search for and install "vscode-tidyhtml", reload Visual Studio Code
  3. Hit the "F1" key, and then type "TidyHtml", hit enter

It should format HTML so that attributes are on different lines. I am not sure if it works well for other file types.

Solution 4:

I know this is late, but I came here searching for an answer too. As @adi518 mentioned, the vscode automatic formatting for this might not be a great idea.

So, to do it where you'd like it, this extension might help: https://marketplace.visualstudio.com/items?itemName=dannyconnell.split-html-attributes

Solution 5:

You should open the settings.json file (shift+cmd+p for mac) and click on Open Preference: Open Settings (JSON)

add the following line at the beginning of the JSON file:

"html.format.wrapAttributes":"force-aligned",

In case formatting still does not work (this is a different issue), search for HTML and add the following lines

"[html]":{"editor.formatOnSave":true,"editor.formatOnPaste":true,"editor.formatOnType":true,"editor.defaultFormatter":"esbenp.prettier-vscode"},

Close and Open vscode then format by using (option+Shift+F)

Post a Comment for "Format Html Code In Visual Studio Code Such That Attributes Are On Separate Lines?"