how to align checkbox with Label using HTML CSS (Should work in all browser)?


How can I simply align my checkbox with Label text using HTML CSS, so that it works in all browsers consistently?

I am facing this issue in some browsers, if it works in chrome, it doesn't work in IE or if it works properly in Firefox it doesn't align properly in chrome.

So want to align Checkbox with Label text properly which can work in all browsers equally? Reference code

<div>
    <label>
     <input type="checkbox" /> Checkbox Label
    </label>
</div>

thanks


Asked by:- jaiprakash
2
: 14054 At:- 10/7/2017 7:53:48 AM
html css alignment checkbox-align-with-label







2 Answers
profileImage Answered by:- Sam

 If you want to go with above HTML code only, i.e. means html would be without wrapping label text, your HTML /CSS code to align checkbox with Label properly can be

input[type=checkbox], input[type=radio] {
  vertical-align: middle;
  position: relative;
  bottom: 1px;
}

input[type=radio] {
  bottom: 2px;
}
<label>
  <input type="checkbox">
  Label text
</label>

Fiddle Example here

Another simple Inline CSS code would be

<input type="checkbox" />
<label style="font-size: 16px;position: relative;top: -2px;">This is the label</label>
3
At:- 10/7/2017 11:29:35 AM


profileImage Answered by:- vikas_jk

Using bottom property and veritcal align in CSS, we can align checkbox

.font2, .font2 input {font-family:Arial; font-size:32px} /* sample large fonts */

input[type=checkbox], input[type=radio] {
  vertical-align: middle; 
  position: relative;
  bottom: .08em; /* this is helpful */
}

and considering HTML

<form>
  <label>
    <input type="radio" checked>
    I am an aligned radio and label
  </label>
  <label>
      <input type="checkbox" checked>
    I am an aligned checkbox and label
  </label>
</form><label><input type="checkbox" /> Simple text</label> 

<p class="font2">
  <label><input type="checkbox"/> Large Label text</label>
</p>

You can check fiddle here: https://jsfiddle.net/zdwjr068/

This should also work, thanks

0
At:- 10/18/2022 2:31:12 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use