197 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			197 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| ---
 | ||
| id: bad88fee1348ce8acef08815
 | ||
| title: 使用 Bootstrap Grid 并排放置元素
 | ||
| challengeType: 0
 | ||
| forumTopicId: 18371
 | ||
| dashedName: use-the-bootstrap-grid-to-put-elements-side-by-side
 | ||
| ---
 | ||
| 
 | ||
| # --description--
 | ||
| 
 | ||
| Bootstrap 具有一套 12 列的响应式栅格系统,可以轻松的将多个元素放入一行并指定它们的相对宽度。 Bootstrap 的大部分 class 属性都可以应用在 `div` 元素上。
 | ||
| 
 | ||
| Bootstrap 的列宽取决于用户的屏幕宽度。 比如,手机有着窄屏幕而笔记本电脑有者更大的屏幕.
 | ||
| 
 | ||
| 就拿 Bootstrap 的 `col-md-*` class 来说。 在这里, `md` 表示 medium (中等的), 而 `*` 是一个数字,说明了这个元素占有多少个列宽度。 这个例子就是指定了中等大小屏幕(例如笔记本电脑)下元素所占的列宽度。
 | ||
| 
 | ||
| 在 Cat Photo App 中,将使用 `col-xs-*` , 其中 `xs` 是 extra small 的缩写 (比如窄屏手机屏幕), `*` 是填写的数字,代表一行中的元素该占多少列宽。
 | ||
| 
 | ||
| 将 `Like`,`Info` 和 `Delete` 三个按钮并排放入一个 `<div class="row">` 元素中,然后每个按钮都各用一个 `<div class="col-xs-4">` 元素包裹起来。
 | ||
| 
 | ||
| 当 `div` 元素设置了 `row` class 之后,那几个按钮便会嵌入其中了。
 | ||
| 
 | ||
| # --hints--
 | ||
| 
 | ||
| 所有按钮都需要嵌入到同一个 `div` 元素中, 并且该元素包含 `row` class 属性。
 | ||
| 
 | ||
| ```js
 | ||
| assert($('div.row:has(button)').length > 0);
 | ||
| ```
 | ||
| 
 | ||
| 每个 Bootstrap 按钮都需要嵌入各自的 `div` 元素,并且该元素包含 class 属性 `col-xs-4`。
 | ||
| 
 | ||
| ```js
 | ||
| assert($('div.col-xs-4:has(button)').length > 2);
 | ||
| ```
 | ||
| 
 | ||
| 确保每一个 `button` 元素都有一个闭合标签。
 | ||
| 
 | ||
| ```js
 | ||
| assert(
 | ||
|   code.match(/<\/button>/g) &&
 | ||
|     code.match(/<button/g) &&
 | ||
|     code.match(/<\/button>/g).length === code.match(/<button/g).length
 | ||
| );
 | ||
| ```
 | ||
| 
 | ||
| 确保每一个 `div` 元素都有一个闭合标签。
 | ||
| 
 | ||
| ```js
 | ||
| assert(
 | ||
|   code.match(/<\/div>/g) &&
 | ||
|     code.match(/<div/g) &&
 | ||
|     code.match(/<\/div>/g).length === code.match(/<div/g).length
 | ||
| );
 | ||
| ```
 | ||
| 
 | ||
| # --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>
 | ||
| 
 | ||
|   <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>
 | ||
| 
 | ||
|   <img src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/running-cats.jpg" class="img-responsive" alt="Three kittens running towards the camera.">
 | ||
|   <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>
 | ||
| ```
 | ||
| 
 | ||
| # --solutions--
 | ||
| 
 | ||
| ```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>
 | ||
| 
 | ||
|   <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>
 | ||
| 
 | ||
|   <img src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/running-cats.jpg" class="img-responsive" alt="Three kittens running towards the camera.">
 | ||
|   <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>
 | ||
| 
 | ||
|   <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>
 | ||
| ```
 |