如何使用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赋予动态交互。三者在网页开发中各司其职,共同构建丰富多彩的网站。

创建 Bootstrap 分割线有两种方法:使用 标签,可创建水平分割线。使用 CSS border 属性,可创建自定义样式的分割线。

React通过JSX与HTML结合,提升用户体验。1)JSX嵌入HTML,使开发更直观。2)虚拟DOM机制优化性能,减少DOM操作。3)组件化管理UI,提高可维护性。4)状态管理和事件处理增强交互性。

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

在 Bootstrap 中插入图片有以下几种方法:直接插入图片,使用 HTML 的 img 标签。使用 Bootstrap 图像组件,可以提供响应式图片和更多样式。设置图片大小,使用 img-fluid 类可以使图片自适应。设置边框,使用 img-bordered 类。设置圆角,使用 img-rounded 类。设置阴影,使用 shadow 类。调整图片大小和位置,使用 CSS 样式。使用背景图片,使用 background-image CSS 属性。

要设置 Bootstrap 框架,需要按照以下步骤:1. 通过 CDN 引用 Bootstrap 文件;2. 下载文件并将其托管在自己的服务器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根据需要编译 Sass/Less;5. 导入定制文件(可选)。设置完成后,即可使用 Bootstrap 的网格系统、组件和样式创建响应式网站和应用程序。

如何使用 Bootstrap 按钮?引入 Bootstrap CSS创建按钮元素并添加 Bootstrap 按钮类添加按钮文本
