2026-01-16 19:10:21 +08:00
|
|
|
|
<!--
|
|
|
|
|
|
CssBoxModel.vue
|
|
|
|
|
|
盒模型速懂:三根滑杆 + 颜色区分,实时显示总宽高、渲染顺序提示和 CSS 片段。
|
|
|
|
|
|
-->
|
2026-01-15 20:10:19 +08:00
|
|
|
|
<template>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="box-demo">
|
2026-02-14 12:14:07 +08:00
|
|
|
|
<div class="demo-header">
|
|
|
|
|
|
<span class="icon">📦</span>
|
|
|
|
|
|
<span class="title">CSS 盒模型</span>
|
|
|
|
|
|
<span class="subtitle">理解元素尺寸的构成(通俗说:盒子的四层包装)</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="controls">
|
|
|
|
|
|
<div class="control-item">
|
|
|
|
|
|
<div class="control-header">
|
|
|
|
|
|
<label>Padding (内边距)</label>
|
|
|
|
|
|
<span class="val">{{ padding }}px</span>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<input type="range" min="0" max="50" v-model.number="padding" />
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="control-item">
|
|
|
|
|
|
<div class="control-header">
|
|
|
|
|
|
<label>Border (边框)</label>
|
|
|
|
|
|
<span class="val">{{ border }}px</span>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<input type="range" min="0" max="30" v-model.number="border" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="control-item">
|
|
|
|
|
|
<div class="control-header">
|
|
|
|
|
|
<label>Margin (外边距)</label>
|
|
|
|
|
|
<span class="val">{{ margin }}px</span>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<input type="range" min="0" max="50" v-model.number="margin" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="stage-container">
|
|
|
|
|
|
<div class="stage-scroll">
|
|
|
|
|
|
<div class="layer margin" :style="{ padding: margin + 'px' }">
|
|
|
|
|
|
<span class="layer-label" v-if="margin >= 15">Margin</span>
|
|
|
|
|
|
<div class="layer border" :style="{ borderWidth: border + 'px' }">
|
|
|
|
|
|
<span class="layer-label" v-if="border >= 10">Border</span>
|
|
|
|
|
|
<div class="layer padding" :style="{ padding: padding + 'px' }">
|
|
|
|
|
|
<span class="layer-label" v-if="padding >= 15">Padding</span>
|
|
|
|
|
|
<div class="content">
|
|
|
|
|
|
<div class="content-inner">
|
2026-01-18 12:21:49 +08:00
|
|
|
|
内容区<br />
|
2026-01-16 19:10:21 +08:00
|
|
|
|
{{ contentW }} × {{ contentH }}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
</div>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="meta">
|
|
|
|
|
|
<div class="meta-row main">
|
|
|
|
|
|
<span class="meta-label">总占用宽度:</span>
|
|
|
|
|
|
<span class="meta-value">{{ total }}px</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="meta-detail">
|
|
|
|
|
|
<div class="detail-item">
|
|
|
|
|
|
<span class="detail-label">渲染顺序</span>
|
|
|
|
|
|
<span class="detail-text">内容 → Padding → Border → Margin</span>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="detail-item">
|
|
|
|
|
|
<span class="detail-label">计算公式</span>
|
2026-01-18 12:21:49 +08:00
|
|
|
|
<span class="detail-text"
|
|
|
|
|
|
>Margin(×2) + Border(×2) + Padding(×2) + 内容宽</span
|
|
|
|
|
|
>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-14 12:14:07 +08:00
|
|
|
|
<div class="info-box">
|
|
|
|
|
|
<span class="icon">💡</span>
|
|
|
|
|
|
<strong>核心思想:</strong>每个元素都是一个"盒子",从内到外依次是:内容区 → 内边距 → 边框 → 外边距。
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="code-block">
|
|
|
|
|
|
<div class="code-title">CSS 代码片段</div>
|
|
|
|
|
|
<div class="code-content">
|
|
|
|
|
|
<div class="line">.box {</div>
|
2026-01-18 12:21:49 +08:00
|
|
|
|
<div class="line">width: {{ contentW }}px;</div>
|
|
|
|
|
|
<div class="line">height: {{ contentH }}px;</div>
|
|
|
|
|
|
<div class="line">padding: {{ padding }}px;</div>
|
|
|
|
|
|
<div class="line">border: {{ border }}px solid #0ea5e9;</div>
|
|
|
|
|
|
<div class="line">margin: {{ margin }}px;</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="line">}</div>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
|
import { computed, ref } from 'vue'
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
const contentW = 120
|
|
|
|
|
|
const contentH = 80
|
2026-01-15 20:10:19 +08:00
|
|
|
|
const padding = ref(20)
|
2026-01-16 19:10:21 +08:00
|
|
|
|
const border = ref(10)
|
2026-01-15 20:10:19 +08:00
|
|
|
|
const margin = ref(20)
|
|
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
const total = computed(
|
|
|
|
|
|
() => margin.value * 2 + border.value * 2 + padding.value * 2 + contentW
|
|
|
|
|
|
)
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.box-demo {
|
2026-01-15 20:10:19 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-14 12:14:07 +08:00
|
|
|
|
border-radius: 8px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-14 12:14:07 +08:00
|
|
|
|
padding: 1rem;
|
|
|
|
|
|
margin: 1rem 0;
|
|
|
|
|
|
max-height: 600px;
|
|
|
|
|
|
overflow-y: auto;
|
2026-01-16 19:10:21 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
gap: 1rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
|
margin-bottom: 0.75rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header .icon {
|
|
|
|
|
|
font-size: 1.25rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header .title {
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
font-size: 1rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header .subtitle {
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
|
margin-left: 0.5rem;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.controls {
|
|
|
|
|
|
display: grid;
|
|
|
|
|
|
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
|
|
|
|
|
gap: 16px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.control-item {
|
2026-01-15 20:10:19 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-16 19:10:21 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-01-15 20:10:19 +08:00
|
|
|
|
border-radius: 8px;
|
2026-01-16 19:10:21 +08:00
|
|
|
|
padding: 12px 16px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 8px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.control-header {
|
2026-01-15 20:10:19 +08:00
|
|
|
|
display: flex;
|
2026-01-16 19:10:21 +08:00
|
|
|
|
justify-content: space-between;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
align-items: center;
|
2026-01-16 19:10:21 +08:00
|
|
|
|
font-size: 13px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
label {
|
|
|
|
|
|
font-weight: 700;
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
.val {
|
|
|
|
|
|
font-family: var(--vp-font-family-mono);
|
|
|
|
|
|
color: var(--vp-c-brand);
|
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
|
}
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
input[type='range'] {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
accent-color: var(--vp-c-brand);
|
|
|
|
|
|
cursor: pointer;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.stage-container {
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
|
padding: 20px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
display: flex;
|
2026-01-16 19:10:21 +08:00
|
|
|
|
justify-content: center;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
overflow: hidden;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.stage-scroll {
|
|
|
|
|
|
overflow: auto;
|
|
|
|
|
|
max-width: 100%;
|
|
|
|
|
|
padding: 10px; /* space for scrollbar if needed */
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.layer {
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
transition: all 0.2s ease-out;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.layer-label {
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
top: 2px;
|
|
|
|
|
|
left: 4px;
|
|
|
|
|
|
font-size: 10px;
|
|
|
|
|
|
font-weight: 700;
|
|
|
|
|
|
text-transform: uppercase;
|
|
|
|
|
|
opacity: 0.7;
|
|
|
|
|
|
pointer-events: none;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
/* Margin Layer */
|
|
|
|
|
|
.margin {
|
|
|
|
|
|
background-color: #f9fafb; /* gray-50 */
|
|
|
|
|
|
border: 1px dashed #d1d5db; /* gray-300 */
|
|
|
|
|
|
color: #6b7280;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.margin > .layer-label {
|
|
|
|
|
|
color: #6b7280;
|
|
|
|
|
|
}
|
2026-01-16 19:10:21 +08:00
|
|
|
|
|
|
|
|
|
|
/* Border Layer */
|
|
|
|
|
|
.border {
|
|
|
|
|
|
background-color: #e0f2fe; /* sky-100 */
|
|
|
|
|
|
border-style: solid;
|
|
|
|
|
|
border-color: #7dd3fc; /* sky-300 */
|
|
|
|
|
|
color: #0284c7;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.border > .layer-label {
|
|
|
|
|
|
color: #0284c7;
|
|
|
|
|
|
}
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
/* Padding Layer */
|
|
|
|
|
|
.padding {
|
|
|
|
|
|
background-color: #dbeafe; /* blue-100 */
|
|
|
|
|
|
border: 1px dashed #93c5fd; /* blue-300 */
|
|
|
|
|
|
color: #2563eb;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.padding > .layer-label {
|
|
|
|
|
|
color: #2563eb;
|
|
|
|
|
|
}
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
/* Content Box */
|
|
|
|
|
|
.content {
|
|
|
|
|
|
background: var(--vp-c-brand);
|
|
|
|
|
|
color: #fff;
|
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
font-weight: 700;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
|
|
|
|
|
width: 120px;
|
|
|
|
|
|
height: 80px;
|
|
|
|
|
|
flex-shrink: 0;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.content-inner {
|
|
|
|
|
|
text-align: center;
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
line-height: 1.4;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.meta {
|
2026-01-15 20:10:19 +08:00
|
|
|
|
display: flex;
|
2026-01-16 19:10:21 +08:00
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 12px;
|
|
|
|
|
|
padding: 16px;
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.meta-row.main {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 8px;
|
|
|
|
|
|
font-size: 16px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
font-weight: 600;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.meta-value {
|
|
|
|
|
|
color: var(--vp-c-brand);
|
|
|
|
|
|
}
|
2026-01-16 19:10:21 +08:00
|
|
|
|
|
|
|
|
|
|
.meta-detail {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 6px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.detail-item {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
gap: 12px;
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
align-items: flex-start;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.detail-label {
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
min-width: 60px;
|
|
|
|
|
|
flex-shrink: 0;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.detail-text {
|
2026-01-15 20:10:19 +08:00
|
|
|
|
color: var(--vp-c-text-1);
|
2026-01-16 19:10:21 +08:00
|
|
|
|
font-family: var(--vp-font-family-mono);
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.code-block {
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
|
padding: 16px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.code-title {
|
|
|
|
|
|
font-weight: 700;
|
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.code-content {
|
|
|
|
|
|
background: #0b1221;
|
|
|
|
|
|
color: #e5e7eb;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
border-radius: 8px;
|
2026-01-16 19:10:21 +08:00
|
|
|
|
padding: 16px;
|
|
|
|
|
|
font-family: var(--vp-font-family-mono);
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
overflow-x: auto;
|
|
|
|
|
|
line-height: 1.6;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
.line {
|
|
|
|
|
|
white-space: pre;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
2026-02-14 12:14:07 +08:00
|
|
|
|
|
|
|
|
|
|
.info-box {
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
padding: 0.75rem;
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
gap: 0.25rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.info-box .icon {
|
|
|
|
|
|
flex-shrink: 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.info-box strong {
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
}
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</style>
|