How to create a "button group" with CSS?


On a web page, we can easily create a button group. We will create a single div for all the buttons in a button group. They will be set with display inline-block. Also, the hover effect will be set.

Create a Button Group on a web Page

In a div, set the buttons using the <button> element −

<div class="btnGroup">
   <button>Facebook</button>
   <button>Twitter</button>
   <button>LinkedIn</button>
</div>

Style the Button Groups

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 i.e. the buttons. The shadow for the buttons is set using the box-shadow property −

.btnGroup{
   display: inline-block;
   font-size: 0;
   border: 2px solid darkgreen;
   box-shadow: 5px 10px 18px rgb(55, 173, 39);
}

Style the Buttons

The individual buttons are aligned to the left using the float property −

button{
   float: left;
   margin:0px;
   border:none;
   padding: 15px;
   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
   font-weight: bold;
   font-size: 20px;
}

On Hover

The :hover selector is used to set the styles for the buttons when they are individually hovered −

button:hover{
   background:rgb(48, 24, 134);
   box-shadow: 5px 10px 18px rgb(41, 39, 173);
   color:white;
}

Example

The following is the code to create a button group using CSS −

<!DOCTYPE html>
<html>
<head>
   <style>
      .btnGroup{
         display: inline-block;
         font-size: 0;
         border: 2px solid darkgreen;
         box-shadow: 5px 10px 18px rgb(55, 173, 39);
      }
      button{
         float: left;
         margin:0px;
         border:none;
         padding: 15px;
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
         font-weight: bold;
         font-size: 20px;
      }
      button:not(:last-child){
         border-right: 2px solid rgb(6, 134, 49);
      }
      button:hover{
         background:rgb(48, 24, 134);
         box-shadow: 5px 10px 18px rgb(41, 39, 173);
         color:white;
      }
   </style>
</head>	
<body>
   <h1>Button Group</h1>
   <div class="btnGroup">
      <button>Facebook</button>
      <button>Twitter</button>
      <button>LinkedIn</button>
   </div>
   <p>Hover over the above button group to see hover effects</p>
</body>
</html>

Updated on: 17-Nov-2023

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements