CSS Button Groups

Please Share On:

Definition:

You can create horizontal and vertical button groups using CSS.

Horizontal Button Groups

To create a group of horizontal buttons, you have to remove margins and apply float: left;

Syntax:

.button { float: left;}

Source Code:

<!DOCTYPE html>
<html>
	<head>
		<style>
                   
		.button {
          background-color: green;
          padding: 20px 20px;
          color:white;
		  text-align: center;
		  text-decoration: none;
          float: left;
		}
          
		</style>
	</head>
	
	<body>
	<h2>CSS Horizontal Button Groups</h2>
      
    <button class="button btn1">btn 1</button>
    <button class="button btn2">btn 2</button>
    <button class="button btn3">btn 3</button>
    <button class="button btn4">btn 4</button>

	</body>
</html>
					

Output:


Vertical Button Groups

For vertical button group, apply display: block; instead of float: left; to display a group of buttons vertically down.

Syntax:

.button { display: block; }

Source Code:

<!DOCTYPE html>
<html>
    <head>
        <style>
                    
        .button {
          background-color: green;
          padding: 20px 20px;
          color:white;
          text-align: center;
          text-decoration: none;
          display: block;
        }
           
        </style>
    </head>
     
    <body>
    <h2>CSS Vertical Button Groups</h2>
       
    <button class="button btn1">btn 1</button>
    <button class="button btn2">btn 2</button>
    <button class="button btn3">btn 3</button>
    <button class="button btn4">btn 4</button>
 
    </body>
</html>
                  

Output:

CSS Vertical Button Groups

Try it yourself by changing float:left; and display:block; to see the differences.


Donate to support writers.


You may be interested in the following topics:

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright @2023. All Right Reserved.


Social media & sharing icons powered by UltimatelySocial