How to align checkboxes and their labels consistently cross-browsers

前端 未结 30 2421
有刺的猬
有刺的猬 2020-11-22 05:56

This is one of the minor CSS problems that plagues me constantly. How do folks around Stack Overflow vertically align checkboxes and

30条回答
  •  生来不讨喜
    2020-11-22 06:10

    Working off of One Crayon's solution, I have something that works for me and is simpler:

    .font2 {font-family:Arial; font-size:32px} /* Sample font */
    
    input[type=checkbox], input[type=radio] {
      vertical-align: middle;
      position: relative;
      bottom: 1px;
    }
    
    input[type=radio] { 
      bottom: 2px; 
    } 
    
    

    Renders pixel-for-pixel the same in Safari (whose baseline I trust) and both Firefox and IE7 check out as good. It also works for various label font sizes, big and small. Now, for fixing IE's baseline on selects and inputs...


    Update: (Third-Party Edit)

    The proper bottom position depends on font-family and font-size! I found using bottom: .08em; for checkbox & radio elements is a good general value. I tested it in Chrome/Firefox/IE11 in windows with Arial & Calibri fonts using several small/mid/large font-sizes.

    .font2, .font2 input {font-family:Arial; font-size:32px} /* Sample font */
    
    input[type=checkbox], input[type=radio] {
      vertical-align: middle; 
      position: relative;
      bottom: .08em; /* this is a better value for different fonts! */
    }
     
    
    

提交回复
热议问题