You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

overflow-x在小分辨率/移动设备上失效问题求助

问题:移动端视图下overflow-x: hidden失效

我搭建了一个包含横向面板的页面,将#panels-container的宽度设为500%,并给body设置了overflow-x: hidden。桌面常规视图中横向滚动被正常隐藏,但在移动设备或Chrome开发者工具的移动端视图中,页面仍能向右滚动。

原代码

CSS

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-weight: 300;
}

body {
  overflow-x: hidden;
  overflow-y: scroll;
}

header {
  position: fixed;
  display: flex;
  justify-content: space-between;
  width: 100%;
  border: 4px solid black;
  padding: 2rem;
}
.intro {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 10px solid green;
}

#panels #panels-container {
  height: 100vh;
  display: flex;
  flex-wrap: nowrap;
  padding: 0;
  overflow: hidden;
  background-color: #ddd;
}

#panels #panels-container .panel {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display: flex;
  color: #333;
  text-align: left;
  border-right: 1px solid #f00;
}

.panel {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: 600;
  font-size: 1.5em;
  text-align: center;
  color: white;
  position: relative;
  box-sizing: border-box;
  padding: 10px;
}

.full-screen {
  display: block;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.breaking {
  display: flex;
  flex: 1;
  justify-content: center;
}

.left {
  margin-right: 1rem;
}

.right {
  margin-left: 1rem;
}

HTML

<html lang="en" class="h-full m-0 p-0">
  <head>
    <meta charset="utf-8" />
    <title>overflow-x</title>
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <link rel="stylesheet" href="src/styles.css" />
  </head>
  <body>
    <div id="root">
      <main>
        <section class="intro"><h1>Intro</h1></section>
        <section id="panels">
          <div id="panels-container" style="width: 500%;">
            <article class="panel">
              <div class="breaking">
                <div class="left">left</div>
                <div class="right">right</div>
              </div>
            </article>
            <article class="panel">
              <h1>Panel 2</h1>
            </article>
            <article class="panel">
              <h1>Panel 3</h1>
            </article>
            <article class="panel">
              <h2>Panel 4</h2>
            </article>
            <article class="panel">
              <h2>Panel 5</h2>
            </article>
          </div>
        </section>
        <section id="map" class="full-screen"><h1>End</h1></section>
      </main>
    </div>
  </body>
</html>

解决方案

修复要点

  1. 给html添加overflow-x: hidden:移动端浏览器会优先以子元素宽度计算视口,同时限制html和body的横向溢出,能覆盖默认行为。
  2. 给#panels容器添加overflow-x: hidden:将横向溢出的限制直接作用在面板的父容器上,避免溢出传递到页面根层级。

修复后的完整CSS

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-weight: 300;
  overflow-x: hidden; /* 新增:给html添加横向溢出隐藏 */
}

body {
  overflow-y: scroll;
}

header {
  position: fixed;
  display: flex;
  justify-content: space-between;
  width: 100%;
  border: 4px solid black;
  padding: 2rem;
}
.intro {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 10px solid green;
}

#panels {
  overflow-x: hidden; /* 新增:面板容器添加横向溢出隐藏 */
}

#panels #panels-container {
  height: 100vh;
  display: flex;
  flex-wrap: nowrap;
  padding: 0;
  overflow: hidden;
  background-color: #ddd;
}

#panels #panels-container .panel {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display: flex;
  color: #333;
  text-align: left;
  border-right: 1px solid #f00;
}

.panel {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: 600;
  font-size: 1.5em;
  text-align: center;
  color: white;
  position: relative;
  box-sizing: border-box;
  padding: 10px;
}

.full-screen {
  display: block;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.breaking {
  display: flex;
  flex: 1;
  justify-content: center;
}

.left {
  margin-right: 1rem;
}

.right {
  margin-left: 1rem;
}

内容的提问来源于stack exchange,提问作者dagda1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 07:12:05