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



我可以在表单组中左对齐复选框。 但是我想在复选框的左侧添加一些文本。因此,我将复选框和前置文本放在输入组中。一旦复选框位于该输入组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>

如果您希望附加的文本保留在复选框的左侧,请将复选框放在附加文本下方,在具有输入组附加的同一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>

如果您坚持将复选框放在输入组前置div 外部和输入组div 下,则可以使用默认值 flex 覆盖表单控件类的 flex 值,或者从div 中删除表单控件类并改用您自己的 css。

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

最新更新