Lining up labels with radio buttons in bootstrap

后端 未结 7 816
忘掉有多难
忘掉有多难 2020-12-23 19:07

I have a Bootstrap form with some inline radio buttons and a label. I\'d like to keep the label on the same line as the buttons, but I can\'t seem to make that happen. Here\

相关标签:
7条回答
  • 2020-12-23 19:28

    This may work for you, Please try this.

    <form>
      <div class="form-inline">
        <div class="controls-row">
          <label class="control-label">Some label</label>
          <label class="radio inline">
            <input type="radio" value="1" />First
          </label>
          <label class="radio inline">
            <input type="radio" value="2" />Second
          </label>
        </div>
      </div>
    </form>
    
    0 讨论(0)
  • 2020-12-23 19:28

    Key insights for me were: - ensure that label content comes after the input-radio field - I tweaked my css to make everything a little closer

    .radio-inline+.radio-inline {
        margin-left: 5px;
    }
    
    0 讨论(0)
  • 2020-12-23 19:34

    This is all nicely lined up including the field label. Lining up the field label was the tricky part.

    HTML Code:

    <div class="form-group">
        <label class="control-label col-md-5">Create a</label>
        <div class="col-md-7">
            <label class="radio-inline control-label">
                <input checked="checked" id="TaskLog_TaskTypeId" name="TaskLog.TaskTypeId" type="radio" value="2"> Task
            </label>
            <label class="radio-inline control-label">
                <input id="TaskLog_TaskTypeId" name="TaskLog.TaskTypeId" type="radio" value="1"> Note
            </label>
        </div>
    </div>
    

    CSHTML / Razor Code:

    <div class="form-group">
        @Html.Label("Create a", htmlAttributes: new { @class = "control-label col-md-5" })
        <div class="col-md-7">
            <label class="radio-inline control-label">
                @Html.RadioButtonFor(model => model.TaskTypeId, Model.TaskTaskTypeId) Task
            </label>
            <label class="radio-inline control-label">
                @Html.RadioButtonFor(model => model.TaskTypeId, Model.NoteTaskTypeId) Note
            </label>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-23 19:38

    Since Bootstrap 3 you have to use checkbox-inline and radio-inline classes on the label.

    This takes care of vertical alignment.

    <label class="checkbox-inline">
        <input type="checkbox" id="inlineCheckbox1" value="option1"> 1
    </label>
    <label class="radio-inline">
        <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> 1
    </label>
    
    0 讨论(0)
  • 2020-12-23 19:41

    If you add the 'radio inline' class to the control label in the solution provided by user1938475 it should line up correctly with the other labels. Or if you're only using 'radio' like your 2nd example just include the 'radio' class.

    <label class="radio control-label">Some label</label>
    

    OR for 'radio inline'

    <label class="radio-inline control-label">Some label</label>
    
    0 讨论(0)
  • 2020-12-23 19:51

    In Bootstrap 4 you can use the form-check-inline class.

    <div class="form-check form-check-inline">
      <input class="form-check-input" type="radio" name="queryFieldName" id="option1" value="1">
      <label class="form-check-label" for="option1">First</label>
    </div>
    <div class="form-check form-check-inline">
      <input class="form-check-input" type="radio" name="queryFieldName" id="option2" value="2">
      <label class="form-check-label" for="option2">Second</label>
    </div>
    
    0 讨论(0)
提交回复
热议问题