| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 587d78a3367417b2b2512ace | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | title: 使用 float 属性将元素左浮动或右浮动 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 0 | 
					
						
							| 
									
										
										
										
											2020-02-11 15:46:34 +08:00
										 |  |  | videoUrl: 'https://scrimba.com/c/c2MDqu2' | 
					
						
							|  |  |  | forumTopicId: 301066 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: push-elements-left-or-right-with-the-float-property | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 接下来要介绍的定位机制并不是 `position` 属性的选项,而是通过元素的 `float` 属性来设置。 浮动元素不在文档流中,它向 `left` 或 `right` 浮动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。 通常需要用 `width` 属性来指定浮动元素占据的水平空间。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 使这两个元素按两列布局,`section` 和 `aside` 左右排列。 设置 `#left` 元素的 `float` 属性值为 `left`,设置 `#right` 元素的 `float` 属性值为 `right`。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | id 为 `left` 的元素的 `float` 属性值应为 `left`。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert($('#left').css('float') == 'left'); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | id 为 `right` 的元素的 `float` 属性值应为 `right`。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert($('#right').css('float') == 'right'); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-02-11 15:46:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```html | 
					
						
							|  |  |  | <head> | 
					
						
							|  |  |  |   <style> | 
					
						
							|  |  |  |     #left { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       width: 50%; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     #right { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       width: 40%; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     aside, section { | 
					
						
							|  |  |  |       padding: 2px; | 
					
						
							|  |  |  |       background-color: #ccc; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   </style> | 
					
						
							|  |  |  | </head> | 
					
						
							|  |  |  | <body> | 
					
						
							|  |  |  |   <header> | 
					
						
							|  |  |  |     <h1>Welcome!</h1> | 
					
						
							|  |  |  |   </header> | 
					
						
							|  |  |  |   <section id="left"> | 
					
						
							|  |  |  |     <h2>Content</h2> | 
					
						
							|  |  |  |     <p>Good stuff</p> | 
					
						
							|  |  |  |   </section> | 
					
						
							|  |  |  |   <aside id="right"> | 
					
						
							|  |  |  |     <h2>Sidebar</h2> | 
					
						
							|  |  |  |     <p>Links</p> | 
					
						
							|  |  |  |   </aside> | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | <head> | 
					
						
							|  |  |  |   <style> | 
					
						
							|  |  |  |     #left { | 
					
						
							|  |  |  |       float: left; | 
					
						
							|  |  |  |       width: 50%; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     #right { | 
					
						
							|  |  |  |       float: right; | 
					
						
							|  |  |  |       width: 40%; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     aside, section { | 
					
						
							|  |  |  |       padding: 2px; | 
					
						
							|  |  |  |       background-color: #ccc; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   </style> | 
					
						
							|  |  |  | </head> | 
					
						
							|  |  |  | <body> | 
					
						
							|  |  |  |   <header> | 
					
						
							|  |  |  |     <h1>Welcome!</h1> | 
					
						
							|  |  |  |   </header> | 
					
						
							|  |  |  |   <section id="left"> | 
					
						
							|  |  |  |     <h2>Content</h2> | 
					
						
							|  |  |  |     <p>Good stuff</p> | 
					
						
							|  |  |  |   </section> | 
					
						
							|  |  |  |   <aside id="right"> | 
					
						
							|  |  |  |     <h2>Sidebar</h2> | 
					
						
							|  |  |  |     <p>Links</p> | 
					
						
							|  |  |  |   </aside> | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | ``` |