WebTools

307 Useful Tools & Utilities to make life easier.

CSS Formatter

Format CSS code that is unformatted.

CSS Formatter Implementation Details

The CSS Formatter tool provides a direct interface for restructuring dense or minified CSS stylesheets into human-readable code. By utilizing an embedded Ace code editor and a dedicated formatting library, the tool transforms stylesheets by injecting standardized indentation, spacing, and line breaks.

The architecture relies on the window.bitflanFormattersComponent Alpine.js component to manage the user interface state. When the tool initializes, it sets up an instance of the Ace Editor inside an Alpine x-data scope. The editor is configured with the ace/theme/clouds visual theme and explicitly hides the print margin (showPrintMargin: false) to maximize horizontal viewing space. Interestingly, due to the shared underlying component design used across multiple formatters in the application, the editor engine is initially instantiated with JavaScript syntax highlighting rules (ace/mode/js). Despite this quirk in the setup configuration, the actual text manipulation strictly processes the code as CSS.

Execution Flow of the Formatting Engine

The core string manipulation is delegated to the css_beautify function, a widely used formatting engine typically packaged with js-beautify. The formatting workflow is triggered by an Alpine.js event listener attached to the primary submit button (x-on:click="formatCss()").

When the formatCss() method fires, it follows a precise execution path:

  • It accesses the current text content of the editor using the this.editor.getValue() method.
  • This raw string is passed as the sole argument to the global css_beautify() function. Because no additional configuration objects are passed into the function call, the beautifier falls back to its default formatting rules for brace placement, spacing, and indentation width.
  • The returned, fully formatted CSS string is then injected back into the editor instance using this.editor.setValue(), immediately updating the visual output for the user.

To extract the formatted code, the interface includes a Copy button bound to @click="window.writeClipboardText($event, editor.getValue())", which retrieves the exact editor contents and dispatches them to the system clipboard.

Step-by-Step Worked Example

To illustrate the exact behavior of the css_beautify engine, consider a scenario where a block of minified CSS needs to be expanded. The formatter processes selectors, properties, and nested at-rules.

Input:

.container{display:flex;justify-content:center;align-items:center;}@media(max-width:768px){.container{flex-direction:column;}}

Processing:

During execution, formatCss() extracts this string and feeds it to the beautifier. The engine recognizes the .container ruleset and the nested @media block. It expands the single-line definitions by inserting line breaks after the opening braces, applying nested indentation to the rules inside the media query, and adding single spaces after each property colon.

Expected Output:

.container {
    display: flex;
    justify-content: center;
    align-items: center;
}

@media(max-width:768px) {
    .container {
        flex-direction: column;
    }
}

Frequently Asked Questions (FAQs)

Does the tool validate or automatically fix incorrect CSS syntax?

No. The css_beautify algorithm operates strictly as a formatter, not a validator or linter. If the provided CSS contains syntax errors—such as missing closing brackets, invalid property names, or omitted semicolons—the tool will attempt to format the text based on the available structural characters. It does not generate error warnings or automatically repair broken CSS logic.

Can I pass custom parameters for indentation size or style?

In this specific implementation, the formatCss() method executes css_beautify(this.editor.getValue()) without any secondary configuration objects. Consequently, there are no user-facing options to customize formatting rules, such as specifying custom tab widths, choosing between spaces and tabs, or altering specific brace placement behaviors. It strictly applies the library's default styling rules.

How does the copy-to-clipboard functionality extract the code?

The copy feature bypasses standard HTML text selection, utilizing a custom application helper called window.writeClipboardText. This function receives the browser click event alongside the direct output of editor.getValue(). This guarantees that the exact whitespace characters, line breaks, and indentation levels maintained by the Ace Editor instance are faithfully preserved when the code is written to your operating system's clipboard.

What happens if I try to format JavaScript or HTML using this specific tool?

Because the formatCss() method exclusively invokes the css_beautify engine, passing HTML markup or JavaScript code into the editor will result in unpredictable structural changes. The parsing logic is specifically calibrated for CSS syntax rules (such as matching CSS selectors, property declarations, and value assignments). For other web languages, you must use their respective, dedicated formatting utilities.

Contact

Missing something?

Feel free to request missing tools or give some feedback using our contact form.

Contact Us