如何让浏览器窗口缩放时按钮保持固定位置?
解决绝对定位按钮窗口缩放时错位/重叠的问题
问题根源
你给按钮设置了left:94%+固定宽度100px,当浏览器窗口宽度缩到小于1667px左右时,94%的位置加上按钮宽度会超出视口,导致按钮被挤变形甚至重叠。虽然用了position:absolute,但定位逻辑没利用好绝对定位的特性。
方案一:快速修改定位属性(适配现有代码)
把left:94%改成right:1%(右侧间距可自行调整),让按钮始终贴着右侧边缘,不会随窗口宽度变化错位。同时去掉重复的position:absolute(通用类里已经定义过)。
修复后的CSS:
html, body { width: 100%; height: 100%; margin: 0px; border: 0; overflow: hidden; /* Disable scrollbars */ display: block; /* No floating content on sides */ } .button-a,.button-b { padding: 0.6em 2em; border: none; outline: none; color: rgb(255, 255, 255); background: rgb(209, 192, 192); cursor: pointer; position: absolute; z-index: 0; border-radius: 10px; user-select: none; -webkit-user-select: none; touch-action: manipulation; } .button-a:before,.button-b:before { content: ""; background: linear-gradient( 45deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000 ); position: absolute; top: -2px; left: -2px; background-size: 400%; z-index: -1; filter: blur(5px); -webkit-filter: blur(5px); width: calc(100% + 4px); height: calc(100% + 4px); animation: glowing-button-color 20s linear infinite; transition: opacity 0.3s ease-in-out; border-radius: 10px; } @keyframes glowing-button-color { 0% { background-position: 0 0; } 50% { background-position: 400% 0; } 100% { background-position: 0 0; } } .button-a:after,.button-b:after { z-index: -1; content: ""; position: absolute; width: 100%; height: 100%; background: #222; left: 0; top: 0; border-radius: 10px; } .button-a { top:1%; right:1%; /* 替换left,设置右侧间距 */ width:100px; height:40px; background: none; } .button-b { top:5.9%; right:1%; /* 替换left,设置右侧间距 */ width:100px; height:40px; background: none; }
HTML保持不变:
<button type="button" class="button-a" id="button_a">A</button> <button type="button" class="button-b" id="button_b">B</button>
方案二:用Flex布局(更现代易维护)
如果不想用绝对定位,给body加Flex布局,把按钮固定在右侧,自动垂直排列,不用手动算top值。
修改后的HTML:
<div class="button-container"> <button type="button" class="button-a" id="button_a">A</button> <button type="button" class="button-b" id="button_b">B</button> </div>
修改后的CSS:
html, body { width: 100%; height: 100%; margin: 0px; border: 0; overflow: hidden; display: flex; justify-content: flex-end; /* 让按钮容器靠右 */ padding: 1% 1% 0 0; /* 顶部+右侧间距 */ box-sizing: border-box; /* 避免padding撑大视口 */ flex-direction: column; /* 按钮垂直排列 */ gap: 10px; /* 按钮之间的间距,替代手动top值 */ } .button-a,.button-b { padding: 0.6em 2em; border: none; outline: none; color: rgb(255, 255, 255); background: rgb(209, 192, 192); cursor: pointer; z-index: 0; border-radius: 10px; user-select: none; -webkit-user-select: none; touch-action: manipulation; width:100px; height:40px; background: none; position: relative; /* 确保伪元素正常工作 */ } .button-a:before,.button-b:before { content: ""; background: linear-gradient( 45deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000 ); position: absolute; top: -2px; left: -2px; background-size: 400%; z-index: -1; filter: blur(5px); -webkit-filter: blur(5px); width: calc(100% + 4px); height: calc(100% + 4px); animation: glowing-button-color 20s linear infinite; transition: opacity 0.3s ease-in-out; border-radius: 10px; } @keyframes glowing-button-color { 0% { background-position: 0 0; } 50% { background-position: 400% 0; } 100% { background-position: 0 0; } } .button-a:after,.button-b:after { z-index: -1; content: ""; position: absolute; width: 100%; height: 100%; background: #222; left: 0; top: 0; border-radius: 10px; }
内容的提问来源于stack exchange,提问作者Pastel
相关产品推荐
相关产品推荐

