简体   繁体   English

Visual Studio 代码 CSS 缩进和格式化

[英]Visual studio code CSS indentation and formatting

I'd like to know if there is any way to activate auto indent a CSS file in visual studio code with the shortcut ALT + SHIFT + F ?我想知道是否有任何方法可以使用快捷键ALT + SHIFT + F在 Visual Studio 代码中激活 CSS 文件的自动缩进?

It's working fine with JavaScript but strangely not with CSS.它适用于 JavaScript,但奇怪的是不适用于 CSS。

Yes, try installing vscode-css-formatter extension .是的,尝试安装vscode-css-formatter 扩展
It just adds the functionality to format .css files and the shortcut stays the same Alt + Shift + F .它只是添加了格式化.css文件的功能,并且快捷方式保持不变Alt + Shift + F

Beautify css/sass/scss/less 美化css/sass/scss/less

to run this运行这个

enter alt+shift+f输入alt+shift+f

or或者

press F1 or ctrl+shift+p and then enter beautify ..F1ctrl+shift+p然后输入 beautify ..

在此处输入图像描述


an another one - JS-CSS-HTML Formatter另一个 - JS-CSS-HTML 格式化程序

i think both this extension uses js-beautify internally我认为这个扩展在内部都使用了js-beautify

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 CSS和SASS/SCSS/LESS都是相关的
  • HTML, Javascript, Typescript, JSON - VS code is already formatting HTML、Javascript、Typescript、JSON - VS 代码已经在格式化
  • CSS and related - VS code is not formatting as of today CSS 和相关 - VS 代码从今天开始没有格式化

Options:选项:

  • To format css/sass/scss/less:要格式化 css/sass/scss/less:
    • Prettier 更漂亮
      • All css related supported, and not others, I choose this, it works great.支持所有与 css 相关的,而不是其他的,我选择这个,它很好用。
  • To format JavaScript/TypeScript/CSS:要格式化 JavaScript/TypeScript/CSS:
  • To format JS, CSS, HTML, JSON file (wraps js-beautify)格式化 JS、CSS、HTML、JSON 文件(包装 js-beautify)
  • To format CSS格式化 CSS
    • CSS Formatter CSS 格式化程序
      • but, only CSS supported, not all the related - not maintained 6+ months但是,仅支持 CSS,并非所有相关的 - 未维护 6 个月以上

To format:格式化:

Press Alt + Shift + F in VS Code, after installing Prettier.安装 Prettier 后,在 VS Code 中按Alt + Shift + F。

I recommend using Prettier as it's very extensible but still works perfectly out of the box:我建议使用Prettier ,因为它非常可扩展,但开箱即用仍然可以完美运行:

1. CMD + Shift + P -> Format Document

or或者

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

EDIT: Prettier has become vastly more popular and standardized since I first posted this answer.编辑:自从我第一次发布这个答案以来,Prettier 变得更加流行和标准化。 It has gone so far as to even be used directly in the build flows of most modern frontend projects .它甚至可以直接用于大多数现代前端项目的构建流程中。 I strongly encourage users looking to format their code use the Prettier VSCode extension, which tries to use the same settings configured by said build flows .我强烈鼓励希望格式化其代码的用户使用 Prettier VSCode 扩展,它尝试使用由上述构建流程配置的相同设置

After opening local bootstrap.min.css in visual studio code, it looked unindented.在 Visual Studio 代码中打开本地bootstrap.min.css后,它看起来没有缩进。 Tried the commad ALT+Shift+F but in vain.尝试了逗号ALT+Shift+F但徒劳无功。

Then installed然后安装

CSS Formatter extension. CSS 格式化程序扩展。

Reloaded it and ALT+Shift+F indented my CSS file with charm.重新加载它并ALT+Shift+F以魅力缩进我的 CSS 文件。

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 .画廊中有几个可供选择,但我正在使用的一个是Michele Melluso 的 Beautify ,它提供了相当程度的可配置性,但对其余设置仍然不显眼。 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.它适用于 CSS 和 SCSS,让您缩进 3 个空格,将其余代码保持在 2 个空格,这很好。

You can snatch it from GitHub and adapt it yourself, should you feel like it too.如果你也喜欢的话,你可以从GitHub 上抓取它并自己调整它。

转到文件菜单->首选项->扩展然后键入CSS Formatter等待它加载并单击安装

Install HookyQR.beautify extension.安装 HookyQR.beautify 扩展。 It will beautify your javascript, JSON, CSS, Sass, and HTML in Visual Studio Code.它将美化您在 Visual Studio Code 中的 javascript、JSON、CSS、Sass 和 HTML。 It is the most use extensions for this purpose这是为此目的最常用的扩展

Beautify ( Github ) & Prettier ( Github ) 是 Visual Studio Code 中最好的 Web 开发插件。

Maybe a little bit late to the party but this might help users using prettier .聚会可能有点晚了,但这可能会帮助用户使用prettier Just add this line to the setting.json file.只需将此行添加到setting.json文件中。

"[css]": {
  "editor.defaultFormatter": "esbenp.prettier-vscode"
}

Save and all should be good now保存,现在一切都应该很好

To format the code in Visual Studio when you want, press: (Ctrl + K) & (Ctrl + F)要在需要时在 Visual Studio 中格式化代码,请按:(Ctrl + K) & (Ctrl + F)

The auto formatting rules can be found and changed in: Tools/Options --> (Left sidebar): Text Editor / CSS (or whatever other language you want to change)可以在以下位置找到并更改自动格式化规则:工具/选项 -->(左侧边栏):文本编辑器/CSS(或您想要更改的任何其他语言)

For the CSS language the options are unfortunately very limited.不幸的是,对于 CSS 语言,选项非常有限。 You can also make some changes in: .../ Text Editor / All Languages您还可以在以下位置进行一些更改:.../文本编辑器/所有语言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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