Home Web Front-end JS Tutorial Detailed explanation of navigation bar implementation examples in Bootstrap

Detailed explanation of navigation bar implementation examples in Bootstrap

Jan 13, 2018 pm 05:25 PM
bootstrap Example Detailed explanation

This article mainly shares with you detailed examples of Bootstrap implementation of navigation. When building a website, different pages have many elements that are the same, such as navigation bars, sidebars, etc. We can use template inheritance to avoid repeated writing. html code. Now we plan to implement a navigation bar at the top of the web page and inherit this navigation bar in all pages. To create the navigation bar, we directly use the following navigation bar style provided by Bootstrap.

Related recommendations: "Bootstrap Tutorial"

Detailed explanation of navigation bar implementation examples in Bootstrap

But I am using Bootstrap Before setting the navigation bar style, you need to reference the css and js files required by Bootstrap as well as jQuery. We are ## in html Insert the following code into #header to complete the reference:

1

2

3

<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>

<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">

Copy after login
Here are references to the

css and js files on the Internet through links instead of downloading them Come down and quote from local. After that, we copy all the html code in the above picture to the body of html, and the browser will display the same navigation bar as in the picture. Let’s make some simple modifications and optimizations. Finally, our navigation bar will look like this:

Detailed explanation of navigation bar implementation examples in Bootstrap

The specific modification point is that I changed the original Brand was replaced with a picture as the logo, the first drop-down control Dropdown was deleted, an option was added to the rightmost drop-down control, and the text was replaced with what we wanted. Then I created a

base.css file to adjust the image size, control position, background color, etc. This part is basic html/css knowledge, so I won’t go into details. All subsequent web pages will use this navigation bar. We will name the html containing the navigation bar base.html, and add the following code below the navigation bar code in its body :

1

2

{% block body_part %}

{% endblock %}

Copy after login
Then create a new

home.html and enter the following content:

1

2

3

4

{% extends 'base.html' %}

{% block body_part %}

<p>This is body content under nav-bar</p>

{% endblock %}

Copy after login
Render

home.html and visit, we can see this The result:

Detailed explanation of navigation bar implementation examples in Bootstrap##So it is not difficult for us to understand that in

home.html

, {% extends 'base .html' %} represents the code block inherited from base.html, home.html in the block and endblock intervals It will be automatically replaced with the block area of ​​base.html also named body_part. You can use multiple block, for example, you can also use it in <title>, so that different pages can have different titles. Finalbase.html
The code is as follows: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt;     &lt;meta charset=&quot;UTF-8&quot;&gt;     &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css&quot; integrity=&quot;sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u&quot; crossorigin=&quot;anonymous&quot;&gt;     &lt;script src=&quot;https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js&quot;&gt;&lt;/script&gt;     &lt;script src=&quot;https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js&quot; integrity=&quot;sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;     &lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url_for(&amp;#39;static&amp;#39;,filename=&amp;#39;css/base.css&amp;#39;) }}&quot;/&gt;     &lt;link rel=&quot;shortcut icon&quot; href=&quot;{{ url_for(&amp;#39;static&amp;#39;, filename=&amp;#39;images/favicon.ico&amp;#39;) }}&quot;&gt;     &lt;title&gt;{% block page_name %}{% endblock %}-HarpQA&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;nav class=&quot;navbar navbar-default&quot;&gt;     &lt;p class=&quot;container&quot;&gt;         &lt;!-- Brand and toggle get grouped for better mobile display --&gt;         &lt;p class=&quot;navbar-header&quot;&gt;           &lt;button type=&quot;button&quot; class=&quot;navbar-toggle collapsed&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#bs-example-navbar-collapse-1&quot; aria-expanded=&quot;false&quot;&gt;             &lt;span class=&quot;sr-only&quot;&gt;Toggle navigation&lt;/span&gt;             &lt;span class=&quot;icon-bar&quot;&gt;&lt;/span&gt;             &lt;span class=&quot;icon-bar&quot;&gt;&lt;/span&gt;             &lt;span class=&quot;icon-bar&quot;&gt;&lt;/span&gt;           &lt;/button&gt;           &lt;a class=&quot;navbar-brand&quot;&gt;               &lt;img class=&quot;logo&quot; src=&quot;{{ url_for(&amp;#39;static&amp;#39;,filename=&amp;#39;images/logo.png&amp;#39;) }}&quot;&gt;           &lt;/a&gt;         &lt;/p&gt;         &lt;!-- Collect the nav links, forms, and other content for toggling --&gt;         &lt;p class=&quot;collapse navbar-collapse&quot; id=&quot;bs-example-navbar-collapse-1&quot;&gt;           &lt;ul class=&quot;nav navbar-nav&quot;&gt;             &lt;li class=&quot;active&quot;&gt;&lt;a href=&quot;#&quot;&gt;首页 &lt;span class=&quot;sr-only&quot;&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;             &lt;li&gt;&lt;a href=&quot;#&quot;&gt;发布问答&lt;/a&gt;&lt;/li&gt;           &lt;/ul&gt;           &lt;form class=&quot;navbar-form navbar-left&quot;&gt;             &lt;p class=&quot;form-group&quot;&gt;               &lt;input type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Key Words&quot;&gt;             &lt;/p&gt;             &lt;button type=&quot;submit&quot; class=&quot;btn btn-default&quot;&gt;搜索&lt;/button&gt;           &lt;/form&gt;           &lt;ul class=&quot;nav navbar-nav navbar-right&quot;&gt;             &lt;li&gt;&lt;a href=&quot;#&quot;&gt;登录&lt;/a&gt;&lt;/li&gt;             &lt;li&gt;&lt;a href=&quot;#&quot;&gt;注册&lt;/a&gt;&lt;/li&gt;              &lt;li class=&quot;dropdown&quot;&gt;               &lt;a href=&quot;#&quot; class=&quot;dropdown-toggle&quot; data-toggle=&quot;dropdown&quot; role=&quot;button&quot; aria-haspopup=&quot;true&quot; aria-expanded=&quot;false&quot;&gt;友情链接&lt;span class=&quot;caret&quot;&gt;&lt;/span&gt;&lt;/a&gt;               &lt;ul class=&quot;dropdown-menu&quot;&gt;                 &lt;li&gt;&lt;a href=&quot;mailto:liutao25@baidu.com&quot;&gt;联系我&lt;/a&gt;&lt;/li&gt;                 &lt;li&gt;&lt;a href=&quot;http://flask.pocoo.org&quot; target=&quot;_blank&quot;&gt;Flask官网&lt;/a&gt;&lt;/li&gt;                 &lt;li&gt;&lt;a href=&quot;https://www.python.org/&quot;&gt;Python官网&lt;/a&gt;&lt;/li&gt;                 &lt;li role=&quot;separator&quot; class=&quot;pider&quot;&gt;&lt;/li&gt;                 &lt;li&gt;&lt;a href=&quot;https://www.baidu.com&quot; target=&quot;_blank&quot;&gt;百度搜索&lt;/a&gt;&lt;/li&gt;                 &lt;li role=&quot;separator&quot; class=&quot;pider&quot;&gt;&lt;/li&gt;                 &lt;li&gt;&lt;a href=&quot;https://www.google.com.hk&quot; target=&quot;_blank&quot;&gt;Google Search&lt;/a&gt;&lt;/li&gt;               &lt;/ul&gt;             &lt;/li&gt;           &lt;/ul&gt;         &lt;/p&gt;&lt;!-- /.navbar-collapse --&gt;       &lt;/p&gt;&lt;!-- /.container-fluid --&gt; &lt;/nav&gt; {% block body_part %} {% endblock %} &lt;/body&gt; &lt;/html&gt;</pre><div class="contentsignin">Copy after login</div></div>Please note the references to

base.css

and logo images, we also use url_for function, the first parameter is static, which represents the static folder under the project folder, and the second parameter is staticThe folder is the relative path of the benchmark static file. We put js files/css files/image files, etc. under this folder, so this usage will be used frequently in the future. When we bookmark a web page, the web page has a small icon. We can also use this line of html code in header to achieve this: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;link rel=&quot;shortcut icon&quot; href=&quot;{{ url_for(&amp;#39;static&amp;#39;, filename=&amp;#39;images/favicon.ico&amp;#39;) }}&quot;&gt;</pre><div class="contentsignin">Copy after login</div></div>Put

favicon.ico

The file can be placed in the static/images folder. We used the Flask icon, and the effect is as follows:

Detailed explanation of navigation bar implementation examples in Bootstrap##base.css

The code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

body{

    background: #F3F3F3;

}

 

.navbar-brand{

    padding: 0 5px;

    padding-right: 10px;

}

 

.logo{

    height: 50px;

}

Copy after login
Related recommendations:


How to implement the fixed positioning effect of jQuery navigation bar

htmlNavigation bar production graphic code sharing

How does JavaScript achieve the effect of highlighting the current menu of the navigation bar after selecting it?

The above is the detailed content of Detailed explanation of navigation bar implementation examples in Bootstrap. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles