# 如何隐藏匿名查看者的侧边栏？

**URL:** <https://meta.discourse.org/t/how-do-i-hide-the-sidebar-for-anon-viewers/250311>\
**Category:** Support\
**Tags:** sidebar\
**Created:** [2023年一月1日 13:22 UTC](https://meta.discourse.org/t/how-do-i-hide-the-sidebar-for-anon-viewers/250311 "2023-01-01T13:22:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)\
**Post date:** [2023年一月1日 13:22 UTC](https://meta.discourse.org/t/how-do-i-hide-the-sidebar-for-anon-viewers/250311/1 "2023-01-01T13:22:55Z")

</div>

我们已经为我们的网站配置了 #sidebar，它主要是私有的，只有少量公共内容。

我们非常希望只在用户登录时才激活侧边栏（即不向 `anon` 显示）。

我尝试了以下 CSS：

```css
// 隐藏未登录用户的侧边栏（不幸的是，这会弄乱布局）
.anon {
    .sidebar-wrapper, .header-sidebar-toggle {
        display: none;
   }
}

```

这确实隐藏了侧边栏，但侧边栏的布局仍然存在（而且看起来不太好）。有没有更好的方法？

我知道我们可以切换到新的 Header Dropdown 视图（在 navigation\_menu 下）并以此方式进行排序，但我们确实希望在可能的情况下（无需点击）为已登录用户显示侧边栏。

---

<div class="post-metadata">

**Author:** ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)\
**Post date:** [2023年一月1日 16:09 UTC](https://meta.discourse.org/t/how-do-i-hide-the-sidebar-for-anon-viewers/250311/2 "2023-01-01T16:09:15Z")

</div>

您好，

使用此方法可以隐藏匿名用户的侧边栏。🙂

通用 / CSS

```scss
html.anon {
  // 隐藏窄桌面侧边栏
  // 宽度小于 1000px 时在桌面上激活的侧边栏移动版本
  .d-header .hamburger-panel {
    display: none;
  }
  // 隐藏菜单打开时的标题斗篷
  .header-cloak {
    display: none !important;
  }
}

```

桌面 / CSS

```scss
html.anon {
  // 如果侧边栏已打开，则使用关闭样式
  body.has-sidebar-page {
    #main-outlet-wrapper {
      grid-template-columns: 0 minmax(0, 1fr);
      gap: 0;
      padding-left: 10px;
    }
    .wrap {
      max-width: var(--d-max-width);
    }
  }
  // 隐藏汉堡包按钮
  .header-sidebar-toggle {
    display: none;
  }
}

```

移动端 / CSS

```scss
html.anon {
  // 隐藏汉堡包按钮
  .d-header-icons {
    .header-dropdown-toggle {
      &.hamburger-dropdown {
        display: none;
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)\
**Post date:** [2023年一月1日 22:02 UTC](https://meta.discourse.org/t/how-do-i-hide-the-sidebar-for-anon-viewers/250311/3 "2023-01-01T22:02:07Z")

</div>

你真是个大好人！

为了表达我的感激之情，我将其打包成了一个 #Customization > Theme component：

> **[GitHub - nathan-nz/discourse-hide-sidebar-from-anon: Hides the sidebar from anonymous users](https://github.com/nathan-nz/discourse-hide-sidebar-from-anon)**
>
> Hides the sidebar from anonymous users

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2023年一月31日 22:02 UTC](https://meta.discourse.org/t/how-do-i-hide-the-sidebar-for-anon-viewers/250311/4 "2023-01-31T22:02:55Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
