【发布时间】:2023-03-26 22:25:01
【问题描述】:
我正在使用 AngularJS ng-repeat 呈现产品列表,并使用引导程序 col-md-2 使其每行显示 6 个产品。我粘贴了我的代码的简短版本,如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of Bootstrap 3 Grid System</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap- theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"> </script>
<style type="text/css">
p{
padding: 50px;
font-size: 32px;
font-weight: bold;
text-align: center;
background: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-2" ng-repeat="item in items">
<img src="{{itme.imgURL}}" />
<p>{{item.title}}</p>
<div>
<a href="{{item.productURL}}" />
</div>
</div>
</div>
</div>
</body>
但是,某些产品的标题太长,会显示为两行或多行。在这种情况下,显示的元素没有很好地对齐。要查看对齐问题,请尝试以下代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of Bootstrap 3 Grid System</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap- theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"> </script>
<style type="text/css">
p{
padding: 50px;
font-size: 32px;
font-weight: bold;
text-align: center;
background: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-2"><p>Box 1 test</p></div>
<div class="col-md-2"><p>Box 2</p></div>
<div class="col-md-2"><p>Box 3</p></div>
<div class="col-md-2"><p>Box 4</p></div>
<div class="col-md-2"><p>Box 5</p></div>
<div class="col-md-2"><p>Box 6</p></div>
<div class="col-md-2"><p>Box 7</p></div>
<div class="col-md-2"><p>Box 8</p></div>
<div class="col-md-2"><p>Box 9</p></div>
<div class="col-md-2"><p>Box 10</p></div>
<div class="col-md-2"><p>Box 11</p></div>
<div class="col-md-2"><p>Box 12</p></div>
</div>
</div>
</body>
</html>
Box 7 显示在 Box 2 下,而不是 Box 1 下。
我知道我们可以在 Box 7 之前添加 clearfix div,或者我们可以将元素 7 - 12 放在新的引导行中。但问题是我使用了 ng-repeat,所以我无法控制每个元素。
那么有人可以给我一些关于如何使所有元素对齐的建议吗?
非常感谢!
【问题讨论】:
-
如何将 clearfix 放在重复中,但将其包装在
ng-if中?您可以使用重复的索引仅在正确的点包含 clearfix。
标签: javascript html css angularjs twitter-bootstrap