วิธีการ - เมนูคงที่
เรียนรู้วิธีสร้างเมนู "คงที่" ด้วย 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 */
}
เคล็ดลับ:ไปที่CSS Navbar Tutorialเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับแถบนำทาง