我用bootstrap编写了一个HTML代码。我想在桌面视图一行4列,3列在平板电脑视图和2列在平板电脑,1在移动设备。我读了其他的堆栈溢出问题,但我没有得到答案/他们没有任何描述。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title> پویان افتخاری - بوت استرپ</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous" />
</head>
<body>
<div class="row">
<div class="col-md-3 col-sm-6 col-xs-12">box 1</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 2</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 3</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 4</div>
</div>
<div class="row">
<div class="col-md-3 col-sm-6 col-xs-12">box 5</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 6</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 7</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 8</div>
</div>
<div class="row">
<div class="col-md-3 col-sm-6 col-xs-12">box 9</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 10</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 11</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 12</div>
</div>
<div class="row">
<div class="col-md-3 col-sm-6 col-xs-12">box 13</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 14</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 15</div>
<div class="col-md-3 col-sm-6 col-xs-12">box 16</div>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
</html>
默认情况下有12列。通常你需要的是,每一行1 2 3或4列。所以一切分裂。
类col-something-number
预定义为宽度的16.66%或50%,具体取决于屏幕宽度和数量。所以<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12">
意味着:
12/3 = 4 in a row when screen is xl
12/4 = 3 in a row when screen is lg
12/6 = 2 in a row when screen is md
12/12= 1 in a row when screen is sm
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous" />
<div class="container">
<div class="row">
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12">box 1</div>
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12">box 2</div>
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12">box 3</div>
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12">box 4</div>
</div>
</div>