🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Adding an underline effect to button text when hovered is a simple yet effective way to enhance user interaction. This visual feedback informs users that the button is clickable. The :hover pseudo-class in CSS allows you to apply styles like underlining the button text when the user hovers over it.
In this tutorial, you'll learn how to add an underline to button text on hover using CSS.
Problem Statement
Create CSS code that:
- Styles a button.
- Adds an underline effect to the button text when hovered.
Example:
- Input: A button with the text "Hover Me".
- Output: A button that underlines its text when hovered.
Solution Steps
- Use the
<button>Element: Create a button using HTML. - Style the Button: Define the appearance of the button using CSS.
- Add Hover Underline Effect: Apply the
text-decoration: underlineproperty on hover.
HTML Structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Underline on Hover Button</title>
<style>
/* Step 1: Style the button */
.underline-button {
font-size: 1.5rem;
color: white;
background-color: #3498db;
padding: 12px 30px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s ease;
text-decoration: none; /* Remove underline initially */
}
/* Step 2: Add underline effect on hover */
.underline-button:hover {
text-decoration: underline; /* Underline text on hover */
}
</style>
</head>
<body>
<div style="text-align: center; margin-top: 100px;">
<button class="underline-button">Hover Me</button>
</div>
</body>
</html>
Output
Explanation
Step 1: Style the Button
The .underline-button class is used to style the button with basic properties like font size, color, background color, and padding.
.underline-button {
font-size: 1.5rem;
color: white;
background-color: #3498db; /* Blue background */
padding: 12px 30px; /* Padding inside the button */
border: none;
border-radius: 5px; /* Rounded corners */
cursor: pointer; /* Pointer cursor */
transition: all 0.3s ease; /* Smooth transition effect */
text-decoration: none; /* Ensure there is no underline by default */
}
text-decoration: none: Ensures the text has no underline by default.transition: all 0.3s ease: Ensures a smooth transition when the hover effect is applied.
Step 2: Add Hover Underline Effect
The :hover pseudo-class applies an underline to the button's text when the user hovers over it.
.underline-button:hover {
text-decoration: underline; /* Underline the text when hovered */
}
text-decoration: underline: Adds the underline to the button text when it is hovered.
Customization
Change Text Color on Hover
You can also change the text color on hover along with the underline:
.underline-button:hover {
text-decoration: underline;
color: yellow; /* Change text color on hover */
}
Change the Button Background on the Hover
To make the button even more interactive, you can change the background color when hovered:
.underline-button:hover {
text-decoration: underline;
background-color: #2980b9; /* Darker blue background on hover */
}
Conclusion
You can add an underline effect to button text on hover using the text-decoration: underline property in combination with the :hover pseudo-class. This simple effect improves interactivity and user feedback, making the button feel more engaging.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment