如何使用HTML和CSS設計一個現代的側邊欄選單?
當你考慮一個典型網站的佈局時,很可能會在主要內容區域的右側或左側包含一列重要的連結(用於網頁中各個部分的導航鏈接)。
這個元件被稱為“側邊欄”,通常用作網頁上的選單。雖然它被廣泛使用,但開發人員通常將此元素添加到網站上,用於在頁面之間導航,甚至導航到網頁的不同部分。
讓我們了解這個功能,並嘗試只使用HTML和CSS來建立一個現代的側邊欄。
什麼是側邊欄選單?
側邊欄是位於主要內容區域右側或左側的靜態列。此元件包含網站中的導覽連結、小工具或其他必要的連結(用於主頁、內容或其他部分)。
下面給出一個範例,示範如何建立一個簡單的側邊欄選單。此選單位於主內容區域的左側(與大多數網站的佈局相同)。
範例
在這個範例中,我們使用 CSS 網格將網頁分成兩個部分。網頁的 15% 構成側邊欄選單,85% 構成主要內容。
CSS網格
透過設定 display: grid,它使開發人員能夠將任何元素轉換為網格容器。要新增列,我們使用,
grid-template-columns: value value;
value代表列的寬度。它可以用長度(px、cm、em)或百分比來表示。
標籤(錨元素)
它用於在網頁內部連結外部頁面。它也可以用於連結文檔內部的部分。 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="https://www.php.cn/link/115c51eb37365df2d4f4e2482b964822" 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 id="Animation-Character"> Animation Character </h1> </header> <section class="main-section" id="background"> <header> <h1 id="Background"> 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 id="Relations"> 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 過渡屬性 來平滑地改變側邊欄的位置。點選按鈕時,側邊欄的 位置 從 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 id="This-is-an-example-This-contains-the-main-content-area"> 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 設計的。
以上是如何使用HTML和CSS設計一個現代的側邊欄選單?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

React通過JSX與HTML結合,提升用戶體驗。 1)JSX嵌入HTML,使開發更直觀。 2)虛擬DOM機制優化性能,減少DOM操作。 3)組件化管理UI,提高可維護性。 4)狀態管理和事件處理增強交互性。

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

創建 Bootstrap 分割線有兩種方法:使用 標籤,可創建水平分割線。使用 CSS border 屬性,可創建自定義樣式的分割線。

要設置 Bootstrap 框架,需要按照以下步驟:1. 通過 CDN 引用 Bootstrap 文件;2. 下載文件並將其託管在自己的服務器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根據需要編譯 Sass/Less;5. 導入定製文件(可選)。設置完成後,即可使用 Bootstrap 的網格系統、組件和样式創建響應式網站和應用程序。

在 Bootstrap 中插入圖片有以下幾種方法:直接插入圖片,使用 HTML 的 img 標籤。使用 Bootstrap 圖像組件,可以提供響應式圖片和更多樣式。設置圖片大小,使用 img-fluid 類可以使圖片自適應。設置邊框,使用 img-bordered 類。設置圓角,使用 img-rounded 類。設置陰影,使用 shadow 類。調整圖片大小和位置,使用 CSS 樣式。使用背景圖片,使用 background-image CSS 屬性。

如何使用 Bootstrap 按鈕?引入 Bootstrap CSS創建按鈕元素並添加 Bootstrap 按鈕類添加按鈕文本
