如何在输入组div中左对齐复选框?

时间:2018-06-15 21:34:15

标签: twitter-bootstrap

我可以在表单组中左对齐一个复选框。但我想在复选框的左侧添加一些文本。所以我将复选框和前置文本放在输入组中。一旦复选框位于该输入组div内,复选框就不会保持对齐。

如何在输入组div中左对齐一个复选框?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>bootstrap alignment test</title>

  <link rel="stylesheet"
        href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">

  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" 
        rel="stylesheet" 
        integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" 
        crossorigin="anonymous">

  <script src="//code.jquery.com/jquery-1.12.4.js"></script>
  <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

  <style>
  .move-left {
    box-shadow: none;
    width: auto;
  }  

  </style>
</head>
<body>

<div id="app">

  <div class="container">
    <form>
      <div class="form-group">
        <label class="control-label col-sm-2" for="field3">Field 3:</label>
        <div class="col-sm-8">
          <input class="form-control move-left" type="checkbox" id="field3">
        </div>
      </div>

      <div class="form-group">

          <div class="input-group">
              <div class="input-group-prepend">
                <span class="input-group-text">Discontinued</span>
              </div>
              <input type="checkbox" class="form-control move-left" > 
            </div>
      </div>

    </form>
  </div>

</div>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" 
        integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" 
        crossorigin="anonymous"></script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果您希望附加的文本位于复选框的左侧,请将附加文本下方的复选框放在与input-group-append相同的div下。这是我能想到解决问题的最快方式。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>bootstrap alignment test</title>

  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">

  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

  <script src="//code.jquery.com/jquery-1.12.4.js"></script>
  <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

  <style>
    .move-left {
      box-shadow: none;
      width: auto;
    }
  </style>
</head>

<body>

  <div id="app">

    <div class="container">
      <form>
        <div class="form-group">
          <label class="control-label col-sm-2" for="field3">Field 3:</label>
          <div class="col-sm-8">
            <input class="form-control move-left" type="checkbox" id="field3">
          </div>
        </div>
        <!-- appended text is now on the left of the checkbox -->
        <div class="form-group">
          <div class="input-group">
            <div class="input-group-prepend">          
              <span class="input-group-text">Discontinued</span>
              <input type="checkbox" class="form-control move-left">
            </div>
          </div>
        </div>  
      </form>
    </div>

  </div>

  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
</body>

</html>

如果您坚持将复选框放在input-group-prepend div之外且在input-group div之下,则可以使用默认值flex覆盖form-control类的flex值,或者从中删除表单控件类你的div并使用你自己的css。

<style>
  .move-left {
    box-shadow: none;
    width: auto;
    flex: 0 1 auto !important;
    -ms-flex: 0 1 auto !important;
  }
</style>