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

如何在特定CSS类生效时修改指定ID元素的CSS样式?

Solution for Targeting #sidebarMenu When .nav-collapse.in.collapse is Active

Hey there! Let's get this sorted out for you. The problem with your initial CSS is that native CSS doesn't support selector nesting (that syntax works for preprocessors like Sass/Less, but plain CSS won't interpret it correctly). Here's how to fix it:

Correct Native CSS Syntax

Instead of nesting the #sidebarMenu selector inside .nav-collapse.in.collapse, you need to chain them together to target the sidebar only when the parent menu container has those active classes:

/* Target #sidebarMenu only when its ancestor has .nav-collapse.in.collapse classes */
.nav-collapse.in.collapse #sidebarMenu {
  width: min-content;
}

Why This Works for Your Toggle Scenario

Since you're using what looks like Bootstrap's collapse component, the .in class is dynamically added/removed when the menu is toggled open/closed. This means:

  • When the menu is toggled open, .nav-collapse.in.collapse is present, so #sidebarMenu gets the width: min-content style.
  • When the menu is toggled closed, the .in class is removed, so the sidebar reverts to its original width.

This perfectly matches your requirement of only applying the size change during the toggle operation.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 16:27:49