CSS Beautifier Tool
/* Global Styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f9;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
width: 100%;
max-width: 800px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
header h1 {
text-align: center;
color: #333;
margin-bottom: 20px;
font-size: 2em;
}
.main-content {
display: grid;
grid-template-columns: 1fr;
gap: 20px;
}
.input-section, .output-section {
background-color: #f7f7f7;
padding: 15px;
border-radius: 8px;
}
textarea {
width: 100%;
height: 150px;
padding: 10px;
font-size: 1rem;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #fafafa;
color: #333;
resize: vertical;
}
textarea:focus {
outline: none;
border-color: #6c63ff;
box-shadow: 0 0 5px rgba(108, 99, 255, 0.5);
}
button.btn {
background-color: #6c63ff;
color: white;
font-size: 1rem;
border: none;
border-radius: 8px;
padding: 10px 15px;
cursor: pointer;
width: 100%;
margin-top: 10px;
}
button.btn:hover {
background-color: #5a52d0;
}
pre {
background-color: #282c34;
color: #f8f8f2;
padding: 15px;
border-radius: 8px;
font-size: 1rem;
white-space: pre-wrap;
word-wrap: break-word;
}
@media (min-width: 768px) {
.main-content {
grid-template-columns: 1fr 1fr;
}
}
document.getElementById("beautify-btn").addEventListener("click", function() {
const inputCSS = document.getElementById("input-css").value;
if (inputCSS.trim() === "") {
alert("Please enter some CSS code to beautify.");
return;
}
const beautifiedCSS = beautifyCSS(inputCSS);
const output = document.getElementById("output-css");
// Set beautified CSS to output
output.textContent = beautifiedCSS;
// Re-initialize Prism for syntax highlighting
Prism.highlightElement(output);
});
function beautifyCSS(css) {
// Basic CSS beautification (you can add more logic for advanced beautification)
const beautified = css
.replace(/;\s*/g, ';\n')
.replace(/{/g, '{\n')
.replace(/}/g, '\n}')
.replace(/\n\s*\n/g, '\n')
.replace(/([a-zA-Z0-9-]+)\s*:/g, '$1: ')
.replace(/\s*;\s*/g, '; ');
return beautified.trim();
}