🎓 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
A button group is a set of buttons that are grouped together, often used for navigation or action controls on websites. It helps to organize buttons neatly in a row, making it easier for users to access multiple actions in one place. In this guide, you’ll learn how to create a button group using HTML and CSS.
Development Steps
- Create Basic HTML: Write the HTML structure for the button group.
- Style the Button Group with CSS: Use CSS to style the buttons and align them into a group.
- Customize the Buttons: Add custom styles like spacing, hover effects, and borders.
Step 1: Create Basic HTML
We’ll start by creating the HTML structure for the button group. The buttons will be inside a div container.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Group Example</title>
</head>
<body>
<!-- Step 1: Button group structure -->
<div class="button-group">
<button class="btn">Button 1</button>
<button class="btn">Button 2</button>
<button class="btn">Button 3</button>
</div>
</body>
</html>
Explanation:
- The
<div class="button-group">contains the buttons, keeping them grouped together. - Each
<button>element has a classbtnthat we’ll style with CSS.
Step 2: Style the Button Group with CSS
Now, we’ll style the buttons to look like a unified group. We will remove the border between buttons to make them appear connected and apply basic styles.
/* Step 2: Style for button group */
.button-group {
display: flex; /* Align buttons in a row */
}
.btn {
padding: 10px 20px; /* Add padding inside buttons */
background-color: #3498db; /* Blue background */
color: white; /* White text color */
border: 1px solid #3498db; /* Border matching the background */
cursor: pointer;
font-size: 16px; /* Set font size */
border-radius: 0; /* Remove border-radius for connected look */
transition: background-color 0.3s ease; /* Smooth transition on hover */
}
.btn + .btn {
border-left: none; /* Remove left border between buttons */
}
Explanation:
display: flex;: Aligns the buttons in a row.border-left: none;: Removes the border between buttons so they appear connected.border-radius: 0;: Removes rounded corners, making the buttons look like they belong to the same group.background-color: #3498db;: Gives the buttons a blue background.
Step 3: Customize the Buttons
We’ll enhance the button group by adding a hover effect and adjusting the spacing between the buttons.
/* Step 3: Hover effect for buttons */
.btn:hover {
background-color: #2980b9; /* Darker blue on hover */
}
/* Add rounded corners for the first and last button */
.button-group .btn:first-child {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
.button-group .btn:last-child {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}
Explanation:
:hover: Changes the background color to a darker blue when the user hovers over a button.border-top-left-radiusandborder-top-right-radius: Adds rounded corners to the first and last buttons to give the group a smooth look.
Complete Code Example
Here’s the full HTML and CSS code to create a button group with a modern style:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Group Example</title>
<style>
/* Style for button group */
.button-group {
display: flex; /* Align buttons in a row */
}
.btn {
padding: 10px 20px; /* Add padding inside buttons */
background-color: #3498db; /* Blue background */
color: white; /* White text color */
border: 1px solid #3498db; /* Border matching the background */
cursor: pointer;
font-size: 16px; /* Set font size */
border-radius: 0; /* Remove border-radius for connected look */
transition: background-color 0.3s ease; /* Smooth transition on hover */
}
.btn + .btn {
border-left: none; /* Remove left border between buttons */
}
/* Hover effect for buttons */
.btn:hover {
background-color: #2980b9; /* Darker blue on hover */
}
/* Add rounded corners for the first and last button */
.button-group .btn:first-child {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
.button-group .btn:last-child {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}
</style>
</head>
<body>
<div class="button-group">
<button class="btn">Button 1</button>
<button class="btn">Button 2</button>
<button class="btn">Button 3</button>
</div>
</body>
</html>
Output
Conclusion
In this guide, you learned how to create a button group using HTML and CSS. The buttons are neatly aligned in a row and appear connected with no borders between them. You can further customize the button group by adjusting the styles, adding more buttons, or changing colors to suit your design needs.
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