Home  >  Article  >  Web Front-end  >  How to align a checkbox and its label on various browsers using CSS?

How to align a checkbox and its label on various browsers using CSS?

PHPz
PHPzforward
2023-08-27 10:49:02879browse

How to align a checkbox and its label on various browsers using CSS?

Web forms are popularly used in modern websites. For webforms, we have a common element known as checkboxes. However, aligning these checkboxes and their labels in different browsers is a challenging task. This is because it may be displayed differently in different browsers and devices. When it comes to display of checkboxes, different browsers may have slightly different styles and rendering methods. To solve this problem, we will be discussing different ways to align the checkboxes along with their labels using CSS on cross-browsers.

How are check boxes displayed in different browsers?

Different browsers have different compatibility for checkboxes in web forms. In Internet Explorer, the appearance of checkboxes depends on the version. Older versions don't support the latest CSS features, making it difficult to align checkboxes and their labels. The same goes for versions of Mozilla Firefox and Safari.

Therefore, in order to ensure consistent display and correct alignment of checkboxes and labels, we must use cross-browser compatibility techniques in CSS.

Note− When creating web forms, it is generally a good practice to use the for attribute with any type of input element. This ensures that the checkbox and its label are aligned. Always ensure that the for attribute value of is the same as the id attribute value of .

We have several CSS techniques and practices to ensure proper alignment of checkboxes and labels on different platforms. Some of these are discussed below.

Use vertical-align to style checkboxes

Using the display and vertical-align attributes, we can align the checkbox and its label.

Example

Here, the "display: inline-block" attribute allows us to set the display type of the checkbox to inline-block. The "vertical-align: middle" property will vertically align the checkbox to the center of its container.

Using these two properties together will ensure that the checkbox appears on the same line as the other elements, aligned in the middle of the line. Therefore, the checkbox and its label will be aligned on the same line, leaving the label's text centered with the checkbox.

<html>
<head>
   <style>
      input[type="checkbox"] {
         display: inline-block;
         vertical-align: middle;
         cursor: pointer;
      }
   </style>
</head>
<body>
   <h2> Checkbox </h2>
   <div class="container">
      <label for="demo">
      <input type="checkbox" id="demo"> Option 1 </label>
      <br>
      <label for="demo">
      <input type="checkbox" id="demo"> Option 2 </label>
   </div>
</body>
</html>

Using CSS Flexbox

We can use the element as a flexible container to align check boxes and labels.

Example

In this example, we use the label element as a flex container by using display: flex. align-items: center The attribute centers the label's text with the checkbox.

Although we use flex: none in the input element to ensure that the width of the checkbox does not change as the label (container) size changes. At the same time, using these three properties allows the checkbox and label to be centered horizontally.

<html>
<head>
   <style>
      .container {
         display: flex;
         align-items: center;
         padding: 2px;
      }

      input[type=checkbox] {
         flex: none;
      }
   </style>
</head>
<body>
   <h2> Fruits </h2>
   <div>
      <label for="demo" class="container">
      <input type="checkbox" id="demo"> Mango </label>
      <br>
      <label for="demo" class="container">
      <input type="checkbox" id="demo"> Banana </label>
   </div>
</body>
</html>

Use vertical-align attribute

Checkboxes are aligned to the baseline of the label text by default in some modern browsers. However, to ensure their correct alignment, we can set the vertical-align attribute to "top" for both label and input elements.

Example

In the example below, we use the "display: inline-block" attribute to display label (class="container") and input elements as inline block elements. This makes both elements inline and their size resizable

“input[type="checkbox"]" is a selector which is used to select or match the checkbox type of input element.

Additionally, we used the "vertical-align: top" attribute to vertically align the element to the top of its container. Use these properties together to set the label and input elements so that they are both vertically aligned at the top of the container and appear inline relative to each other.

<html>
<head>
   <style>
      .container {
         display: inline-block;
         vertical-align: top;
         margin-right: 15px;
         letter-spacing: 1px;
      }
      input[type="checkbox"] {
         display: inline-block;
         vertical-align: top;
      }
   </style>
</head>
<body>
   <h2> Programming Languages </h2>
   <div>
      <label for="demo" class="container">
      <input type="checkbox" id="demo"> JavaScript </label>
      <br>
      <label for="demo" class="container">
      <input type="checkbox" id="demo"> Python </label>
   </div>
</body>
</html>

Use Position and Vertical-align properties

Keep the position attribute of the input element to relative, and use the vertical-align: bottom attribute to also align checkboxes and labels.

Example

Here, we make the label a block-level element so that it takes up the full width of the container. Removes padding and margin from the input element. Use the vertical-align: bottom property to align the checkbox vertically with the bottom of the container. Use the position property to align the checkbox with the label.

<html>
<head>
   <style>
      CSS code * {
         padding: 0;
         margin: 0;
      }
      .container {
         display: block;
         padding-left: 20px;
      }
      input {
         width: 17px;
         height: 17px;
         vertical-align: bottom;
         position: relative;
         top: -1px;
      }
   </style>
</head>
<body>
   <h2> Programming Languages </h2>
   <br>
   <div>
      <label for="demo" class="container">
      <input type="checkbox" id="demo"> JavaScript </label>
      <br>
      <label for="demo" class="container">
      <input type="checkbox" id="demo"> Python </label>
   </div>
</body>
</html>

in conclusion

Web forms are fundamental component of web development which are popularly used. To make it cross-browser compatible, we should make sure that alignment of input and label element should be proper. This makes your website look consistent and professional across all browsers and devices.

The above is the detailed content of How to align a checkbox and its label on various browsers using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete