如何防止浏览器窗口缩放时页面元素尺寸发生变化?
问题:调整浏览器窗口时固定页面元素尺寸
如何在调整浏览器窗口大小时,让页面中的.rectangle元素宽度和高度保持固定不变?尝试过用像素(px)替代百分比单位、将元素放入设置了1000px宽高的div容器,但都没效果。
当前页面代码如下:
<!DOCTYPE html> <html> <head> <title>Kirjautuminen</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto"> <style> body { font-family: Roboto, sans-serif; font-size: 1vw; margin: 0; padding: 0; background-color: black; min-height: 100vh; height: 1000px; width: 1000px; } .username input { position: absolute; left: 50vw; top: 45vh; transform: translateX(-50%); width: 250px; height: 20px; } .password input { position: absolute; left: 50vw; top: 50vh; transform: translateX(-50%); width: 250px; height: 20px; } .modern-button { background-color: #333; color: #fff; border: none; padding: 10px 20px; border-radius: 5px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; width: 150px; margin: 0 10px 10px 10px; } .modern-button:hover { background-color: #ddd; } .submit-button input{ position: absolute; left: 50vw; top: 55vh; transform: translateX(-50%); width: 5vw; height: 3vh; } img { width: auto; height: 100vh; display: block; margin: 0 auto; position: absolute; left: 0; top: 50; z-index = -1; } label { color: white; margin-right: 100px; } .header { position: relative; left: 50vw; top: 20vh; text-align: center; transform: translateX(-50%); z-index: +1 } .paragraph1 { position: absolute; left: 50vw; top: 39vh; transform: translateX(-50%); z-index: +1 } .rectangle { position: absolute; width: 400px; height: 23vh; background-color: #808080; border-radius: 30px; top: 50%; left: 50%; transform: translate(-50%,-50%); } h1 { color: white; font-size: 50px; } h2 { color: white; } p { color: white; font-size: 20px; } ::placeholder { font-size: 20px; text-align: left; } </style> </head> <body> <div style="min-width: max-content; min-height: max-content;"> <div class = "rectangle"></div> <form action="/home" method="POST"> <div class = "username"> <input type="username" id="username" name="username" style="border-radius: 5px; padding: 0.25%;" placeholder="Käyttäjätunnus"><br> </div> <div class = "password"> <input type="password" id="password" name="password" style="border-radius: 5px; padding: 0.25%;" placeholder="Salasana"><br> </div> <div class = "submit-button"> <input type="submit" value="Kirjaudu"> </div> <div class = "header"> <h1>mywebpage.net</h1> </div> <div class = "paragraph1"> <p>Kirjaudu sisään:</p> </div> </form> </div> </body> </html>
解决方案
你的核心问题在于.rectangle的高度使用了vh(视口高度)单位,这个单位会随浏览器窗口高度变化而改变;同时其他表单元素依赖vh/vw定位,窗口变化时也会联动偏移。按以下步骤修改即可解决:
1. 固定.rectangle的宽高为像素单位
把.rectangle的height: 23vh替换为固定像素值,比如height: 200px,保留width: 400px:
.rectangle { position: absolute; width: 400px; height: 200px; /* 替换vh为固定像素 */ background-color: #808080; border-radius: 30px; top: 50%; left: 50%; transform: translate(-50%,-50%); position: relative; /* 添加相对定位,作为内部元素的定位参考 */ }
2. 调整内部元素的定位逻辑
将表单元素从基于视口的定位,改为基于.rectangle的相对定位,确保元素始终在矩形内部:
- 把表单元素的
left: 50vw改为left: 50%(相对于矩形容器) - 用固定像素值设置
top属性,替代vh单位
修改后的完整代码
<!DOCTYPE html> <html> <head> <title>Kirjautuminen</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto"> <style> body { font-family: Roboto, sans-serif; font-size: 16px; /* 替换vw为固定像素,避免字体随窗口缩放 */ margin: 0; padding: 0; background-color: black; min-height: 100vh; } .rectangle { position: absolute; width: 400px; height: 200px; background-color: #808080; border-radius: 30px; top: 50%; left: 50%; transform: translate(-50%,-50%); position: relative; } .username input { position: absolute; left: 50%; top: 30px; transform: translateX(-50%); width: 250px; height: 20px; } .password input { position: absolute; left: 50%; top: 80px; transform: translateX(-50%); width: 250px; height: 20px; } .submit-button input{ position: absolute; left: 50%; top: 130px; transform: translateX(-50%); width: 100px; /* 替换vw为固定像素 */ height: 30px; /* 替换vh为固定像素 */ } .header { position: absolute; left: 50%; top: 10vh; transform: translateX(-50%); z-index: 1; } .paragraph1 { position: absolute; left: 50%; top: calc(50% - 130px); transform: translateX(-50%); z-index: 1; } h1 { color: white; font-size: 50px; } p { color: white; font-size: 20px; } ::placeholder { font-size: 16px; text-align: left; } </style> </head> <body> <div class="rectangle"> <form action="/home" method="POST"> <div class="username"> <input type="username" id="username" name="username" style="border-radius: 5px; padding: 2px;" placeholder="Käyttäjätunnus"><br> </div> <div class="password"> <input type="password" id="password" name="password" style="border-radius: 5px; padding: 2px;" placeholder="Salasana"><br> </div> <div class="submit-button"> <input type="submit" value="Kirjaudu"> </div> </form> </div> <div class="header"> <h1>mywebpage.net</h1> </div> <div class="paragraph1"> <p>Kirjaudu sisään:</p> </div> </body> </html>
关键修改总结
- 所有需要固定尺寸的元素,统一使用
px单位替代vh/vw/百分比 - 将表单元素移入
.rectangle内部,利用相对定位确保元素位置不随窗口变化偏移 - 替换
body的font-size: 1vw为固定像素,避免字体随窗口缩放
内容的提问来源于stack exchange,提问作者M M
相关产品推荐
相关产品推荐

