freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/basic-css/add-a-negative-margin-to-an...

116 lines
2.0 KiB
Markdown
Raw Normal View History

---
id: bad87fee1348bd9aedf08823
title: 给元素添加负外边距
challengeType: 0
videoUrl: 'https://scrimba.com/c/cnpyGs3'
forumTopicId: 16166
dashedName: add-a-negative-margin-to-an-element
---
# --description--
元素的 `margin外边距` 用来控制元素 `border边框` 与其周围元素之间的距离大小。
如果你把元素的 `margin` 设置为负值,元素会变得占用更多空间。
# --instructions--
请将蓝色框的 `margin` 设为负值,跟红色框 `margin` 的属性值设置成一样的大小。
将蓝色框的 `margin` 设置为 `-15px`,它会让蓝色框填满整个黄色框。
# --hints--
class 为 `blue-box` 的元素的 `margin` 应设置为 `-15px`
```js
assert($('.blue-box').css('margin-top') === '-15px');
```
# --seed--
## --seed-contents--
```html
<style>
.injected-text {
margin-bottom: -25px;
text-align: center;
}
.box {
border-style: solid;
border-color: black;
border-width: 5px;
text-align: center;
}
.yellow-box {
background-color: yellow;
padding: 10px;
}
.red-box {
background-color: crimson;
color: #fff;
padding: 20px;
margin: -15px;
}
.blue-box {
background-color: blue;
color: #fff;
padding: 20px;
margin: 20px;
}
</style>
<div class="box yellow-box">
<h5 class="box red-box">padding</h5>
<h5 class="box blue-box">padding</h5>
</div>
```
# --solutions--
```html
<style>
.injected-text {
margin-bottom: -25px;
text-align: center;
}
.box {
border-style: solid;
border-color: black;
border-width: 5px;
text-align: center;
}
.yellow-box {
background-color: yellow;
padding: 10px;
}
.red-box {
background-color: crimson;
color: #fff;
padding: 20px;
margin: -15px;
}
.blue-box {
background-color: blue;
color: #fff;
padding: 20px;
margin: 20px;
margin-top: -15px;
}
</style>
<div class="box yellow-box">
<h5 class="box red-box">padding</h5>
<h5 class="box blue-box">padding</h5>
</div>
```