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

21 Dec, 2020
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:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
arorakashish0911
CSS-Misc
HTML-Misc
Picked
CSS
HTML
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to create footer to stay at the bottom of a Web page?
Types of CSS (Cascading Style Sheet)
How to position a div at the bottom of its container using CSS?
Create a Responsive Navbar using ReactJS
Design a web page using HTML and CSS
How to set the default value for an HTML <select> element ?
How to set input type date in dd-mm-yyyy format using HTML ?
Hide or show elements in HTML using display property
How to Insert Form Data into Database using PHP ?
REST API (Introduction)