调整窗口大小时如何让页面内容自动适配 修复背景适配异常问题
问题修复方案
问题根源
- 背景无法适配视口:html和body未设置占满视口高度,
background-size使用auto只会按原图尺寸渲染,同时未配置背景居中规则。 - 缩放时出现无限横向延伸线条:标题样式
.mchead设置了width: 200%,文本容器.mcp使用固定值left: 500px偏移,两个元素均会超出视口宽度,触发横向溢出,就会出现延伸横线和横向滚动条。
修复步骤
1. 新增全局基础样式
首先重置默认边距,配置html和body占满视口,禁止横向溢出,同时新增viewport元标签适配移动端:
* { margin: 0; padding: 0; box-sizing: border-box; } html, body { height: 100%; overflow-x: hidden; }
2. 修复背景适配规则
修改.minecraftbody的背景配置,让背景铺满视口且居中:
.minecraftbody { background-image: url(https://www.startpage.com/av/proxy-image?piurl=https%3A%2F%2Fwallpaper-house.com%2Fdata%2Fout%2F8%2Fwallpaper2you_213216.jpg&sp=1637611406T358bfd9113b26feaf1742f91b1f8cc55007334c10b4ecebee265c9435d20b5f9); background-repeat: no-repeat; background-size: cover; background-position: center center; background-attachment: fixed; /* 可选,滚动页面时背景固定,体验更好 */ }
3. 修复元素溢出问题
修改标题样式
删掉.mchead里的width:200%、height:200%和相对定位偏移,用边距调整位置:
.mchead { font-family: 'Lora', serif; color: white; margin: 26px 0 0 90px; }
修改文本容器样式
去掉固定left偏移,设置百分比宽度,添加你需要的蓝色背景,实现自适应:
.mcp { color: black; border: 5px solid #000; width: 60%; margin: 30px 5% 30px auto; padding: 20px; background-color: #b3d9ff; /* 可自行调整蓝色深浅 */ } /* 新增图片自适应规则,避免图片超出容器 */ .mcp img { max-width: 100%; height: auto; margin-top: 15px; }
完整修改后代码
<!DOCTYPE html> <html lang="en"> <head> <title>Minecraft</title> <meta charset="UTF-8"> <meta name="author" content="My Name"> <meta name="keywords" content="HTML, CSS, JavaScript"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <style> @import url('https://fonts.googleapis.com/css2?family=Lora:ital,wght@1,700&display=swap'); * { margin: 0; padding: 0; box-sizing: border-box; } html, body { height: 100%; overflow-x: hidden; } .Gaming { font-family: 'Lora', serif; color: red; } .border { background-color: white; margin: 30px; padding: 20px; position: relative; border: 5px solid; width: 50%; height: 50%; } .ave { width: 25%; height: 50; } .app { width: 60%; height: 60%; } .minecraftbody { background-image: url(https://www.startpage.com/av/proxy-image?piurl=https%3A%2F%2Fwallpaper-house.com%2Fdata%2Fout%2F8%2Fwallpaper2you_213216.jpg&sp=1637611406T358bfd9113b26feaf1742f91b1f8cc55007334c10b4ecebee265c9435d20b5f9); background-repeat: no-repeat; background-size: cover; background-position: center center; background-attachment: fixed; } .homebg { background-image: url(https://images.unsplash.com/photo--82d13781b33a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=872&q=80); background-size: auto; } .mchead { color: white; margin: 26px 0 0 90px; font-family: 'Lora', serif; } .mcp { color: black; border: 5px solid #000; width: 60%; margin: 30px 5% 30px auto; padding: 20px; background-color: #b3d9ff; } .mcp img { max-width: 100%; height: auto; margin-top: 15px; } </style> </head> <body class='minecraftbody'> <h1 class="mchead">MINECRAFT</h1> <div class="mcp"> <p>Minecraft is one of my favorite games ever since I was still a child. I made many friends through it and <br> countless memories was and are being made.</p> <br><br><br> <p>This is a picture of one of my servers that me and my friends made in Minecraft. <br> We had so much fun making this house and I still come back to this place everytime I feel nostalgic <br> about my past</p> <img src="Images/Mc_server.png" alt="minecraft server"> </div> </body> </html>
内容的提问来源于stack exchange,提问作者H2WO
相关产品推荐
相关产品推荐

