Analyze bootstrap navigation bar and its responsive implementation
The purpose of this article: to analyze the bootstrap navigation bar and its responsive implementation, so as to improve your programming technology to a higher level
Let’s analyze it first How to implement it? The first step is to paste a bootstrap navigation bar template
2. The code is as follows
1 <nav> 2 <div> 3 <div> 4 <button> 5 <span>Toggle navigation</span> 6 <span></span> 7 <span></span> 8 <span></span> 9 </button>10 <a>Project name</a>11 </div>12 <div>13 <ul>14 <li> <a>Home</a> </li>15 <li><a>About</a></li>16 <li><a>Contact</a></li>17 <li>18 <a>Dropdown <span></span></a>19 <ul>20 <li><a>Action</a></li>21 <li><a>Another action</a></li>22 <li><a>Something else here</a></li>23 <li>24 <li>Nav header</li>25 <li><a>Separated link</a></li>26 <li><a>One more separated link</a></li>27 </ul>28 </li>29 </ul>30 <ul>31 32 </ul>33 </div> <!--/.nav-collapse -->34 </div> <!--/.container-fluld -->35 </nav>
The effect is as follows;
Mobile terminal:
3. Code analysis
Analyze the role of each tag and its style from the outside to the inside
3.1 The outermost div container (style is navbar navbar-default navbar-fixed-top):
Source code
.navbar { position: relative; min-height: 50px;/**导航条最小宽度为50px**/ margin-bottom: 20px;/****/ border: 1px solid transparent; }@media (min-width: 768px) {/**>=768的设备,其实就是pc,移动设备width属性都小于768px**//**可能有很多人不理解,实际上移动端的width属性是以device-width来计量的,不是单纯的像数的概念,建议有疑问的同学自行搜索device-width关键字**/ .navbar { border-radius: 4px;/****/ }} .navbar-default {/**设备导航栏的配色**/ background-color: #f8f8f8; border-color: #e7e7e7; }.navbar-fixed-top, .navbar-fixed-bottom { position: fixed;/**相对浏览器定位**/ right: 0; left: 0; z-index: 1030;/**样式层叠在上层的优先级**/ }
As can be seen from the source code, the main function of the outermost div container is to create a bar container (.navbar) with a minimum height of 50px, which is positioned relative to the browser (.navbar- fixed-top), determine the color of the navigation bar (.navbar-default)
For relevant knowledge about device-width, please refer to this article
3.2 div container with navbar-header style
The css source code is as follows
<br>
/**在pc端显示时向右浮动,在移动端此样式无效**/
@media (min-width: 768px) { .navbar-header { float: left; }}
The display effect of this div on the PC and mobile terminals is as follows
PC side:
Mobile terminal:
It can be seen that on the PC side, the browser width is sufficient, and this div only exists as a small block-level element; and On the mobile side, because the screen width is not enough, other elements of the navigation bar are implemented in the form of drop-down menus, and this div alone fills the parent container.
There are two text elements under navbar-header:
The css source code is as follows:
.navbar-toggle {/**在最右侧画了一个圆角矩形**/ position: relative; float: right; padding: 9px 10px; margin-top: 8px; margin-right: 15px; margin-bottom: 8px; background-color: transparent; background-image: none; border: 1px solid transparent; border-radius: 4px; }.navbar-toggle:focus { outline: 0; }@media (min-width: 768px) {/**此button在pc端不显示**/ .navbar-toggle { display: none; }} .navbar-toggle .icon-bar {/**icon-bar负责在button盒子里画横线**/ display: block; width: 22px; height: 2px; border-radius: 1px; }.navbar-brand { float: left; height: 50px; padding: 15px 15px; font-size: 18px; line-height: 20px; }
At this point, we have figured out the navbar-header component. This is a responsive layout. On the PC side, navbar-header only displays brand text. On the mobile side, navbar-header will Exclusively occupy the entire navbar, and other parts will be hidden.
3.3 Continue to look at the navbar-collapse collapse component
Source code:
/**由于.navbar-collapse,.navbar-collapse.in,.collapse在(@meida min-width:768px)pc端均有定义,故一下的属性只对移动端有效**/
.navbar-collapse { padding-right: 15px; padding-left: 15px; overflow-x: visible; -webkit-overflow-scrolling: touch; border-top: 1px solid transparent; -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, .1); box-shadow: inset 0 1px 0 rgba(255, 255, 255, .1); }.navbar-collapse.in {/**点击navbar-header的navbar-toggle的button后, navbar-collapse collapse会被js修改成。navbar-collapse collapse in**/ overflow-y: auto; }.collapse {/**决定了本组件在移动端时不显示**/ display: none;/**点击事件发生后将被覆盖**/ }<br>
.collapse.in {/**After the click event occurs, it is displayed as a block-level element, covering display: none**/<br> display : block;<br> }
@media (min-width: 768px) {/**pc端**/ .navbar-collapse { width: auto;border-top: 0;-webkit-box-shadow: none;box-shadow: none; } .navbar-collapse.collapse {display: block !important;/**作为块级显示,由于兄弟节点navbar-header是浮动元素,所以navbar-collapse会占满父元素的宽高**/height: auto !important;padding-bottom: 0;overflow: visible !important; } .navbar-collapse.in {overflow-y: visible; } .navbar-fixed-top .navbar-collapse, .navbar-static-top .navbar-collapse, .navbar-fixed-bottom .navbar-collapse {padding-right: 0;padding-left: 0; }}
So far, we have also figured out how the navbar-collapse collapse component is hidden on the mobile side. navbar-collapse is responsible for the appearance style of the component, and .collapse is responsible for it. Whether the entire component is displayed (normally displayed on the PC side, not displayed on the mobile side (display: none), displayed as a block-level element after the click event occurs)
4. Summary
Navigation from bootstrap Analysis of the column source code shows the following points
4.1: The size style and color matching style of bootstrap are set separately. It can be imagined that such settings can be combined at will, which increases the reusability of the code and can also be used at will. Modify the color scheme according to your needs.
4.2: Implementation of the navigation bar: The fixing method of the navigation bar is implemented by navbar-fixed-top. Other values include navbar-static-top and the default value. The display effect is also different (determines the display position of the entire navigation bar)
The color implementation is implemented by navbar-default, and the entire color matching can be modified by modifying navbar-defau (determines the color matching of the entire navigation bar)
§§
##5. Note: Many of the details such as changes in margin padding are not discussed in this article. You can refer to this articleThe above is the detailed content of Analyze bootstrap navigation bar and its responsive implementation. For more information, please follow other related articles on the PHP Chinese website!

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 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.

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 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.

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.

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

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool