Files

374 lines
10 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<link rel="icon" href="../../favicon.svg">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS 压缩/格式化工具</title>
<link rel="canonical" href="https://www.htmls.dev/tools/css-minifier/app.html">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #30cfd0 0%, #330867 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
color: white;
margin-bottom: 30px;
}
.header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}
.card {
background: rgba(255, 255, 255, 0.95);
border-radius: 20px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.action-bar {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
flex-wrap: wrap;
gap: 15px;
}
.btn-group {
display: flex;
gap: 10px;
}
.btn {
padding: 12px 24px;
border: none;
border-radius: 10px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
background: linear-gradient(135deg, #30cfd0 0%, #330867 100%);
color: white;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 25px rgba(48, 207, 208, 0.4);
}
.btn-secondary {
background: #6c757d;
}
.editor-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.editor-panel {
background: #f8f9fa;
border-radius: 12px;
padding: 15px;
display: flex;
flex-direction: column;
}
.editor-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.editor-title {
font-weight: 600;
color: #333;
font-size: 1.1rem;
}
.editor-stats {
font-size: 13px;
color: #666;
}
.editor-textarea {
width: 100%;
flex: 1;
min-height: 450px;
padding: 15px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-family: 'Courier New', monospace;
font-size: 14px;
resize: none;
transition: border-color 0.3s;
}
.editor-textarea:focus {
outline: none;
border-color: #30cfd0;
}
.options {
display: flex;
gap: 20px;
margin-bottom: 15px;
}
.option-item {
display: flex;
align-items: center;
gap: 8px;
}
.option-item input[type="checkbox"] {
width: 18px;
height: 18px;
cursor: pointer;
}
.option-item label {
cursor: pointer;
color: #555;
}
@media (max-width: 900px) {
.editor-container {
grid-template-columns: 1fr;
}
.editor-textarea {
min-height: 300px;
}
}
.tool-doc-link,
.tool-home-link {
position: fixed;
top: 16px;
padding: 6px 12px;
border-radius: 999px;
font-size: 12px;
font-weight: 600;
color: #0f172a;
background: rgba(255, 255, 255, 0.9);
border: 1px solid rgba(15, 23, 42, 0.12);
text-decoration: none;
letter-spacing: 0.02em;
z-index: 9999;
box-shadow: 0 6px 16px rgba(15, 23, 42, 0.08);
transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s ease;
}
.tool-doc-link {
right: 16px;
}
.tool-home-link {
right: 74px;
}
.tool-doc-link:hover,
.tool-home-link:hover {
background: rgba(255, 255, 255, 1);
border-color: rgba(15, 23, 42, 0.2);
transform: translateY(-1px);
}
</style>
</head>
<body>
<a class="tool-home-link" href="../../index.html">首页</a>
<a class="tool-doc-link" href="index.html">说明</a>
<div class="container">
<div class="header">
<h1>CSS 压缩/格式化工具</h1>
<p>压缩和美化 CSS 代码</p>
</div>
<div class="card">
<div class="action-bar">
<div class="btn-group">
<button class="btn" onclick="minifyCSS()">压缩</button>
<button class="btn" onclick="beautifyCSS()">格式化</button>
<button class="btn btn-secondary" onclick="loadSample()">示例</button>
<button class="btn btn-secondary" onclick="clearAll()">清空</button>
</div>
</div>
<div class="editor-container">
<div class="editor-panel">
<div class="editor-header">
<span class="editor-title">输入 CSS</span>
<span class="editor-stats" id="input-stats">0 字符</span>
</div>
<textarea class="editor-textarea" id="input" placeholder="在此输入 CSS 代码..."></textarea>
</div>
<div class="editor-panel">
<div class="editor-header">
<span class="editor-title">输出结果</span>
<span class="editor-stats" id="output-stats">0 字符 (减少 0%)</span>
</div>
<textarea class="editor-textarea" id="output" placeholder="处理后的 CSS 将显示在这里..." readonly></textarea>
</div>
</div>
</div>
</div>
<script>
function minifyCSS() {
const input = document.getElementById('input').value;
if (!input.trim()) {
document.getElementById('output').value = '';
updateStats('', 0);
return;
}
let css = input;
// Remove comments
css = css.replace(/\/\*[\s\S]*?\*\//g, '');
// Remove extra whitespace
css = css.replace(/\s+/g, ' ');
// Remove spaces around special characters
css = css.replace(/\s*([{}:;,>~+])\s*/g, '$1');
// Remove last semicolon
css = css.replace(/;}/g, '}');
// Remove leading/trailing whitespace
css = css.trim();
document.getElementById('output').value = css;
const saved = input.length - css.length;
const percent = ((saved / input.length) * 100).toFixed(1);
updateStats(css, saved, percent);
}
function beautifyCSS() {
const input = document.getElementById('input').value;
if (!input.trim()) {
document.getElementById('output').value = '';
updateStats('', 0);
return;
}
let css = input;
// Add newline after {
css = css.replace(/\{/g, '{\n');
// Add newline after }
css = css.replace(/\}/g, '\n}\n');
// Add newline after ;
css = css.replace(/;/g, ';\n');
// Add space after :
css = css.replace(/:/g, ': ');
// Remove extra newlines
css = css.replace(/\n\s*\n/g, '\n');
// Add proper indentation
const lines = css.split('\n');
let indented = '';
let indent = 0;
lines.forEach(line => {
line = line.trim();
if (!line) return;
if (line.includes('}')) {
indent = Math.max(0, indent - 1);
}
indented += ' '.repeat(indent) + line + '\n';
if (line.includes('{')) {
indent++;
}
});
document.getElementById('output').value = indented.trim();
updateStats(indented.trim(), 0);
}
function updateStats(output, saved, percent = '0.0') {
const outputLength = output ? output.length : 0;
document.getElementById('output-stats').textContent =
`${outputLength.toLocaleString()} 字符${saved > 0 ? ` (减少 ${percent}%, 节省 ${saved.toLocaleString()} 字符)` : ''}`;
const inputLength = document.getElementById('input').value.length;
document.getElementById('input-stats').textContent = `${inputLength.toLocaleString()} 字符`;
}
function clearAll() {
document.getElementById('input').value = '';
document.getElementById('output').value = '';
updateStats('', 0);
}
function loadSample() {
document.getElementById('input').value = `/* Header Styles */
.header {
background-color: #333;
color: white;
padding: 20px;
text-align: center;
}
.header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}
/* Button Styles */
.button {
display: inline-block;
padding: 12px 24px;
background: #667eea;
color: white;
text-decoration: none;
border-radius: 8px;
transition: all 0.3s;
}
.button:hover {
transform: translateY(-2px);
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
}`;
updateStats(document.getElementById('input').value, 0);
}
// Auto update stats on input
document.getElementById('input').addEventListener('input', function() {
updateStats('', 0);
});
</script>
<script src="../../assets/clicks.js" defer></script>
</body>
</html>