Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio code CSS indentation and formatting

People also ask

How do you indent CSS codes in Visual Studio?

css in visual studio code, it looked unindented. Tried the commad ALT+Shift+F but in vain. CSS Formatter extension. Reloaded it and ALT+Shift+F indented my CSS file with charm.

How do I automatically fix indentation in Visual Studio Code?

Press Ctrl+Shift+P to open Command Palette. Type in settings and select Open User Settings. In Search settings box, input indent to search for settings related to indentation. Select full in Editor: Auto Indent section.

How do I indent selected code in Visual Studio Code?

I want to indent a specific section of code in Visual Studio Code: Select the lines you want to indent, and. use Ctrl + ] to indent them.

How do I use code formatting in Visual Studio Code?

VS Code has great support for source code formatting. The editor has two explicit format actions: Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.


Yes, try installing vscode-css-formatter extension.
It just adds the functionality to format .css files and the shortcut stays the same Alt+Shift+F.


Beautify css/sass/scss/less

to run this

enter alt+shift+f

or

press F1 or ctrl+shift+p and then enter beautify ..

enter image description here


an another one - JS-CSS-HTML Formatter

i think both this extension uses js-beautify internally


Wasted an hour finding the best option.

Just putting it together, for easy reading and choosing one them.

Notes:

  • CSS and SASS/SCSS/LESS are all related
  • HTML, Javascript, Typescript, JSON - VS code is already formatting
  • CSS and related - VS code is not formatting as of today

Options:

  • To format css/sass/scss/less:
    • Prettier
      • All css related supported, and not others, I choose this, it works great.
  • To format JavaScript/TypeScript/CSS:
    • Beautify css/sass/scss/less
      • but, already JS, TS are supported by VS code
  • To format JS, CSS, HTML, JSON file (wraps js-beautify)
    • JS-CSS-HTML Formatter
      • but, already JS, HTML, JSON are supported by VS code
  • To format CSS
    • CSS Formatter
      • but, only CSS supported, not all the related - not maintained 6+ months

To format:

Press Alt + Shift + F in VS Code, after installing Prettier.


I recommend using Prettier as it's very extensible but still works perfectly out of the box:

1. CMD + Shift + P -> Format Document

or

1. Select the text you want to Prettify
2. CMD + Shift + P -> Format Selection

After opening local bootstrap.min.css in visual studio code, it looked unindented. Tried the commad ALT+Shift+F but in vain.

Then installed

CSS Formatter extension.

Reloaded it and ALT+Shift+F indented my CSS file with charm.

Bingo !!!


There are several to pick from in the gallery but the one I'm using, which offers considerable level of configurability still remaining unobtrusive to the rest of the settings is Beautify by Michele Melluso. It works on both CSS and SCSS and lets you indent 3 spaces keeping the rest of the code at 2 spaces, which is nice.

You can snatch it from GitHub and adapt it yourself, should you feel like it too.


Go to Files menu -> Preference -> Extentions Then type CSS Formatter wait for it to load and click install