Navigation Bar With Hover Line Effect Using HTML And CSS
Navbar is a crucial element for every website as it helps users navigate and understand the website's content. A well-designed navigation bar enhances user experience by making interactions smooth and visually appealing. Various animation effects can be added using CSS, JavaScript, and libraries to improve the navbar's aesthetics.
In this effect, when we hover over a navigation link, a line appears above it, starting from zero width and expanding. This effect adds a sleek, modern touch to the navigation bar.
Read Also:
- Interactive Playing Cards Animation Using Html Css Only
- Star Rating On Hover Using Html Css And Javascript
- Next Button Design With Hover Effect Using Html Css Only
- Animated Click To Send Button Using Html Css And Gsap
- Animated Share Button Design Using Html Css And Javascript
- Pharma Capsule Animation Using Html Css and Svg
- Sticky Navigation Bar Design With Css Only
- Image Visibility Animation Using Html Css And jQuery
- Social Media Share Button With Hover Effect Using Html Css Only
- Yin and Yang Design With Single Div Using Html Css
- Responsive Our Team Page Design Using Html and Css Only
- Login Form In Html Css
- Button With Shadow Hover Effect Using Html Css Only
- Heart-Shape Design with Heartbeat Effect Using Html and Css
01. HTML:
Before writing code, we need to create a basic structure for an HTML document. Writing a basic structure is very important because it helps the browser easily understand the document type and initiate necessary processes. The basic HTML structure is shown below.
<!DOCTYPE html>
<html>
<head>
<title>Navigation Bar</title>
<style>
/* CSS will be added here */
</style>
</head>
<body>
<script type="text/javascript">
// JavaScript if needed
</script>
</body>
</html>
After writing the basic structure of the HTML document, you can add the required tags and content. Once done, run the code in your browser to see the output. As we know, HTML only displays content in a basic format and does not style it. To enhance the appearance of our content, we need CSS, which is added to an HTML document to create an attractive user interface.
<!DOCTYPE html>
<html>
<head>
<title>Navigation Bar</title>
<style>
/* CSS will be added here */
</style>
</head>
<body>
<div class="container">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
<a href="#">Login</a>
</div>
<script type="text/javascript"></script>
</body>
</html>
Code Explanation:
- The
<div class="container">
wraps the navigation links. - Each
<a>
tag represents a menu item. - A placeholder
<style>
tag is included for adding CSS styling. - A
<script>
tag is added for JavaScript functionality if needed.
Output
Read Also:
03. Add CSS Styling
Now, it's time to enhance the appearance of our HTML content by adding CSS to our HTML file. CSS can be integrated into an HTML document in three ways. Here, we are using the internal CSS method. Apply the CSS shown below and view the output in your browser.
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: sans-serif;
background: #000;
text-align: center;
background-image: url(bg.jpg);
background-size: cover;
background-repeat: no-repeat;
}
.container {
overflow: hidden;
background-color: black;
text-align: center;
}
.container a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 18px;
text-transform: uppercase;
position: relative;
}
.container a::before {
content: "";
width: 0%;
height: 3px;
display: block;
background-color: #fff;
position: absolute;
top: 0;
left: 0;
transition: width 0.5s;
}
.container a:hover::before {
width: 100%;
}
Code Explanation:
- The
*
selector appliesbox-sizing: border-box;
to all elements. - The
body
has a black background, centered text, and a full-screen background image. - The
.container
styles the navbar with a black background and centered links. - Navigation links:
- Styled with white text, uppercase letters, and padding.
position: relative;
enables the hover effect.
- The
::before
pseudo-element creates a hover underline:- Initially hidden (
width: 0%;
), expands on hover. - Uses smooth transition (
width 0.5s;
).
- Initially hidden (
Output
Read Also:
- Preloader Loading Animation | HTML, CSS, and GSAP
- Social Media Buttons Animation With Rotation Hover Effect | HTML & CSS
- Pure CSS Progress Bar Animation | HTML and CSS
- Shining Effect On Text | HTML and CSS
- Show and Hide Password Animation | HTML, CSS, and JavaScript
- Simple Button Group Design | HTML and CSS
- Responsive Newsletter Subscription Form Design | HTML and CSS
- Responsive Price Table Design | HTML and CSS
Read Also:
- Side Navigation Bar Design | HTML And CSS
- Sticky Navigation Bar Design With CSS Only
- Animated Button With Pressed Effect | HTML And CSS
- Smooth Parallax Scrolling Effect | HTML And CSS
- Announcement Popup Box Using PopboxJs | Rustcode
- Cursor Animation With Hover Effect Using GSAP | HTML, CSS and GSAP
- Custom Mouse Cursor JavaScript | HTML, CSS and PointerJs
- HTML Elements Smooth Drag and Drop Animation | HTML, CSS and Sortable
- Particle Background Animation | HTML, CSS and ParticleJs
- Responsive Portfolio Landing Page | HTML, CSS, jQuery and GSAP
- Two Image Slider | HTML, CSS and JsPlugin
04. Video Tutorial
To gain a better understanding of the implementation, watch our detailed YouTube video. We regularly share web development tutorials, so be sure to visit our YouTube channel and subscribe for more valuable content!
If you would like to download the complete source code, you can get it from the link below:
Read Also:
- How to find LCM of two numbers in javascript
- How to generate random number within a range in javascript
- How to get file extension using javascript
- How to get the current page URL with JavaScript
- How to get the first element of an array using javascript
- How to get unique values of an array using JavaScript
- How to get user screen size using javascript
- How to include one javascript file in another
- How to insert an item into an array at a specific index in JavaScript
- How to print hello world using javascript
- How to redirect to another page using javascript
- How to refresh page on specific time using javascript
- How to remove a property of JavaScript object
- How to remove a specific item from an array in javascript
- How to scroll to the top of the page using javascript
- How to set default argument values in JavaScript functions
- How to validate an email address Using JavaScript
- What is the reverse of the push function in javascript
- Write a JavaScript function to check if an input is an array
If this article was useful, share it! If you have any questions, leave a comment below.
Comments
Post a Comment