DOM元素尺寸全解析:盒模型与宽高属性的指南

两种盒模型

CSS 的 box-sizing 属性控制盒模型类型:

  • 标准盒模型(content-boxwidth/height 仅表示内容区域的尺寸
  • IE 盒模型(border-boxwidth/height 表示内容 + padding + border的总和

假设设置以下样式:

css 复制代码
.box {
  width: 200px;
  height: 100px;
  padding: 20px;
  border: 10px solid;
  margin: 30px;
}

1. 标准盒模型

lua 复制代码
+----------------------------------------+
| Margin (30px)                          |
|  +----------------------------------+  |
|  | Border (10px)                    |  |
|  |  +----------------------------+  |  |
|  |  | Padding (20px)             |  |  |
|  |  |  +----------------------+  |  |  |
|  |  |  | Content (200px)      |  |  |  |
|  |  |  +----------------------+  |  |  |
|  |  |                            |  |  |
|  |  +----------------------------+  |  |
|  |                                  |  |
|  +----------------------------------+  |
+----------------------------------------+
  • 总宽度 = 200 + 20 * 2 + 10 * 2 + 30 * 2 = 320px

2. IE盒模型

scss 复制代码
+----------------------------------------+
| Margin (30px)                          |
|  +----------------------------------+  |
|  | Border (10px) + Padding (20px)   |  |
|  |  +----------------------------+  |  |
|  |  | Content (实际: 140px)       |  |  |
|  |  | (200 - 20*2 - 10*2 = 140)  |  |  |
|  |  +----------------------------+  |  |
|  |                                  |  |
|  +----------------------------------+  |
+----------------------------------------+
  • 总宽度 = 200 + 30 * 2 = 260px

宽高属性详解

1.offsetWidth / offsetHeight

  • 包含内容width/height + padding + border
  • 不包含margin

例子:

content-boxoffsetWidth = 内容 + padding + border

宽: 200 + 20 * 2 + 10 * 2 = 260px

border-boxoffsetWidth = 设置的 width

200px

2. clientWidth / clientHeight

  • 包含内容width/height + padding
  • 不包含bordermargin

content-boxclientWidth = 内容 + padding

200 + 20 * 2 = 240px

border-boxclientWidth = 设置的 width - border

200 - 10*2 = 180px

3. getBoundingClientRect

getBoundingClientRect.width = (content + padding + border) * transform缩放系数

宽高与盒模型无关

  • 作用 :返回元素相对于视口的精确几何信息
  • 返回值 :一个 DOMRect 对象,包含 8 个属性:
js 复制代码
{
  x:       // 元素左上角 X 坐标(等同 left)
  y:       // 元素左上角 Y 坐标(等同 top)
  left:    // 元素左侧到视口左侧的距离
  top:     // 元素顶部到视口顶部的距离
  right:   // 元素右侧到视口左侧的距离
  bottom:  // 元素底部到视口顶部的距离
  width:   // 元素的实际渲染宽度(含 transform)
  height:  // 元素的实际渲染高度(含 transform)
}

特性

特性 说明
浮点精度 返回精确到亚像素级别的数值(如 100.25px)
包含变换 计算 CSS transform(缩放/旋转/位移)后的实际渲染尺寸
视口相对 坐标基于当前可视区域(viewport),随滚动位置变化
实时计算 每次调用都会重新计算,可能触发重排(reflow)

1.坐标系示意图

css 复制代码
视口左上角 (0,0)
  │
  ├──────────→ X轴
  │
  ▼ Y轴
  ┌───────────────────────┐
  │ 元素左上角 (left, top) │
  │                       │
  │ width                 │
  │                       │
  └───────────────────────┘
          height
arduino 复制代码
const rect = element.getBoundingClientRect();

rect.right === rect.left + rect.width

rect.bottom === rect.top + rect.height

2. 性能注意事项

  • 避免频繁调用:会强制同步布局
  • 批量读取:连续读取多个属性时只会触发一次重排
arduino 复制代码
// (触发两次重排)
const width = el.getBoundingClientRect().width;
const height = el.getBoundingClientRect().height;

// (单次重排)
const rect = el.getBoundingClientRect();
const width = rect.width;
const height = rect.height;

4. scroll

1. scrollHeight & scrollWidth

scrollHeight = 内容实际高度 + padding

scrollWidth = 内容实际宽度 + padding

html 复制代码
<div id="container" style="height:200px; overflow-y:scroll">
  <div style="height:800px; padding:20px"></div>
</div>

<script>
  console.log(container.scrollHeight); // 800 + 20*2 = 840
  // 如果设置overflow-y:hidden,container.scrollHeight = 800 + 20 = 820
</script>

2. scrollTop & scrollLeft

图例

css 复制代码
 Content Area
  ↑
  │ scrollTop
  │
┌─┼──────────────────┐
│ │                  │ ← 可视化区域
│ │                  │
└─┴──────────────────┘

是否在底部

复制代码
element.scrollTop >= scrollHeight - clientHeight

到达最右侧也是同理

复制代码
element.scrollLeft >= scrollWidth - clientWidth

3. scrollTo方法

平滑滚动

less 复制代码
// 原生平滑滚动
element.scrollTo({
  top: 500,
  behavior: 'smooth'
});

4. scrollTopMax / scrollLeftMax

目前只有火狐支持!

属性 描述 典型应用场景
scrollTopMax 最大可滚动距离 (scrollHeight - clientHeight) 检测是否滚动到底部
scrollLeftMax 最大水平滚动距离 (scrollWidth - clientWidth) 横向滚动限制检测

兼容性对照表

属性/方法 Chrome Firefox Safari Edge IE
scrollTopMax ✅ 44+
相关推荐
layman05283 小时前
ES6/ES11知识点 续五
前端·ecmascript·es6
Jiaberrr5 小时前
uniapp app 端获取陀螺仪数据的实现攻略
前端·javascript·vue.js·uni-app·陀螺仪
MINO吖5 小时前
项目改 pnpm 并使用 Monorepo 发布至 npm 上
前端·npm·node.js
筱歌儿8 小时前
小程序问题(记录版)
前端·小程序
Jinuss8 小时前
源码分析之Leaflet中的LayerGroup
前端·leaflet
赶飞机偏偏下雨8 小时前
【前端笔记】CSS 选择器的常见用法
前端·css·笔记
LuckyLay9 小时前
AI教你学VUE——Deepseek版
前端·javascript·vue.js
我是哈哈hh9 小时前
【Vue】全局事件总线 & TodoList 事件总线
前端·javascript·vue.js·vue3·vue2
liuyang___10 小时前
vue3+ts的watch全解!
前端·javascript·vue.js
我是哈哈hh10 小时前
【Vue】组件自定义事件 & TodoList 自定义事件数据传输
前端·javascript·vue.js·vue3·vue2