video设width:100%仍溢出,因height:auto受固有宽高比影响且父容器未设明确高度;需确保父级有height约束或改用aspect-ratio+width:100%,并配合body{overflow-x:hidden}截断横向溢出。
因为 width: 100% 只控制宽度,不约束高度;而视频有固有宽高比,一旦父容器高度没被有效限制,height: auto 就会撑高整个容器,导致垂直滚动或内容被顶出视口。更隐蔽的是:如果父容器本身没设 height 或 max-height,100% 在嵌套中会失效——它向上找第一个有明确高度的祖先,找不到就按内容撑开。
实操建议:
立即学习“前端免费学习笔记(深入)”;
video 的直接父容器设 height: 100%(前提是该父容器已有明确高度,比如 html 和 body 都设了 height: 100%)aspect-ratio + width: 100% + height: auto 替代百分比高度,让浏览器自动算高video 直接设 height: 100% —— 它会强制拉伸,破坏比例这是移动端常见问题:即使 video 设了 width: 100%、object-fit: cover,某些 iOS/Android 浏览器仍会在竖屏时多渲染几像素,触发 overflow-x: scroll。根本不是视频本身宽了,而是布局链里某层没截断。
实操建议:
立即学习“前端免费学习笔记(深入)”;
body 上加 overflow-x: hidden —— 最简、最可靠,不影响功能html 和 body 都设了 width: 100% 和 margin: 0
video 自身的 overflow: hidden —— 它对 position: absolute 子元素无效position: absolute 布局,父容器必须设 position: relative,否则定位基准错乱单纯靠 aspect-ratio: 16 / 9 不够——当视口高度很小时(比如窄屏),容器高度会压缩,但 video 若设 height: 100% 就可能裁剪过度;若设 object-fit: contain,又留黑边。关键是让容器“优先守高”,再按比例缩放内容。
实操建议:
立即学习“前端免费学习笔记(深入)”;
display: grid + position: absolute 组合:父容器设 display: grid,video 设 position: absolute 并覆盖整个网格区域min-height: 100vh 而非 height: 100vh,防止内容少时塌陷video 用 width: 100vw + height: 100vh + object-fit: cover,再靠 top: 50% + left: 50% + transform: translate(-50%, -50%) 居中aspect-ratio,需 fallback:padding-top: 56.25%(16:9)+ position: relative + video 绝对定位填满第三方播放器(如 ReactPlayer)默认会生成内联样式或 wrapper div,常带固定宽高或未设 max-width,容易绕过你写的 CSS。它不是原生 video 标签,行为更不可控。
实操建议:
立即学习“前端免费学习笔记(深入)”;
ReactPlayer 外层容器设 width: 100%、max-width: 100%、height: auto,并启用 controls 和 playing 等 props 控制行为className 或 style 直接作用于其 wrapper,而非试图改写内部 video 标签width="100%"、height="auto",并确保父容器有 aspect-ratio 或明确高度max-width: 100% 或 overflow: hidden
overflow-x: hidden,以及父容器忘了 position: relative 就直接对子元素 absolute 定位。