如何使用HTML和CSS設計現代側邊欄選單?


當您考慮典型網站的佈局時,您可能會在主要內容區域的右側或左側包含一列重要的連結(網頁中各個部分的導航連結)。

此元件稱為“側邊欄”,通常用作網路上的選單。雖然它通常被使用,但開發人員通常將此元素新增到網站中,以便從一個頁面導航到另一個頁面,甚至導航到網頁的不同部分。

讓我們瞭解一下這個功能,並嘗試只使用HTML和CSS建立一個現代側邊欄。

什麼是側邊欄選單?

側邊欄是位於主要內容區域右側或左側的靜態列。此元件包含網站中的導航連結、小部件或其他必要的連結(用於主頁、內容或其他部分)。

下面是一個示例,演示如何建立一個簡單的側邊欄選單。此選單位於主要內容區域的左側(大多數網站都遵循此方式)。

示例

在這個例子中,我們使用CSS網格將網頁分成兩個部分。網頁的15%構成側邊欄選單,而85%構成主要內容。

CSS網格

它允許開發人員透過設定`display: grid`將任何元素轉換為網格容器。要新增列,我們使用:

grid-template-columns: value value;

代表列的寬度。它可以用長度(px、cm、em)或百分比表示。

<a>標籤(錨元素)

它用於在網頁內連結外部頁面。它也可以用於連結文件內的內部部分。`id`屬性唯一地定義了元素。

<a href= "#"> </a>

href屬性包含外部頁面的url或文件內內部部分的id

<!DOCTYPE html>
<html>
<head>
   <title> Sidebar menu </title>
   <style>
      #main-doc {
         display: grid;
         grid-template-columns: 15% 85%;
         grid-template-rows: auto;
         grid-template-areas: "advert content";
      }

      .item1 {
         padding: 10px;
      }

      #head {
         font-family: serif !important;
         color: #8b0000 !important;
         font-weight: 900;
         margin: 5px;
         padding: 0 5px 5px;
      }

      .main-section {
         font-family: Brush Script MT;
         font-size: 20px;
         color: #000080;
      }

      .item2 {
         background: linear-gradient(-35deg, #fff000, #ffb6c1, #afeeee);
         padding: 6px 8px 6px 16px;
         margin: 0
      }

      .contents {
         font-size: 26px !important;
         color: grey;
      }

      .item1 a {
         border-radius: 5px;
         padding: 6px 16px 6px 16px;
         display: block;
      }
      a:hover {
         color: red;
         transform: scale(1.1);
      }
   </style>
</head>
<body>
   <main id="main-doc">
   <div class="item1">
      <nav id="navbar">
         <header class="contents">
            <strong> Contents </strong>
         </header>
         <br>
         <a href="#background" class="nav-link"> Background </a>
         <br>
         <hr>
         <a href="#romance" class="nav-link"> Romance </a>
         <br>
         <hr>
         <a href="#relations" class="nav-link"> Relations </a>
         <br>
         <hr>
         <a href="#voice_actors" class="nav-link"> Voice Actors </a>
         <br>
         <hr>
         <a href="#costumes" class="nav-link"> Costumes </a>
         <br>
         <hr>
         <a href="#gallery" class="nav-link"> Gallery </a>
         <br>
         <hr>
      </nav>
   </div>
   <div class="item2">
   <header id="head">
      <h1> Animation Character </h1>
   </header>
   <section class="main-section" id="background">
      <header>
         <h1> Background </h1>
      </header>
      <hr>
      <p> This is placeholder text. This paragraph contains information about the background of the character. </p>
   </section>
   <section class="main-section" id="romance">
      <header>
         <h1> Romance <h1>
         <hr>
      </header>
      <p> This paragraph contains text related to the life of the character. </p>
   </section>
   <section class="main-section" id="relations">
      <header>
      <h1> Relations </h1>
      </header>
      <hr>
      <ul>
         <li> Mother <br>
         <p> Text about character's mother </p>
         <li> Father <br>
         <p> Information about the father. </p>
         <li> Sister <br>
         <p> Text about character's sister </p>
         <li> Friend <br>
         <p> Text about friend </p>
      </ul>
   </section>
   <section class="main-section" id="voice_actors">
      <header>
         <h1> Voice actors
            <hr>
         </h1>
      </header>
      <p> This contains information about voice actors in the animation </p>
   </section>
   <section class="main-section" id="costumes">
      <header>
         <h1> Costumes
            <hr>
         </h1>
      </header>
      <br>
      <br>
   </section>
</body>
</html>

示例

在這裡,我們將建立一個切換側邊欄。我們建立了一個側邊欄並將其定位在內容區域的左側。我們在內容區域中添加了一個按鈕,單擊該按鈕可以摺疊建立的側邊欄。

我們使用了CSS transition屬性來平滑地更改側邊欄的位置。單擊按鈕後,側邊欄的位置從0變為-160px(等於側邊欄的寬度)。換句話說,側邊欄向移動了其寬度的距離。

<!DOCTYPE html>
<html>
<head>
   <title> Toggle Sidebar </title>
   <style>
      body {
         margin: 0;
      }
      .container {
         display: flex;
         min-height: 90px;
      }
      .sidebar {
         position: relative;
         left: 0;
         margin-right: 20px;
         width: 160px;
         background-color: #ccc;
         transition: all 0.20s;
      }

      .sidebar.collapsed {
         left: -160px;
         margin-right: -150px;
      }
   </style>
</head>
<body>
   <div class="container">
      <div class="sidebar" id="sidebar">
         <strong> Sidebar menu </strong>
         <ul>
            <a href="#" class="nav-link">
               <li> Link 1 </li>
            </a>
            <a href="#" class="nav-link">
               <li> Link 2 </li>
            </a>
            <a href="#" class="nav-link">
               <li> Link 3 </li>
            </a>
            <a href="#" class="nav-link">
               <li> Link 4 </li>
            </a>
         </ul>
      </div>
      <div class="content">
         <h2> This is an example. This contains the main content area. </h2>
         <br> Click the button below to toggle the sidebar <br>
         <br>
         <button onclick="document.getElementsByClassName('sidebar')[0].classList.toggle('collapsed')"> toggle Sidebar </button>
      </div>
   </div>
</body>
</html>

結論

在本文中,我們討論了網頁中兩種型別的側邊欄選單。其中一個是基本側邊欄,另一個是切換側邊欄。兩者都是僅使用HTML和CSS設計的。

更新於:2023年4月28日

2K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.