2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: bad87fee1348bd9aedf08826
|
2020-12-16 07:37:30 +00:00
|
|
|
title: 使用顺时针方向指定元素的内边距
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 0
|
2019-12-13 05:47:57 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/cB7mBS9'
|
|
|
|
forumTopicId: 18346
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: use-clockwise-notation-to-specify-the-padding-of-an-element
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
如果不想每次都要分别声明 `padding-top`、`padding-right`、`padding-bottom`、`padding-left` 属性,可以把它们汇总在 `padding` 属性里面一并声明,像是这样:
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
`padding: 10px 20px 10px 20px;`
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
这四个值按顺时针排序:上、右、下、左,并且设置的效果等同于分别声明每一个方向的 `padding`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
按照顺时针顺序,将 class 为 "blue-box" 的元素的上内边距以及左内边距设置为 `40px`,右内边距和下内边距设置为 `20px`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
class 为 `blue-box` 的元素的上内边距应为 `40px`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('.blue-box').css('padding-top') === '40px');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
class 为 `blue-box` 的元素的右内边距应为 `20px`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('.blue-box').css('padding-right') === '20px');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
class 为 `blue-box` 的元素的下内边距应为 `20px`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('.blue-box').css('padding-bottom') === '20px');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
class 为 `blue-box` 的元素的左内边距应为 `20px`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('.blue-box').css('padding-left') === '40px');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
你应该按照顺时针排序,汇总声明的方式来设置 `blue-box` 的 `padding` 值。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
const removeCssComments = (str) => str.replace(/\/\*[\s\S]+?\*\//g, '');
|
|
|
|
assert(
|
|
|
|
/\.blue-box\s*{[\s\S]*padding[\s]*:\s*\d+px\s+\d+px\s+\d+px\s+\d+px(;\s*[^}]+\s*}|;?\s*})/.test(
|
|
|
|
removeCssComments($('style').text())
|
|
|
|
)
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2019-12-13 05:47:57 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --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: 20px 40px 20px 40px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.red-box {
|
|
|
|
background-color: crimson;
|
|
|
|
color: #fff;
|
|
|
|
padding: 20px 40px 20px 40px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.blue-box {
|
|
|
|
background-color: blue;
|
|
|
|
color: #fff;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<h5 class="injected-text">margin</h5>
|
|
|
|
|
|
|
|
<div class="box yellow-box">
|
|
|
|
<h5 class="box red-box">padding</h5>
|
|
|
|
<h5 class="box blue-box">padding</h5>
|
|
|
|
</div>
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```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: 20px 40px 20px 40px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.red-box {
|
|
|
|
background-color: crimson;
|
|
|
|
color: #fff;
|
|
|
|
padding: 20px 40px 20px 40px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.blue-box {
|
|
|
|
background-color: blue;
|
|
|
|
color: #fff;
|
|
|
|
padding: 40px 20px 20px 40px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<h5 class="injected-text">margin</h5>
|
|
|
|
|
|
|
|
<div class="box yellow-box">
|
|
|
|
<h5 class="box red-box">padding</h5>
|
|
|
|
<h5 class="box blue-box">padding</h5>
|
|
|
|
</div>
|
|
|
|
```
|