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

如何在桌面窗口高度低于特定值时用CSS取消元素固定定位?

实现屏幕高度低于特定值时取消元素固定定位

你可以通过**CSS媒体查询(Media Query)**针对屏幕高度设置阈值,当视口高度低于指定值时,覆盖固定定位样式,将元素恢复为相对定位。

具体操作如下:

  • 先确定触发样式切换的屏幕高度阈值(比如600px,可根据实际需求调整)
  • 添加针对max-height的媒体查询,在查询内重置.fixed-section的定位属性

完整代码示例:

/* 基础固定样式 */
.fixed-section {
  position: fixed !important;
  left: 0 !important;
  top: 0 !important;
  pointer-events: none !important;
}

.fixed-section .column-content {
  pointer-events: auto !important;
}

/* 移动端/平板禁用固定效果 */
.fixed-section { position: relative !important; }

/* 屏幕高度低于600px时取消固定 */
@media (max-height: 600px) {
  .fixed-section {
    position: relative !important;
    left: auto !important;
    top: auto !important;
  }
}

说明:

  • max-height: 600px表示视口高度小于等于600px时应用该样式,可根据实际场景修改数值
  • 重置left和top为auto,避免固定定位的残留属性影响布局

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 04:42:04