Open In App

How to align checkboxes and their labels on cross-browsers using CSS ?

Last Updated : 21 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

For aligning the checkboxes or radio buttons with their labels can be achieved in many ways. Some of the simplest methods to achieve this are described below with proper code and output in different browsers. Now styling can be done in various ways to align the checkboxes and their labels. For this article, we are using internal stylesheet which is done under the style tag.

Method 1: By making the position of checkbox relative, set the vertical-align to the middle can align the checkboxes and their labels. Here, we have made the position of the checkbox relative to the label. So the checkboxes are aligned according to the label.

Example:




<!DOCTYPE html>
<html>
  
<head>
    <title>
        Aligning Checkboxes consistently
        in cross browsers
    </title>
    <style>
        h1 {
            color: green;
        }
          
        input[type=checkbox] {
            vertical-align: middle;
            position: relative;
            bottom: 1px;
        }
          
        label {
            display: block;
        }
    </style>
</head>
  
<body>
    <h1>Geeksforgeeks</h1>
    <form>
        <label>
            <input type="checkbox"
          A Computer Science Portal for Geeks
        </label>
  
        <label>
            <input type="checkbox"
          Just an Edutech Company to educate
        </label>
    </form>
  
</body>
  
</html>


Output:

  • Mozilla Firefox:
  • Google Chrome:
  • Internet Explorer:

Method 2: By using the flex display, as the display is set to flex and alignment is center so both the checkbox and label are aligned to the center of the cross-axis.

Example:




<!DOCTYPE html>
<html>
  
<head>
    <title>
        Aligning Checkboxes consistently
        in cross browsers
    </title>
    <style>
        h1 {
            color: green;
        }
          
        label {
            display: flex;
            align-items: center;
        }
        input[type=checkbox]{
            flex: none;
        }
    </style>
</head>
  
<body>
    <h1>Geeksforgeeks</h1>
    <form>
        <label>
            <input type="checkbox"
          A Computer Science Portal for Geeks
        </label>
  
        <label>
            <input type="checkbox"
          Just an Edutech Company to educate
        </label>
    </form>
  
</body>
  
</html>


Output:

  • Mozilla Firefox:
  • Google Chrome:
  • Internet Explorer:

Method 3: By grouping the label and checkbox into the same block, we can align the checkbox and label consistently in cross-browsers.
Example:




<!DOCTYPE html>
<html>
  
<head>
    <title>
        Aligning Checkboxes consistently
        in cross browsers
    </title>
</head>
  
<body>
    <h1 style="color: green;">Geeksforgeeks</h1>
    <form>
    <div>
        <label style="display: inline-block">
            <input style="vertical-align: middle"
                        type="checkbox" />
            <span style="vertical-align: middle">
                A Computer Science Portal for Geeks
            </span>
        </label>
    </div>
    <div>
        <label style="display: inline-block">
            <input style="vertical-align: middle"
                        type="checkbox" />
            <span style="vertical-align: middle">
                Just an Edutech Company to educate
            </span>
        </label>
    </div>
    </form>
</body>
  
</html>


Output:

  • Mozilla Firefox:
  • Google Chrome:
  • Internet Explorer:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads