navigation (navbar) 導航條或叫導覽列

 

準備一範例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>bootstrap hello world</title>

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

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>
        p {
            height: 100vh;
        }

        .p1 {
            background-color: #b6ff00;
        }

        .p2 {
            background-color: #0094ff;
        }

        .p3 {
            background-color: #ffd800;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container">

            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Brand</a>
            </div>

            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <label>When the label's width less 768px then that will be hide</label>
            </div>
        </div>
    </nav>
    <p class="p1">1</p>
    <p class="p2">2</p>
    <p class="p3">3</p>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

一些相關class效果說明如下

 

一、.navbar-default

主要對background-color上「淡」色而已,

若不指定navbar-default樣式,則底色為透明。

 

二、.navbar-inverse

主要對background-color上「深」色而已,

若不指定navbar-inverse樣式,則底色為透明。

 

三、.navbar-fixed-top

加在navbar-default的後面,如下

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
</nav>

其特性為不管滑鼠怎麼捲動,navigation bar 將一直固定在視窗上。

官方建議將body作

body {
    padding-top: 50px;
}

以免body內容被遮到。

 

四、.navbar-fixed-bottom

加在navbar-default的後面,如下

<nav class="navbar navbar-default navbar-fixed-bottom" role="navigation">
</nav>

其效果就是navbar-fixed-top的相反,

其特性為不管滑鼠怎麼捲動,navigation bar 將一直固定在視窗下。

 

五、.navbar-static-top

加在navbar-default的後面,如下

<nav class="navbar navbar-default navbar-static-top" role="navigation">
</nav>

其為navbar的預設樣式,當捲動滑鼠時,navigation bar 將跟著往上捲,

就算不特別指定navbar-static-top,其預設效果就是navbar-static-top。

 

六、.navbar-header、navbar-brand、navbar-toggle

navbar-header表示為navigation bar縮起來的範圍,

如上範例,navbar-header裡面有兩元素,<botton>與<a>,

如果 a 元素要作為brand時,class屬性就加上navbar-brand,

botton要作為選單圖示時,則class屬性就加上navbar-toggle,如下

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
</button>

「data-toggle="collapse"」是固定要加的,而data-target屬性的指定為對哪一個元素做toggle特效。

當標上.navbar-brand或.navbar-toggle樣式時,兩元素可不分順序。

 

七、.navbar-collapse

當決定哪一個元素要被做toggle特效時,則固定加上「collapse navbar-collapse」樣式,如下

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <label>When the label's width less 768px then that will be hide</label>
</div>

 

八、.navbar-form

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>bootstrap hello world</title>

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

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>
        p {
            height: 100vh;
        }

        .p1 {
            background-color: #b6ff00;
        }

        .p2 {
            background-color: #0094ff;
        }

        .p3 {
            background-color: #ffd800;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container">

            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Brand</a>
                <form class="navbar-form" role="search">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search">
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>
            </div>

            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <label>When the label's width less 768px then that will be hide</label>
            </div>
        </div>
    </nav>
    <p class="p1">1</p>
    <p class="p2">2</p>
    <p class="p3">3</p>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

如果要在navigation bar裡使用表單的話,請加上navbar-form樣式,

整體看起來會比較正常。

 

九、.navbar-left、.navbar-right

承上,在表單裡還可加上navbar-left樣式,讓表單裡的元素皆靠左

<form class="navbar-form navbar-left" role="search">
    <div class="form-group">
        <input type="text" class="form-control" placeholder="Search">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

效果如下圖

而navbar-right樣式則為靠右。

 

十、.pull-left、.pull-right

承上,在表單裡加上pull-left樣式,讓表單裡的元素皆靠左,

特性跟navbar-left一樣,只是樣式不太一樣而已。

<form class="navbar-form pull-left" role="search">
    <div class="form-group">
        <input type="text" class="form-control" placeholder="Search">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

效果如下圖

而pull-right樣式則為靠右。

 

十一、.navbar-text

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>bootstrap hello world</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        p {
            height: 100vh;
        }

        .p1 {
            background-color: #b6ff00;
        }

        .p2 {
            background-color: #0094ff;
        }

        .p3 {
            background-color: #ffd800;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Brand</a>
            </div>
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <label class="navbar-text">When the label's width less 768px then that will be hide</label>
            </div>
        </div>
    </nav>
    <p class="p1">1</p>
    <p class="p2">2</p>
    <p class="p3">3</p>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

承原範例,你有沒有覺得label元素裡面的字很不搭嘎,

請使用 navbar-text 樣式,讓字看起來和協一點,如下圖

navbar-text 樣式不限應用於label元素,p元素也可以。

 

十二、.navbar-link

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>bootstrap hello world</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        p {
            height: 100vh;
        }

        .p1 {
            background-color: #b6ff00;
        }

        .p2 {
            background-color: #0094ff;
        }

        .p3 {
            background-color: #ffd800;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Brand</a>
            </div>
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <label class="navbar-text navbar-right">Signed in as <a href="#" class="navbar-link">Mark Otto</a></label>
            </div>
        </div>
    </nav>
    <p class="p1">1</p>
    <p class="p2">2</p>
    <p class="p3">3</p>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

特性跟navbar-text一樣,navbar-link用途就是讓a元素的字看起來和協一點,如下圖

 

十三、.navbar-btn

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>bootstrap hello world</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        p {
            height: 100vh;
        }

        .p1 {
            background-color: #b6ff00;
        }

        .p2 {
            background-color: #0094ff;
        }

        .p3 {
            background-color: #ffd800;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Brand</a>
                <button type="button" class="btn btn-default navbar-btn">Sign in</button>
            </div>
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <label class="navbar-text navbar-right">Signed in as <a href="#" class="navbar-link">Mark Otto</a></label>
            </div>
        </div>
    </nav>
    <p class="p1">1</p>
    <p class="p2">2</p>
    <p class="p3">3</p>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

這是在navigation bar裡使用botton時,的botton專屬樣式,

原因無他,就是讓整體看起來更好看而已。