freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/applied-visual-design/center-an-element-horizonta...

64 lines
1.2 KiB
Markdown
Raw Normal View History

---
id: 587d78a3367417b2b2512ad0
challengeType: 0
videoUrl: 'https://scrimba.com/c/cyLJqU4'
forumTopicId: 301043
2020-10-01 15:54:21 +00:00
title: 使用 margin 属性将元素水平居中
---
## Description
<section id='description'>
在应用设计中经常需要把一个块级元素水平居中显示。一种常见的实现方式是把块级元素的 <code>margin</code> 值设置为 <code>auto</code>
同样的,这个方法也对图片奏效。图片默认是内联元素,但是可以通过设置其 <code>display</code> 属性为 <code>block</code>来把它变成块级元素。
</section>
## Instructions
<section id='instructions'>
通过给页面中的 <code>div</code> 添加值为 <code>auto</code><code>margin</code> 属性将其居中显示。
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: '<code>div</code><code>margin</code> 应该为 <code>auto</code>。'
testString: assert(code.match(/margin:\s*?auto;/g));
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='html-seed'>
```html
<style>
div {
background-color: blue;
height: 100px;
width: 100px;
}
</style>
<div></div>
```
</div>
</section>
## Solution
<section id='solution'>
```html
// solution required
```
</section>