
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a Fixed Menu with CSS
To create a fixed menu on a web page, use the position property and set the value fixed. Set the width to 100% using the width property.
Set the Navigation Menu
Use the <nav> element to create the navigation menu on a web page. The links are set using the <a> and the href attribute −
<nav> <a class="links selected" href="#">Home</a> <a class="links" href="#">Login</a> <a class="links" href="#">Register</a> <a class="links" href="#">Contact Us</a> <a class="links" href="#">More Info</a> </nav>
Style the Navigation Menu
We have positioned the menu as fixed using the position property with the value fixed. The width is set to 100% using the width property −
Nav { position: fixed; top: 0; width: 100%; background-color: rgb(39, 39, 39); overflow: auto; height: auto; }
Style the Links
The menu links are set to inline-block value for the display property. The display suggests how to control the layout of an element. In this case, the inline-block of the display property displays an element as an inline-level block container. The text decoration is set to none −
.links { display: inline-block; text-align: center; padding: 14px; color: rgb(178, 137, 253); text-decoration: none; font-size: 17px; }
On Hover
The background color of a link is changed on hover using the :hover selector −
.links:hover { background-color: rgb(100, 100, 100); }
Selected Link
The background color is changed for the link that is selected −
.selected{ background-color: rgb(0, 18, 43); }
Example
The following is the code to produce a fixed menu with CSS −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body{ margin:0px; margin-top:60px; padding: 0px; } nav{ position: fixed; top: 0; width: 100%; background-color: rgb(39, 39, 39); overflow: auto; height: auto; } .links { display: inline-block; text-align: center; padding: 14px; color: rgb(178, 137, 253); text-decoration: none; font-size: 17px; } .links:hover { background-color: rgb(100, 100, 100); } .selected{ background-color: rgb(0, 18, 43); } </style> </head> <body> <nav> <a class="links selected" href="#"> Home</a> <a class="links" href="#"> Login</a> <a class="links" href="#"> Register</a> <a class="links" href="#"> Contact Us</a> <a class="links" href="#">More Info</a> </nav> <div class="sample-content"> <h1>Here are some headers</h1> <h2>Here are some headers</h2> <h3>Here are some headers</h3> <h4>Here are some headers</h4> <h1>Here are some headers</h1> <h2>Here are some headers</h2> <h3>Here are some headers</h3> <h4>Here are some headers</h4> </div> </body> </html>