search
HomeWeb Front-endHTML TutorialIntroduction to navigation bar (navbar) and navigation (nav)

Bootstrap Navigation Bar

The navigation bar is a good feature and a prominent feature of the Bootstrap website. The navigation bar serves as the responsive foundational component of the navigation header in your app or website. The navigation bar collapses in mobile viewports and expands horizontally as the available viewport width increases. At the core of the Bootstrap navigation bar, the navigation bar includes the site name and basic navigation definition styles.

Default navigation bar

The steps to create a default navigation bar are as follows:

Add class .navbar, .navbar-default to the

Adding role="navigation" to the above element will help increase accessibility.

Add a header class .navbar-header to the

element, which contains the element with class navbar-brand. This will make the text appear larger.

To add a link to the navigation bar, simply add an unordered list with class .nav, .navbar-nav.

Previous words

The difference between navigation bar (navbar) and navigation (nav) is only one word, and there is an extra word "bar". In fact, they are still obviously different in the Bootstrap framework. There is a background color in the navigation bar (navbar), and the navigation bar can be a pure link (similar to navigation), a form, or a combination of form and navigation. This article will introduce the Bootstrap navigation bar in detail

Basic navigation bar

In the Bootstrap framework, the navigation bar and navigation are not much different in appearance, but in actual use Navigation bars are much more complex than navigation.

The navigation bar is a responsive basic component used as the navigation header in an application or website. They can be folded (and opened or closed) on mobile devices, and gradually change to horizontal expansion mode when the width of the viewport (viewport) increases

When making a basic navigation bar, there are mainly the following steps: :

 1. First, add the class name “navbar-nav” on the basis of making the navigation list (

    )

     2. Add a new one outside the list Container (div), and use the class names "navbar" and "navbar-default"

    [Principle Analysis]

    The main function of the ".navbar" style is to set left and right padding and rounded corners, etc. effect, but its color-related style has not been set in any way

    .navbar {
      position: relative;
      min-height: 50px;
      margin-bottom: 20px;
      border: 1px solid transparent;
    }

    The color of the navigation bar is controlled through ".navbar-default"

    .navbar-default {
      background-color: #f8f8f8;
      border-color: #e7e7e7;
    }

    The navbar-nav style re-adjusts the floating and inner and outer margins of menu items based on navigation.nav. It also does not include color and other style settings, and color and other styles are implemented together with the parent container "navbar-default"

     [Note] It is best to use the <nav></nav> element , if you are using the generic <div> element, be sure to set the <code>role="navigation" attribute for the navigation bar, so that users using assistive devices know clearly that this is A navigation area

    <div> <ul> <li><a>网站首页</a></li>
    <li><a>系列教程</a></li>
    <li><a>名师介绍</a></li>
    <li><a>成功案例</a></li>
    <li><a>关于我们</a></li> </ul>
    </div>

    [Title]

    In Web page production, there is often a title (text) in front of the menu The font size is slightly larger than other text). In fact, this aspect has also been considered in the Bootstrap framework, which is implemented through "navbar-header" and "navbar-brand"

    <div>
       <div>
           <a>小火柴的蓝色理想</a>
       </div>
    <ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li>   <li><a>Bootstrap</a></li>   <li><a>jQuery</a></li> </ul>
    </div>


    【Brand Icon】

    Replace the place where the brand logo is placed in the navigation bar with the <img src="/static/imghwm/default1.png" data-src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAB+0lEQVR4AcyYg5LkUBhG+1X2PdZGaW3btm3btm3bHttWrPomd1r/2Jn/VJ02TpxcH4CQ/dsuazWgzbIdrm9dZVd4pBz4zx2igTaFHrhvjneVXNHCSqIlFEjiwMyyyOBilRgGSqLNF1jnwNQdIvAt48C3IlBmHCiLQHC2zoHDu6zG1iXn6+y62ScxY9AODO6w0pvAqf23oSE4joOfH6OxfMoRnoGUm+de8wykbFt6wZtA07QwtNOqKh3ZbS3Wzz2F+1c/QJY0UCJ/J3kXWJfv7VhxCRRV1jGw7XI+gcO7rEFFRvdYxydwcPsVsC0bQdKScngt4iUTD4Fy/8p7PoHzRu1DclwmgmiqgUXjD3oTKHbAt869qdJ7l98jNTEblPTkXMwetpvnftA0LLHb4X8kiY9Kx6Q+W7wJtG0HR7fdrtYz+x7iya0vkEtUULIzCjC21wY+W/GYXusRH5kGytWTLxgEEhePPwhKYb7EK3BQuxWwTBuUkd3X8goUn6fMHLyTT+DCsQdAEXNzSMeVPAJHdF2DmH8poCREp3uwm7HsGq9J9q69iuunX6EgrwQVObjpBt8z6rdPfvE8kiiyhsvHnomrQx6BxYUyYiNS8f75H1w4/ISepDZLoDhNJ9cdNUquhRsv+6EP9oNH7Iff2A9g8h8CLt1gH0Qf9NMQAFnO60BJFQe0AAAAAElFTkSuQmCC" class="lazy" alt="Introduction to navigation bar (navbar) and navigation (nav)" > element to display your own brand icon. Since .navbar-brand has been set with padding and height, you need to add some CSS code according to your own situation to override the default settings

    <div>
       <div>  <a><img  src="/static/imghwm/default1.png" data-src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAB+0lEQVR4AcyYg5LkUBhG+1X2PdZGaW3btm3btm3bHttWrPomd1r/2Jn/VJ02TpxcH4CQ/dsuazWgzbIdrm9dZVd4pBz4zx2igTaFHrhvjneVXNHCSqIlFEjiwMyyyOBilRgGSqLNF1jnwNQdIvAt48C3IlBmHCiLQHC2zoHDu6zG1iXn6+y62ScxY9AODO6w0pvAqf23oSE4joOfH6OxfMoRnoGUm+de8wykbFt6wZtA07QwtNOqKh3ZbS3Wzz2F+1c/QJY0UCJ/J3kXWJfv7VhxCRRV1jGw7XI+gcO7rEFFRvdYxydwcPsVsC0bQdKScngt4iUTD4Fy/8p7PoHzRu1DclwmgmiqgUXjD3oTKHbAt869qdJ7l98jNTEblPTkXMwetpvnftA0LLHb4X8kiY9Kx6Q+W7wJtG0HR7fdrtYz+x7iya0vkEtUULIzCjC21wY+W/GYXusRH5kGytWTLxgEEhePPwhKYb7EK3BQuxWwTBuUkd3X8goUn6fMHLyTT+DCsQdAEXNzSMeVPAJHdF2DmH8poCREp3uwm7HsGq9J9q69iuunX6EgrwQVObjpBt8z6rdPfvE8kiiyhsvHnomrQx6BxYUyYiNS8f75H1w4/ISepDZLoDhNJ9cdNUquhRsv+6EP9oNH7Iff2A9g8h8CLt1gH0Qf9NMQAFnO60BJFQe0AAAAAElFTkSuQmCC" class="lazy" alt="Introduction to navigation bar (navbar) and navigation (nav)" >  </a>
    </div>
    <ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li>   <li><a>Bootstrap</a></li>   <li><a>jQuery</a></li> </ul>
    </div>


    [Second-level menu]

    The basic navigation bar provides the current state, disabled state, suspended state and other effects for the menu, and can also have a secondary menu. The navigation bar

    <div><ul> <li><a>网站首页</a></li>
    <li>  <a>系列教程<span></span></a>  <ul>
    <li><a>CSS3</a></li>
    <li><a>JavaScript</a></li>
    <li><a>PHP</a></li>  </ul>   </li>   <li><a>关于我们</a></li>
    </ul></div>


    【部件排列】

      通过添加 .navbar-left 和 .navbar-right工具类让导航链接、表单、按钮或文本对齐。两个类都会通过 CSS 设置特定方向的浮动样式。例如,要对齐导航链接,就要把它们放在个分开的、应用了工具类的

      标签里

        这些类是 .pull-left 和 .pull-right 的 mixin 版本,但是他们被限定在了媒体查询(media query)中,这样可以更容易的在各种尺寸的屏幕上处理导航条组件

        [注意]导航条目前不支持多个.navbar-right类。为了让内容之间有合适的空隙,最后一个.navbar-right元素使用负边距(margin)。如果有多个元素使用这个类,它们的边距(margin)将不能按照预期正常展现

      【表单】

        有的导航条中会带有搜索表单,Bootstrap框架中提供了一个“navbar-form”,使用方法很简单,在navbar容器中放置一个带有navbar-form类名的表单即可

        navbar-left实现左浮动,navbar-right实现右浮动

      <div>
      <ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li>   <li><a>Bootstrap</a></li>   <li><a>jQuery</a></li> </ul> <form>   <div>  <input>   </div>
      <button>搜索</button> </form>
      </div>


      【按钮】

        对于不包含在 <form></form> 中的 <button></button> 元素,加上 .navbar-btn 后,可以让它在导航条里垂直居中。有一些对于为辅助设备提供可识别标签的方法,例如, aria-labelaria-labelledby 或者 title 属性。如果这些方法都没有,屏幕阅读器将使用 placeholder 属性(如果这个属性存在的话),但是请注意,使用 placeholder 代替其他识别标签的方式是不推荐的

        [注意]就像标准的 按钮类 一样,.navbar-btn 可以被用在 <a></a> 和 <input> 元素上。然而,在 .navbar-nav 内,.navbar-btn 和标准的按钮类都不应该被用在 <a></a> 元素上。

      <div>
      <ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul>
      <button>Sign in</button>
      </div>


      【文本】

        把文本包裹在 .navbar-text中时,为了有正确的行距和颜色,通常使用 <p></p> 标签

      <div>
      <ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul>
      <p>Signed in as huochai</p>
      </div>


      【非导航链接】

        可以在标准的导航组件之外添加标准链接,使用 .navbar-link 类可以让链接有正确的默认颜色和反色设置

      <div>
      <ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul>
      <p>Signed in as <a>huochai</a></p>
      </div>


       

      导航条位置

        很多情况下,设计师希望导航条固定在浏览器顶部或底部,这种固定式导航条的应用在移动端开发中更为常见。Bootstrap框架提供了g两种固定导航条的方式:

         ☑  .navbar-fixed-top:导航条固定在浏览器窗口顶部

         ☑  .navbar-fixed-bottom:导航条固定在浏览器窗口底部

        使用方法很简单,只需要在制作导航条最外部容器navbar上追加对应的类名即可

        这个固定的导航条会遮住页面上的其它内容,除非给  元素底部设置了 padding。提示:导航条的默认高度是 50px

      body { padding-top: 70px; }
      body { padding-bottom: 70px; }
      <nav><ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul></nav><p>测试内容<br><br>测试内容<br><br>测试内容<br><br>测试内容<br><br>测试内容<br><br>测试内容<br><br>测试内容</p><nav><ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul></nav>

      【静止在顶部】

        通过添加 .navbar-static-top 类即可创建一个与页面等宽度的导航条,它会随着页面向下滚动而消失

      <nav><ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul></nav><p>测试内容<br><br><br>测试内容<br><br><br>测试内容<br><br><br>测试内容<br><br><br>测试内容<br><br><br>测试内容<br><br><br>测试内容</p>

       

      响应式导航条

        Bootstrap的响应式导航条实现如下:

        1、保证在窄屏时需要折叠的内容必须包裹在带一个div内,并且为这个div加入collapse、navbar-collapse两个类名。最后为这个div添加一个class类名或者id名

      <div><ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul></div>

        或者

      <div><ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul></div>

        2、保证在窄屏时要显示的图标样式(固定写法):

      <button>
        <span>Toggle Navigation</span>
        <span></span>
        <span></span>
        <span></span></button>

        3、并为button添加data-target=".类名/#id名",究竞是类名还是id名呢?由需要折叠的div来决定。如

      <button>
        <span>Toggle Navigation</span>
        <span></span>
        <span></span>
        <span></span></button>

        或者,对应class="example"

      <button>
        <span>Toggle Navigation</span>
        <span></span>
        <span></span>
        <span></span></button>
      <div>
        <div>   <button> <span>Toggle Navigation</span> <span></span> <span></span> <span></span>   </button>
        </div>
        <div>
      <ul>   <li><a>HTML</a></li>   <li><a>CSS</a></li>   <li><a>Javascript</a></li> </ul>
        </div>
      </div>


       

      反色导航条

        反色导航条其实是Bootstrap框架提供的第二种风格的导航条,与默认的导航条相比,使用方法并无区别,只是将navbar-deafult类名换成navbar-inverse。其变化只是导航条的背景色和文本做了修改

      <p></p>
    <div>
    <ul>   
    <li><a>HTML</a></li>   
    <li><a>CSS</a></li>   <li>
    <a>Javascript</a>
    </li> 
    </ul>
    </div>


     

The above is the detailed content of Introduction to navigation bar (navbar) and navigation (nav). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.