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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Mozila 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Mozila 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Mozila Firefox:
  • Google Chrome:
  • Internet Explorer:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.