How to Format Code Online: HTML, CSS, JavaScript, JSON, and SQL
Code formatting is one of those things developers either do habitually or never think about at all — until they encounter someone else's unformatted code and have to spend ten minutes figuring out where a function ends and the next one begins.
This guide covers what code formatting actually does, why it matters more than most developers think, how each major web language gets formatted, and how to do it privately in your browser without sending your source code to anyone's server.
What Is Code Formatting?
Code formatting — sometimes called code beautification — is the process of adjusting a piece of code's whitespace, indentation, and layout to make it consistently readable. A formatter does not change what your code does: it changes how it looks on the screen.
The distinction matters. A formatter will take this:
function greet(name){if(!name){return 'Hello, stranger';}return 'Hello, '+name+'!';}
And produce this:
function greet(name) {
if (!name) {
return 'Hello, stranger';
}
return 'Hello, ' + name + '!';
}
Same logic, same output at runtime — completely different readability. The formatted version lets you see the structure at a glance: one function, one conditional, two return paths.
Why Code Formatting Matters
The case for consistent formatting is stronger than it looks at first. It touches debugging speed, code review quality, team coordination, and long-term maintenance cost.
It reduces debugging time. Unformatted code — especially minified code — hides structure. A missing closing brace in a block of compact, single-line code can take thirty minutes to spot. The same error in a properly indented file is visible in seconds because the indentation visually breaks at the wrong level.
It makes code reviews faster and more accurate. When a reviewer sees a diff full of whitespace changes alongside logic changes, they are forced to distinguish between the two manually. A consistently formatted codebase eliminates that noise — every line in a diff represents a real change.
It signals professionalism. Consistently formatted code is easier for new team members, future maintainers, and open-source contributors to approach. It communicates that the author thought about readability, not just functionality.
It is free insurance against certain bugs. Some formatting patterns actively prevent bugs. Well-indented conditional blocks make it obvious which branch of code executes under which condition. Clearly separated function bodies make it harder to accidentally mix concerns.
Formatting HTML
HTML formatting is about making the document's tree structure visible through indentation. A properly formatted HTML file shows at a glance which elements are nested inside which, where sections begin and end, and how the page is organised.
The core rules for HTML formatting are:
- Block-level elements (
<div>,<section>,<article>,<header>,<main>,<ul>,<li>,<p>) get their own indented block. Each child is indented one level deeper than its parent. - Inline elements (
<span>,<a>,<strong>,<em>) stay on the same line as their surrounding text content where possible. - Attributes are separated by a single space. Very long attribute lists can be broken across lines for readability, though formatters differ in their approach here.
- Self-closing elements (
<img>,<input>,<br>) are written as single lines. - Embedded
<style>and<script>blocks are formatted using CSS and JavaScript rules respectively.
HTML formatting is especially useful when working with template output — HTML generated by a server-side framework like Django, Rails, or Next.js is often dense and unindented. Formatting it makes the structure navigable.
Formatting CSS
CSS formatting ensures each property sits on its own line, selectors are clearly defined, and values are easy to scan and edit individually. The basic rules:
- Each declaration (
property: value;) gets its own line. - The opening brace stays on the same line as the selector.
- The closing brace gets its own line.
- A blank line separates rule blocks.
- Nested rules (in
@media,@keyframes, or with a CSS nesting syntax) are indented one level deeper.
The most common use case for an online CSS formatter is unminifying production stylesheets. CSS in production is often compressed to a single line to reduce file size. When you need to read or debug it, a formatter restores the full readable structure.
Formatting JavaScript
JavaScript formatting is the most complex of the web languages because the syntax allows so much variation. A JavaScript formatter needs to handle:
- Function declarations and function expressions
- Arrow functions (
const fn = () => {}) - Template literals (
`Hello, ${name}`) - Destructuring assignments (
const { a, b } = obj) - Spread and rest operators (
...args) - Async/await syntax
- Class declarations and class expressions
- Object and array literals with nested structures
The most valuable use of a JavaScript formatter is un-minifying production bundles. Modern JavaScript build tools (Webpack, Vite, Rollup) produce single-line bundles optimised for download speed. When a bug occurs in production, formatting the bundle is often the first debugging step — it exposes individual function names, variable assignments, and conditional branches that are invisible in the minified version.
getUserById to a or b3. A formatter cannot recover the original names — only a source map can do that.
Formatting JSON
JSON (JavaScript Object Notation) is a data format used everywhere: API responses, configuration files, package manifests, settings files. It has strict syntax rules — no trailing commas, no comments, quoted keys only — and the difference between valid and invalid JSON is often a single misplaced character.
A JSON formatter does two things simultaneously: it makes the JSON readable, and it validates the syntax. If the JSON is invalid, a good formatter tells you exactly what is wrong and where.
Common JSON syntax errors that a formatter catches:
- Trailing commas —
[1, 2, 3,]is invalid JSON (valid in JavaScript, but not JSON). - Single-quoted strings —
{'key': 'value'}is invalid JSON; keys and string values must use double quotes. - Comments — JSON does not support comments. Files with
// commentsor/* block comments */fail JSON parsing. - Undefined or unquoted values —
{"key": undefined}is invalid; usenullinstead. - Missing commas between properties —
{"a": 1 "b": 2}fails to parse.
These errors are extremely common when hand-editing JSON files or when copying JSON from a JavaScript source (where trailing commas and comments are fine) into a context that requires valid JSON.
Formatting SQL
SQL formatting is about making queries readable at a structural level. A well-formatted SQL query makes the intent of each clause immediately clear: what is being selected, from which tables, filtered by which conditions, grouped and sorted how.
The standard conventions for SQL formatting:
- Keywords in uppercase:
SELECT,FROM,WHERE,JOIN,GROUP BY,ORDER BY,HAVING,LIMIT. This visually separates SQL structure from table and column names. - Each major clause on its own line: The SELECT clause on one line, the FROM clause on the next, WHERE on the next, and so on. This makes it easy to understand the query's logic without reading it word by word.
- AND / OR conditions indented: Conditions within a WHERE clause are indented under the WHERE keyword, with AND and OR at the start of each new condition.
- Aliases consistently applied: Table aliases (
uforusers) are consistent throughout the query.
Formatted SQL is essential for documentation. A query written for execution in a database console is often a single compact line. A query intended for a wiki, a README, or a code review should always be formatted — it needs to be readable by someone who did not write it.
| Unformatted | Formatted |
|---|---|
select u.name, o.total from users u join orders o on u.id = o.user_id where o.total > 100 order by o.total desc limit 10 |
SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100 ORDER BY o.total DESC LIMIT 10 |
Formatting Tools: Online vs Local
There are two main categories of code formatting tools: online formatters that run in a browser, and locally installed tools that run in your development environment.
| Type | Examples | Best for | Privacy |
|---|---|---|---|
| Online formatter (server-side) | Beautifier.io, FreeFormatter.com | Quick one-off formatting | Your code is sent to a server |
| Online formatter (client-side) | CodeMint (privotools), Prettier Playground | Quick formatting with no upload risk | Your code never leaves your browser |
| CLI tool | Prettier, js-beautify CLI, sqlfluff | Automated formatting in CI/CD pipelines | Runs locally; no network required |
| Editor extension | Prettier for VS Code, ESLint, EditorConfig | Format-on-save across an entire project | Runs locally; no network required |
For casual or one-off formatting tasks — especially when you are working with code that contains sensitive information like API keys, database credentials, or proprietary logic — a client-side online formatter is the right balance of convenience and privacy. Your code is formatted in milliseconds without ever leaving your device.
For team-wide consistency across a codebase, a CLI tool or editor extension is the better long-term investment because it can be automated, version-controlled, and enforced consistently across every developer's environment.
The Privacy Risk Most Developers Ignore
Most popular online formatting tools process your code on their servers. When you paste a snippet into a server-side formatter, that code travels across the internet to a machine you have no visibility into. It may be logged. It may be retained. The service's privacy policy — if they have one — may permit them to use your submitted code for analytics, training, or other purposes.
For most public code, this is a tolerable risk. But developers routinely paste things into formatters that are far from public: config files with database URLs, JavaScript files with hardcoded API keys, SQL queries referencing proprietary schema names, environment files with SECRET_KEY values. These should not be sent to any external service.
Client-side formatters eliminate this risk entirely. Once the page has loaded, no network requests are made — your code is processed by your browser's JavaScript engine, the same engine running the tab you are already working in. There is no server to receive it.
🟢 Try CodeMint — privotools' free browser-based code formatter. Format HTML, CSS, JavaScript, JSON, and SQL entirely in your browser with 2- or 4-space indentation. Your source code never leaves your device. Open the tool, paste your code, click Format.
Open CodeMint →Quick Summary
- Code formatting changes how code looks, not what it does — it adjusts whitespace, indentation, and layout.
- Consistent formatting reduces debugging time, improves code reviews, and makes codebases easier to maintain.
- HTML formatting makes document nesting visible; CSS formatting puts each property on its own line; JavaScript formatting exposes block structure and function boundaries.
- JSON formatting is also validation — a good JSON formatter tells you exactly what is wrong with invalid syntax.
- SQL formatting puts each clause on its own line and uppercases keywords, making the query's logic immediately readable.
- Client-side online formatters process your code in your browser without transmitting it to any server — the right choice when working with code that contains credentials or proprietary logic.
Frequently Asked Questions
Is it safe to format code using an online tool?
It depends on the tool. Server-based formatters receive your code on a remote server, which may be a concern for proprietary or sensitive code. Browser-based formatters like CodeMint process your code entirely within your browser — no code is transmitted to any server.
What is the difference between code formatting and code linting?
Code formatting deals with the visual appearance of code — indentation, spacing, line breaks, and brace placement. Code linting analyses code for potential errors, bad practices, and style violations. Formatting is purely cosmetic; linting identifies functional issues.
Can I format minified code online?
Yes. Minified code — which has whitespace and line breaks removed to reduce file size — can be "beautified" back into readable, indented code. Online formatters handle this well. Paste the minified code and the formatter re-introduces appropriate whitespace and line breaks.
Which code formatter should I use for JavaScript?
For professional projects, Prettier is the industry standard — it integrates with most code editors and enforces consistent style across teams. For quick online formatting of snippets, browser-based tools like CodeMint provide instant formatting without setup or installation.
Does code formatting affect how code runs?
No. Code formatting only changes the visual presentation — whitespace, indentation, and line breaks. It does not alter the logic, syntax, or behaviour of the code. Minified and beautifully formatted versions of the same code produce identical output when executed.