2018-10-10 18:03:03 -04:00
---
id: bad88fee1348ce8acef08815
2021-03-04 19:55:32 -07:00
title: 使用 Bootstrap Grid 并排放置元素
2018-10-10 18:03:03 -04:00
challengeType: 0
2020-09-07 16:17:39 +08:00
forumTopicId: 18371
2021-01-13 03:31:00 +01:00
dashedName: use-the-bootstrap-grid-to-put-elements-side-by-side
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-03-04 19:55:32 -07:00
Bootstrap 具有一套 12 列的响应式栅格系统,可以轻松的将多个元素放入一行并指定它们的相对宽度。 Bootstrap 的大部分 class 属性都可以应用在 `div` 元素上。
2020-12-16 00:37:30 -07:00
2021-03-04 19:55:32 -07:00
Bootstrap 的列宽取决于用户的屏幕宽度。 比如,手机有着窄屏幕而笔记本电脑有者更大的屏幕.
2018-10-10 18:03:03 -04:00
2021-03-04 19:55:32 -07:00
就拿 Bootstrap 的 `col-md-*` class 来说。 在这里, `md` 表示 medium (中等的), 而 `*` 是一个数字,说明了这个元素占有多少个列宽度。 这个例子就是指定了中等大小屏幕(例如笔记本电脑)下元素所占的列宽度。
2020-12-16 00:37:30 -07:00
2021-03-04 19:55:32 -07:00
在 Cat Photo App 中,将使用 `col-xs-*` , 其中 `xs` 是 extra small 的缩写 (比如窄屏手机屏幕), `*` 是填写的数字,代表一行中的元素该占多少列宽。
2020-12-16 00:37:30 -07:00
2021-03-04 19:55:32 -07:00
将 `Like` , `Info` 和 `Delete` 三个按钮并排放入一个 `<div class="row">` 元素中,然后每个按钮都各用一个 `<div class="col-xs-4">` 元素包裹起来。
2020-12-16 00:37:30 -07:00
2021-03-04 19:55:32 -07:00
当 `div` 元素设置了 `row` class 之后,那几个按钮便会嵌入其中了。
2020-12-16 00:37:30 -07:00
# --hints--
2021-03-04 19:55:32 -07:00
所有按钮都需要嵌入到同一个 `div` 元素中, 并且该元素包含 `row` class 属性。
2020-12-16 00:37:30 -07:00
```js
assert($('div.row:has(button)').length > 0);
```
2021-03-04 19:55:32 -07:00
每个 Bootstrap 按钮都需要嵌入各自的 `div` 元素,并且该元素包含 class 属性 `col-xs-4` 。
2020-12-16 00:37:30 -07:00
```js
assert($('div.col-xs-4:has(button)').length > 2);
2018-10-10 18:03:03 -04:00
```
2021-03-04 19:55:32 -07:00
确保每一个 `button` 元素都有一个闭合标签。
2020-12-16 00:37:30 -07:00
```js
assert(
code.match(/< \/button > /g) &&
code.match(/< button / g ) & &
code.match(/< \/button > /g).length === code.match(/< button / g ). length
);
2018-10-10 18:03:03 -04:00
```
2021-03-04 19:55:32 -07:00
确保每一个 `div` 元素都有一个闭合标签。
2020-12-16 00:37:30 -07:00
```js
assert(
code.match(/< \/div > /g) &&
code.match(/< div / g ) & &
code.match(/< \/div > /g).length === code.match(/< div / g ). length
);
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```html
< link href = "https://fonts.googleapis.com/css?family=Lobster" rel = "stylesheet" type = "text/css" >
< style >
.red-text {
color: red;
}
h2 {
font-family: Lobster, Monospace;
}
p {
font-size: 16px;
font-family: Monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
.smaller-image {
width: 100px;
}
< / style >
< div class = "container-fluid" >
< h2 class = "red-text text-center" > CatPhotoApp< / h2 >
< p > Click here for < a href = "#" > cat photos< / a > .< / p >
2021-09-22 08:34:59 -07:00
< a href = "#" > < img class = "smaller-image thick-green-border" src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt = "A cute orange cat lying on its back." > < / a >
2021-01-13 03:31:00 +01:00
2021-09-22 08:34:59 -07:00
< img src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/running-cats.jpg" class = "img-responsive" alt = "Three kittens running towards the camera." >
2021-01-13 03:31:00 +01:00
< button class = "btn btn-block btn-primary" > Like< / button >
< button class = "btn btn-block btn-info" > Info< / button >
< button class = "btn btn-block btn-danger" > Delete< / button >
< p > Things cats love:< / p >
< ul >
< li > cat nip< / li >
< li > laser pointers< / li >
< li > lasagna< / li >
< / ul >
< p > Top 3 things cats hate:< / p >
< ol >
< li > flea treatment< / li >
< li > thunder< / li >
< li > other cats< / li >
< / ol >
< form action = "https://freecatphotoapp.com/submit-cat-photo" >
< label > < input type = "radio" name = "indoor-outdoor" > Indoor< / label >
< label > < input type = "radio" name = "indoor-outdoor" > Outdoor< / label >
< label > < input type = "checkbox" name = "personality" > Loving< / label >
< label > < input type = "checkbox" name = "personality" > Lazy< / label >
< label > < input type = "checkbox" name = "personality" > Crazy< / label >
< input type = "text" placeholder = "cat photo URL" required >
< button type = "submit" > Submit< / button >
< / form >
< / div >
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```html
< link href = "https://fonts.googleapis.com/css?family=Lobster" rel = "stylesheet" type = "text/css" >
< style >
.red-text {
color: red;
}
h2 {
font-family: Lobster, Monospace;
}
p {
font-size: 16px;
font-family: Monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
.smaller-image {
width: 100px;
}
< / style >
< div class = "container-fluid" >
< h2 class = "red-text text-center" > CatPhotoApp< / h2 >
< p > Click here for < a href = "#" > cat photos< / a > .< / p >
2021-09-22 08:34:59 -07:00
< a href = "#" > < img class = "smaller-image thick-green-border" src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt = "A cute orange cat lying on its back." > < / a >
2021-01-13 03:31:00 +01:00
2021-09-22 08:34:59 -07:00
< img src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/running-cats.jpg" class = "img-responsive" alt = "Three kittens running towards the camera." >
2021-01-13 03:31:00 +01:00
< div class = "row" >
< div class = "col-xs-4" >
< button class = "btn btn-block btn-primary" > Like< / button >
< / div >
< div class = "col-xs-4" >
< button class = "btn btn-block btn-info" > Info< / button >
< / div >
< div class = "col-xs-4" >
< button class = "btn btn-block btn-danger" > Delete< / button >
< / div >
< / div >
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
< p > Things cats love:< / p >
< ul >
< li > cat nip< / li >
< li > laser pointers< / li >
< li > lasagna< / li >
< / ul >
< p > Top 3 things cats hate:< / p >
< ol >
< li > flea treatment< / li >
< li > thunder< / li >
< li > other cats< / li >
< / ol >
< form action = "https://freecatphotoapp.com/submit-cat-photo" >
< label > < input type = "radio" name = "indoor-outdoor" > Indoor< / label >
< label > < input type = "radio" name = "indoor-outdoor" > Outdoor< / label >
< label > < input type = "checkbox" name = "personality" > Loving< / label >
< label > < input type = "checkbox" name = "personality" > Lazy< / label >
< label > < input type = "checkbox" name = "personality" > Crazy< / label >
< input type = "text" placeholder = "cat photo URL" required >
< button type = "submit" > Submit< / button >
< / form >
< / div >
```