Radio/checkbox alignment in HTML/CSS

前端 未结 13 944
不知归路
不知归路 2020-12-04 08:08

What is the cleanest way to align properly radio buttons / checkboxes with text? The only reliable solution which I have been using so far is table based:

&l         


        
相关标签:
13条回答
  • 2020-12-04 08:53

    Below I will insert a checkbox dynamically. Style is included to align the checkbox and most important to make sure word wrap is straight. the most important thing here is display: table-cell; for the alignment

    The visual basic code.

    'the code to dynamically insert a checkbox

    Dim tbl As Table = New Table()
    Dim tc1 As TableCell = New TableCell()
    tc1.CssClass = "tdCheckTablecell"
    
    'get the data for this checkbox
    Dim ds As DataSet
    Dim Company As ina.VullenCheckbox
    Company = New ina.VullenCheckbox
    Company.IDVeldenperScherm = HETid
    Company.IDLoginBedrijf = HttpContext.Current.Session("welkbedrijf")
    ds = Company.GetsDataVullenCheckbox("K_GetS_VullenCheckboxMasterDDLOmschrijvingVC") 'ds6
    

    'create the checkbox

    Dim radio As CheckBoxList = New CheckBoxList
    radio.DataSource = ds
    radio.ID = HETid
    radio.CssClass = "tdCheck"
    radio.DataTextField = "OmschrijvingVC"
    radio.DataValueField = "IDVullenCheckbox"
    radio.Attributes.Add("onclick", "documentChanged();")
    radio.DataBind()
    

    'connect the checkbox

    tc1.Controls.Add(radio)
    tr.Cells.Add(tc1)
    tbl.Rows.Add(tr)
    

    'the style for the checkbox

    input[type="checkbox"] {float: left;   width: 5%; height:20px; border: 1px solid black; }
    
    .tdCheck label {     width: 90%;display: table-cell;    align:right;}
    
    .tdCheck {width:100%;}
    

    and the HTML output

    <head id="HEAD1">
        <title>
            name
        </title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR" /><meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE" />
    </head>
    <style type='text/css'>
    input[type="checkbox"] {float: left;   width: 20px; height:20px;  }
    .tdCheck label {     width: 90%;display: table-cell;    align:right;}
    .tdCheck {width:100%;}
    .tdLabel {width:100px;}
    .tdCheckTableCell {width:400px;}
    TABLE
    {
    
    vertical-align:top;
    border:1;border-style:solid;margin:0;padding:0;border-spacing:0;
    border-color:red;
    }
    TD
    {
    vertical-align:top; /*labels ed en de items in het datagrid*/
    border: 1;  border-style:solid;
    border-color:green;
        font-size:30px  }
    </style>
    <body id="bodyInternet"  > 
        <form name="Form2" method="post" action="main.aspx?B" id="Form2">
            <table border="0">
                <tr>
                    <td class="tdLabel">
                        <span id="ctl16_ID{A}" class="DynamicLabel">
                            TITLE
                        </span>
                    </td>
                    <td class="tdCheckTablecell">
                        <table id="ctl16_{A}" class="tdCheck" onclick="documentChanged();" border="0">
                            <tr>
                                <td>
                                    <input id="ctl16_{A}_0" type="checkbox" name="ctl16${A}$0" />
                                    <label for="ctl16_{A}_0">
                                        this is just dummy text to show the text will warp this is just dummy text to show the text will warp this is just dummy text to show the text will warp this is just dummy text to show the text will warp this is just dummy text to show the text will warp this is just dummy text to show the text will warp  
                                    </label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input id="ctl16_{A}_1" type="checkbox" name="ctl16${A}$1" />
                                    <label for="ctl16_{A}_1">
                                        ITEM2
                                    </label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input id="ctl16_{A}_2" type="checkbox" name="ctl16${A}$2" />
                                    <label for="ctl16_{A}_2">
                                        ITEM3
                                    </label>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
    </form>
    </body>
    

    0 讨论(0)
  • 2020-12-04 08:54

    I wouldn't use tables for this at all. CSS can easily do this.

    I would do something like this:

       <p class="clearfix">
          <input id="option1" type="radio" name="opt" />
          <label for="option1">Option 1</label>
       </p>
    
    p { margin: 0px 0px 10px 0px; }
    input { float: left; width: 50px; }
    label { margin: 0px 0px 0px 10px; float: left; }
    

    Note: I have used the clearfix class from : http://www.positioniseverything.net/easyclearing.html

    .clearfix:after {
        content: ".";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
    }
    
    .clearfix {display: inline-block;}
    
    /* Hides from IE-mac \*/
    * html .clearfix {height: 1%;}
    .clearfix {display: block;}
    /* End hide from IE-mac */
    
    0 讨论(0)
  • 2020-12-04 08:54

    If your label is long and goes on multiple rows setting the width and display:inline-block will help.

    .form-field * {
      vertical-align: middle;
    }
    .form-field input {
      clear:left;
    }
    .form-field label { 
      width:200px;
      display:inline-block;
    }
    
    <div class="form-field">
        <input id="option1" type="radio" name="opt" value="1"/>
        <label for="option1">Option 1 is very long and is likely to go on two lines.</label>
        <input id="option2" type="radio" name="opt" value="2"/>
        <label for="option2">Option 2 might fit into one line.</label>
    </div>
    
    0 讨论(0)
  • 2020-12-04 08:57

    There are several ways to implement it:

    1. For ASP.NET Standard CheckBox:

      .tdInputCheckBox
      { 
      position:relative;
      top:-2px;
      }
      <table>
              <tr>
                  <td class="tdInputCheckBox">                  
                      <asp:CheckBox ID="chkMale" runat="server" Text="Male" />
                      <asp:CheckBox ID="chkFemale" runat="server" Text="Female" />
                  </td>
              </tr>
      </table>
      
    2. For DevExpress CheckBox:

      <dx:ASPxCheckBox ID="chkAccept" runat="server" Text="Yes" Layout="Flow"/>
      <dx:ASPxCheckBox ID="chkAccept" runat="server" Text="No" Layout="Flow"/>
      
    3. For RadioButtonList:

      <asp:RadioButtonList ID="rdoAccept" runat="server" RepeatDirection="Horizontal">
         <asp:ListItem>Yes</asp:ListItem>
         <asp:ListItem>No</asp:ListItem>
      </asp:RadioButtonList>
      
    4. For Required Field Validators:

      <asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator ID="reqEmailId" runat="server" ErrorMessage="Email id is required." Display="Dynamic" ControlToValidate="txtEmailId"></asp:RequiredFieldValidator>
      <asp:RegularExpressionValidator ID="regexEmailId" runat="server" ErrorMessage="Invalid Email Id." ControlToValidate="txtEmailId" Text="*"></asp:RegularExpressionValidator>`
      
    0 讨论(0)
  • 2020-12-04 08:58

    The following works in Firefox and Opera (sorry, I do not have access to other browsers at the moment):

    <div class="form-field">
        <input id="option1" type="radio" name="opt"/>
        <label for="option1">Option 1</label>
    </div>
    

    The CSS:

    .form-field * {
        vertical-align: middle;
    }
    
    0 讨论(0)
  • 2020-12-04 09:00

    This is a simple solution which solved the problem for me:

    label 
    {
    
    /* for firefox */
    vertical-align:middle; 
    
    /*for internet explorer */
    *bottom:3px;
    *position:relative; 
    
    padding-bottom:7px; 
    
    }
    
    0 讨论(0)
提交回复
热议问题