方法-固定メニュー
CSSを使用して「固定」メニューを作成する方法を学びます。
固定トップメニューを作成する方法
ステップ1)HTMLを追加します。
例
<div class="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
</div>
<div class="main">
<p>Some text some text some text some text..</p>
</div>
ステップ2)CSSを追加します。
固定トップメニューを作成するには、とを使用position:fixed
しtop:0
ます。固定メニューは他のコンテンツをオーバーレイすることに注意してください。これを修正するにmargin-top
は、メニューの高さと同じかそれ以上の(コンテンツに)を追加します。
例
/* The navigation bar */
.navbar {
overflow: hidden;
background-color: #333;
position: fixed; /* Set
the navbar to fixed position */
top: 0;
/* Position the navbar at the top of the page */
width:
100%; /* Full width */
}
/* Links inside the navbar */
.navbar a {
float: left;
display:
block;
color: #f2f2f2;
text-align:
center;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar
a:hover {
background: #ddd;
color:
black;
}
/* Main
content */
.main {
margin-top: 30px; /* Add a top
margin to avoid content overlay */
}
固定ボトムメニューを作成する方法
固定ボトムメニューを作成するには、およびを使用position:fixed
し
bottom:0
ます。
例
/* The navigation bar */
.navbar {
position: fixed; /* Set the navbar to fixed position */
bottom: 0;
/* Position the navbar at the bottom of the page */
width:
100%; /* Full width */
}
/* Main
content */
.main
{
margin-bottom: 30px; /* Add a bottom margin to avoid content overlay */
}
ヒント:ナビゲーションバーの詳細については、CSSNavbarチュートリアルにアクセスしてください。