Skip to content Skip to sidebar Skip to footer

How Can I Make Multi-line, Vertically And Horizontally Aligned Labels For Radio Buttons In Html Forms With Css?

Assuming the following markup:
Radio Buttons

Solution 1:

I believe this does it all. You didn't mention that it has to validate, however, so I used the inline-block (-moz-inline-box) display. One of my favorites, actually.

Here's a working copy

Tested in Safari 3, FireFox 3, and IE7.

<styletype="text/css">ol{
    padding-left: 0;
    margin-left:0;
}

ol>li {
    list-style-type: none;
    margin-bottom: .5em;
}

ol>liinput[type=radio] {
    display: -moz-inline-box;
    display: inline-block;
    vertical-align: middle;
}

ol>lilabel {
    display: -moz-inline-box;
    display: inline-block;
    vertical-align: middle;
}
</style>

Solution 2:

Using the following markup and css I was able to produce multi-line labels that do not wrap under the radio button:

<styletype="text/css">fieldsetinput, label {
      float: left;
      display: block;
    }

    fieldsetli {
      clear: both;
    }
</style><fieldset><ol><li><inputtype="radio"id="x" /><labelfor="x">
        stuff<br/>
        stuff1
      </label></li><li><inputtype="radio"id="x" /><labelfor="x">
        stuff<br/>
        stuff1
      </label></li></ol></fieldset>

however I was unable to use:

fieldsetlabel {
  vertical-align: middle;
}

to center the label vertically on the radio button, even when applying a width (both suggestions in Dmitri Farkov's answer. My main purpose was to prevent wrapping under the radio button, so this solution will be fine for the time being.

Solution 3:

Since I asked how to handle really long labels above, and I finally solved it myself. Here is the solution to my problem. Maybe it could help you to?

<styletype="text/css">#master_frame { 
      background: #BBB;
      height: 300px;
      width: 300px;
  } 
  fieldset.radios { 
      border: none;
  } 
  fieldset fields { 
      clear: both;
  } 
  input { 
      float: left;
      display: block;
  } 
  label { 
      position: relative;
      margin-left: 30px;
      display: block;
  } 
</style><divid="master_frame"><fieldsetclass='radios'><divclass='field'><inputtype="radio"id="a" /><labelfor="a">Short</label></div><divclass='field'><inputtype="radio"id="b" /><labelfor="b">
        A really long and massive text that does not fit on one row!
      </label></div></fieldset></div>

Solution 4:

Make input and label both

float: left;
display: block;

Set width's for the label and input.


apply

clear: both;
 vertical-align: middle;

to all the li's.

Solution 5:

You should use white-space: normal; in label for multiline

Post a Comment for "How Can I Make Multi-line, Vertically And Horizontally Aligned Labels For Radio Buttons In Html Forms With Css?"