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

网站超出屏幕出现横向滚动条,CSS适配问题求助

解决网站横向滚动条、适配屏幕宽度的问题

你的网站出现横向滚动条,核心是CSS的两处错误导致元素宽度超出屏幕:

问题定位

  1. 全局样式选择器错误:原代码中* body写法无效,无法重置所有元素的默认margin和padding,浏览器默认样式会让部分元素产生额外宽度。
  2. 导航栏固定内边距适配问题:padding: 10px 90px的固定左右内边距,在小屏幕下会让导航栏总宽度超出100%,结合fixed定位特性触发横向滚动。

修复方案

1. 修正全局样式,统一盒模型

将全局选择器改为*, body,并添加box-sizing: border-box确保所有元素的宽度计算包含内边距和边框:

*, body {
  margin: 0;
  padding: 0;
  font-family: Arial;
  box-sizing: border-box;
}

2. 优化导航栏内边距,适配多屏幕

把导航栏的固定左右内边距改为百分比,避免小屏幕下溢出:

nav {
  width: 100%;
  height: 100px;
  background: rgba(0, 0, 0, 0.6);
  border-bottom: 1px solid #fff;
  margin: 0;
  padding: 10px 5%; /* 替换固定90px为百分比,自适应屏幕 */
  top: 0;
  left: 0;
  box-sizing: border-box;
  position: fixed;
}

3. 兜底设置(可选)

给body添加横向溢出隐藏,防止其他元素意外溢出:

body {
  overflow-x: hidden;
}

完整修复后的CSS代码

*, body {
  margin: 0;
  padding: 0;
  font-family: Arial;
  box-sizing: border-box;
}

nav {
  width: 100%;
  height: 100px;
  background: rgba(0, 0, 0, 0.6);
  border-bottom: 1px solid #fff;
  margin: 0;
  padding: 10px 5%;
  top: 0;
  left: 0;
  box-sizing: border-box;
  position: fixed;
}

nav .logo {
  padding: 22px 20px;
  height: 80px;
  float: left;
  font-size: 24px;
  font-weight: bold;
  text-transform: uppercase;
  color: #fff;
}

nav ul {
  list-style: none;
  float: right;
  margin: 0;
  padding: 0;
  display: flex;
}

nav ul li a {
  line-height: 80px;
  color: #fff;
  padding: 12px 30px;
  text-decoration: none;
  font-size: 14px;
  font-weight: bold;
  text-transform: uppercase;
}

nav ul li a:hover {
  background: rgba(0, 0, 0, 0.7);
  border-radius: 6px;
}

section {
  width: 100%;
  height: 100vh;
  background: url(background.jpg);
  background-size: cover;
  background-position: center;
}

.container {
  width: 100%;
  height: 100vh;
  padding: 0 8%;
}

.container h1 {
  text-align: center;
  padding-top: 10%;
  margin-bottom: 60px;
  font-weight: 800;
  position: relative;
}

.container h1::after {
  content: '';
  background: #303ef7;
  width: 100px;
  height: 5px;
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
}

.row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  grid-gap: 30px;
}

.service {
  text-align: center;
  padding: 25px 10px;
  border-radius: 5px;
  font-size: 14px;
  cursor: pointer;
  background: transparent;
  transition: transform 0.5s, background 0.5s;
}

.service i {
  font-size: 40px;
  margin-bottom: 10px;
  color: #303ef7;
}

.service h2 {
  font-weight: 600;
  margin-bottom: 8px;
}

.service:hover {
  background: #303ef7;
  color: #fff;
  transform: scale(1.05);
}

.service:hover i {
  color: #fff;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 21:01:19