2018-10-10 22:03:03 +00:00
---
id: bad87fee1348bd9aedf08845
2021-03-05 02:55:32 +00:00
title: 使用 span 创建行内元素
2018-10-10 22:03:03 +00:00
challengeType: 0
2020-09-07 08:17:39 +00:00
forumTopicId: 18370
2021-01-13 02:31:00 +00:00
dashedName: use-a-span-to-target-inline-elements
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-03-05 02:55:32 +00:00
可以使用 span 标签来创建行内元素。 还记得怎么使用 `btn-block` class 来创建填满整行的按钮吗?
2020-12-16 07:37:30 +00:00
2021-03-05 02:55:32 +00:00
< button class = 'btn' style = 'background-color: rgb(0, 100, 0); color: rgb(255, 255, 255);' > 普通按钮< / button >
2020-12-16 07:37:30 +00:00
2021-03-05 02:55:32 +00:00
< button class = 'btn btn-block' style = 'background-color: rgb(0, 100, 0); color: rgb(255, 255, 255);' > 块级按钮< / button >
2020-12-16 07:37:30 +00:00
2021-03-05 02:55:32 +00:00
上面的例子就是 "inline" (行内)元素和 "block" (块级)元素的区别。
2020-12-16 07:37:30 +00:00
2021-03-05 02:55:32 +00:00
通过使用行内元素 `span` ,可以把不同的元素放在同一行,甚至能为一行的不同部分指定不同样式。
2020-12-16 07:37:30 +00:00
2021-03-05 02:55:32 +00:00
使用一个 `span` 元素,将文本 `love` 嵌入现在包含文本 `Things cats love` 的 `p` 元素中。 然后给 `span` 元素设置 class `text-danger` ,使文本变成红色。
2020-12-16 07:37:30 +00:00
2021-03-05 02:55:32 +00:00
对含有文本 `Top 3 things cats hate` 的 `p` 元素这样处理:
2018-10-10 22:03:03 +00:00
2021-03-22 13:52:28 +00:00
```html
< p > Top 3 things cats < span class = "text-danger" > hate:< / span > < / p >
```
2020-09-07 08:17:39 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-03-05 02:55:32 +00:00
`span` 元素应该在 `p` 元素内。
2020-12-16 07:37:30 +00:00
```js
assert($('p span') & & $('p span').length > 0);
```
2018-10-10 22:03:03 +00:00
2021-03-22 13:52:28 +00:00
`span` 元素应该有文本 `love` 。
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(
$('p span') & &
$('p span').text().match(/love/i) & &
!$('p span')
.text()
.match(/Things cats/i)
);
2018-10-10 22:03:03 +00:00
```
2021-03-22 13:52:28 +00:00
`span` 元素应该有 `text-danger` class。
2020-12-16 07:37:30 +00:00
```js
assert($('span').hasClass('text-danger'));
2018-10-10 22:03:03 +00:00
```
2021-03-22 13:52:28 +00:00
`span` 元素应该有一个闭合标签。
2020-12-16 07:37:30 +00:00
```js
assert(
code.match(/< \/span > /g) &&
code.match(/< span / g ) & &
code.match(/< \/span > /g).length === code.match(/< span / g ). length
);
2018-10-10 22:03:03 +00:00
```
2020-08-13 15:24:35 +00:00
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```html
< link href = "https://fonts.googleapis.com/css?family=Lobster" rel = "stylesheet" type = "text/css" >
< style >
h2 {
font-family: Lobster, Monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
< / style >
< div class = "container-fluid" >
< h2 class = "text-primary text-center" > CatPhotoApp< / h2 >
2021-09-22 15:34:59 +00:00
< a href = "#" > < img class = "img-responsive 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 >
2021-01-13 02:31:00 +00:00
2021-09-22 15:34:59 +00:00
< img src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/running-cats.jpg" class = "img-responsive" alt = "Three kittens running towards the camera." >
2021-01-13 02:31:00 +00:00
< 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 >
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```html
< link href = "https://fonts.googleapis.com/css?family=Lobster" rel = "stylesheet" type = "text/css" >
< style >
h2 {
font-family: Lobster, Monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
< / style >
< div class = "container-fluid" >
< h2 class = "text-primary text-center" > CatPhotoApp< / h2 >
2021-09-22 15:34:59 +00:00
< a href = "#" > < img class = "img-responsive 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 >
2021-01-13 02:31:00 +00:00
2021-09-22 15:34:59 +00:00
< img src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/running-cats.jpg" class = "img-responsive" alt = "Three kittens running towards the camera." >
2021-01-13 02:31:00 +00:00
< 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 < span class = "text-danger" > love< / span > :< / 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 >
```