【问题标题】:checkboxes central vertical alignment using angular, bootstrap使用角度,引导程序的复选框中心垂直对齐
【发布时间】:2026-01-31 06:15:01
【问题描述】:

我正在尝试使我的复选框居中对齐,当您添加新输入时,它们现在的显示方式如下: checkboxes not aligned well 。 这是我正在使用的代码(角度和引导程序):

<div  class="container text-center">
<form ng-submit="todoAdd()">
    <input type="text" ng-model="todoInput" size="50" placeholder="Add New">
    <input type="submit" class="btn btn-success" value="Add New">
</form>

<br>

<div ng-repeat="x in todoList">
    <input type="checkbox" ng-model="x.done"> <span ng-bind="x.todoText"></span>
</div>

<p><button ng-click="remove()">Remove marked</button></p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('todoCtrl', function($scope) {
    $scope.todoList = [{todoText:'Clean House', done:false}];

    $scope.todoAdd = function() {
        $scope.todoList.push({todoText:$scope.todoInput, done:false});
        $scope.todoInput = "";
    };

    $scope.remove = function() {
        var oldList = $scope.todoList;
        $scope.todoList = [];
        angular.forEach(oldList, function(x) {
            if (!x.done) $scope.todoList.push(x);
        });
    };
});
</script>

我想知道是否有任何方法可以垂直对齐框(在页面中心),也许使用引导程序或任何其他建议会有所帮助?这是我的第一个问题,所以如果表述不正确,我提前道歉,并提前感谢您的回答!

【问题讨论】:

    标签: angularjs twitter-bootstrap checkbox alignment


    【解决方案1】:

    这是一个纯 css 主题,你应该使用 flexbox:

    <div ng-repeat="x in todoList" style="display: flex; flex-direction: row; align-items: center">
    <input type="checkbox" ng-model="x.done"> <span style="flex: 1" ng-bind="x.todoText"></span>
    </div>
    

    或在课堂上:

    .flex-container {
     display: flex;
     flex-direction: row;
     align-items: center;
    }
    
    .flex-fill {
     flex: 1;
    }
    

    并将容器设置为重复 div,并在标签上设置填充类。 fyi: flex: 1 表示元素应该填满容器的剩余宽度。 (我希望上面的代码有正确的语法)

    在引导程序中应该是这样的:

    <div ng-repeat="x in todoList" class="form-group">
      <label class="control-label" ng-bind="x.todoText">
          <input class="form-control" type="checkbox" ng-model="x.done"> 
      </label>
    </div>
    

    但我不太确定这是否 100% 正确

    【讨论】: