CSS

CSS Formatter & Minifier

Format or minify CSS code directly in the browser

CSS Formatter & Minifier — what it does

Paste a stylesheet and this tool either expands it into readable, consistently indented CSS, or strips it down to a minified single-line version. Both directions run on the text you paste — nothing is fetched, compiled or sent anywhere — so it works equally well on a snippet from a browser inspector and a full production file.

Worked example

A compressed stylesheet with a comment, a grouped selector and a media query, run through the Format button.

Input — compressed CSS

.card{margin:0 auto;padding:16px 24px;color:#333}
/* section headings */
.card h2,.card h3{font-family:Georgia,serif;font-size:1.25rem;margin-bottom:8px}
@media (max-width:600px){.card{padding:12px;box-shadow:0 1px 2px rgba(0,0,0,.2)}}

Output — formatted

.card {
  margin: 0 auto;
  padding: 16px 24px;
  color: #333;
}
/* section headings */
.card h2, .card h3 {
  font-family: Georgia, serif;
  font-size: 1.25rem;
  margin-bottom: 8px;
}
@media (max-width:600px) {
  .card {
    padding: 12px;
    box-shadow: 0 1px 2px rgba(0,0,0,.2);
  }
}

The comment stays on its own line, every property gets ": " and every declaration ends with ";", while the commas inside rgba(0,0,0,.2) are left alone. Minify on the same input drops the comment and returns a single line of 210 characters.

When this tool helps

  • Making a minified vendor stylesheet readable so you can find the rule that is overriding your own.
  • Tidying CSS that arrived from several contributors with different indentation and brace styles.
  • Producing a minified version for a page where you inline critical CSS in the document head.

How to use it

  1. Paste your CSS into the input box.
  2. Press format for readable output, or minify to compress it.
  3. Review the result in the output panel.
  4. Copy the formatted or minified CSS back into your project.

Accuracy, limits, and good practice

  • Minification here removes whitespace and comments; it does not rename selectors, merge duplicate rules or reorder declarations.
  • Formatting normalises layout but preserves order, which matters because CSS specificity and source order decide what actually wins.
  • Keep the readable version in source control and minify at build time, rather than committing compressed CSS you cannot review.

Frequently asked questions

Will formatting change how my page looks?

No. Only whitespace and comments change. Selectors, properties, values and their order are preserved exactly.

How much does minifying save?

Typically 15 to 30 percent before gzip on hand-written CSS, and considerably less once server compression is already applied.

Does it handle modern syntax?

Nesting, custom properties, media queries and layer blocks are treated as ordinary CSS text and pass through intact.

Are comments kept?

Format keeps each /* … */ comment and puts it on its own line at the current indentation, as with /* section headings */ above. Minify always removes comments; there is no option to keep them in the minified output.

Does formatting change text inside quotes, url() or functions?

No. Quoted strings and anything inside parentheses are copied as they are: content:"Total, incl. VAT", url(data:image/png;base64,iVBO;x) and repeat(3,1fr) come out unchanged from both Format and Minify, including the semicolon inside url(). Format only adds ", " after commas in selector lists and in top-level value lists such as font-family: Georgia, serif.

Related tools on this site

Everything above runs inside this page. CSS Formatter & Minifier needs no account, no upload, and no server round trip — close the tab and nothing is left behind.

CSS 格式化與壓縮能做什麼

貼上樣式表,工具可以把它展開成縮排一致、易讀的 CSS,也可以壓成單行的精簡版。兩個方向都只處理你貼上的文字,不會抓取、編譯或上傳任何內容,因此無論是從瀏覽器開發工具複製的片段,或整份正式檔案都適用。

實際範例

一段壓縮過、含註解、群組選擇器與 @media 區塊的樣式表,按下格式化。

原文:壓縮過的 CSS

.card{margin:0 auto;padding:16px 24px;color:#333}
/* section headings */
.card h2,.card h3{font-family:Georgia,serif;font-size:1.25rem;margin-bottom:8px}
@media (max-width:600px){.card{padding:12px;box-shadow:0 1px 2px rgba(0,0,0,.2)}}

格式化後

.card {
  margin: 0 auto;
  padding: 16px 24px;
  color: #333;
}
/* section headings */
.card h2, .card h3 {
  font-family: Georgia, serif;
  font-size: 1.25rem;
  margin-bottom: 8px;
}
@media (max-width:600px) {
  .card {
    padding: 12px;
    box-shadow: 0 1px 2px rgba(0,0,0,.2);
  }
}

註解獨立成一行保留下來,每個屬性名稱後面加上「: 」,每條宣告都以「;」結尾,而 rgba(0,0,0,.2) 括號裡的逗號維持原樣。同樣的原文按壓縮會拿掉註解,得到單獨一行(210 個字元)。

什麼時候用得上

  • 把壓縮過的第三方樣式表還原成可讀格式,找出到底是哪條規則蓋掉了你的樣式。
  • 整理由多人提供、縮排與大括號風格不一致的 CSS。
  • 為在 head 內嵌關鍵 CSS 的頁面產生壓縮版本。

使用步驟

  1. 把 CSS 貼進輸入框。
  2. 按「格式化」取得易讀版本,或按「壓縮」取得精簡版本。
  3. 在輸出區檢視結果。
  4. 把格式化或壓縮後的 CSS 複製回專案。

精確度、限制與實務建議

  • 此處的壓縮只移除空白與註解,不會重新命名選擇器、合併重複規則或調整宣告順序。
  • 格式化只統一排版、保留順序,這很重要,因為 CSS 由權重與原始順序決定最終生效的規則。
  • 建議在版本控制中保留可讀版本,於建置時再壓縮,而不是提交無法審閱的壓縮檔。

常見問題

格式化會改變頁面外觀嗎?

不會。只有空白與註解改變,選擇器、屬性、數值與順序都完整保留。

壓縮能省多少?

手寫 CSS 在 gzip 前通常可省 15% 至 30%,若伺服器已啟用壓縮,額外效益會小得多。

支援新語法嗎?

巢狀語法、自訂屬性、媒體查詢與 layer 區塊都會被當成一般 CSS 文字原樣保留。

註解會保留嗎?

格式化會保留每則 /* … */ 註解,並依目前的縮排讓它獨立成一行,如上例的 /* section headings */。壓縮則一律拿掉註解,沒有在壓縮結果中保留註解的選項。

格式化會改動引號、url() 或函式裡的文字嗎?

不會。引號內的字串與括號內的內容都原樣照抄:content:"Total, incl. VAT"、url(data:image/png;base64,iVBO;x) 與 repeat(3,1fr) 經過格式化或壓縮都不變,連 url() 裡的分號也不受影響。格式化只會在選擇器清單與最外層的值清單(例如 font-family: Georgia, serif)的逗號後加上「, 」。

本站相關工具

以上步驟全部在這個頁面內完成,CSS 格式化與壓縮 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。