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

Semantic UI响应式菜单开发:桌面与移动端菜单切换实现需求

Hey there! Let's get your responsive desktop and mobile menu set up properly. Here's a complete breakdown of the code and how to control visibility across screen sizes:

Desktop & Mobile Responsive Menu Setup

HTML Structure

First, we'll define both menu structures—one for desktop, another for the mobile vertical sidebar.

Desktop Menu

This is your standard horizontal menu for larger screens:

<div class="ui borderless stackable container main menu">
  <!-- Add your desktop navigation items here -->
  <a class="item" href="#">Home</a>
  <a class="item" href="#">About</a>
  <a class="item" href="#">Services</a>
  <a class="item" href="#">Contact</a>
</div>

Mobile Vertical Sidebar Menu

This fixed sidebar will be used for mobile devices:

<div class="ui container fixed secondary menu">
  <!-- Add your mobile navigation items here -->
  <a class="item" href="#">Home</a>
  <a class="item" href="#">About</a>
  <a class="item" href="#">Services</a>
  <a class="item" href="#">Contact</a>
</div>

CSS for Styling & Responsive Visibility

We'll use CSS to style the menus and media queries to toggle their display based on screen size. I've completed the truncated CSS you provided and added mobile-specific styles:

/* Desktop menu styling */
.main.menu { 
  margin-top: 4em; 
  border-radius: 0; 
  border: none; 
  box-shadow: none; 
  transition: box-shadow 0.5s ease, padding 0.5s ease ; 
}

/* Hide mobile menu by default */
.secondary.menu { 
  display: none !important; 
  /* Mobile sidebar styles: fixed to left, full height, vertical layout */
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: 250px;
  flex-direction: column;
  background-color: #fff;
  z-index: 1000; /* Ensure it sits above other content */
}

/* Media query: Switch menus on mobile screens (adjust breakpoint as needed) */
@media only screen and (max-width: 768px) {
  /* Hide desktop menu on mobile */
  .main.menu {
    display: none !important;
  }
  
  /* Show mobile vertical sidebar */
  .secondary.menu {
    display: flex !important; /* Flex ensures vertical alignment of items */
  }
}

Quick Note

If you want to add a hamburger toggle button to show/hide the mobile sidebar (instead of displaying it by default on mobile), you can use a simple JavaScript snippet to toggle the display property of .secondary.menu when the button is clicked.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:37:18