2018-09-30 23:01:58 +01:00
---
id: 587d7fa5367417b2b2512bbd
title: Extend One Set of CSS Styles to Another Element
challengeType: 0
2019-08-05 09:17:33 -07:00
forumTopicId: 301456
2021-01-13 03:31:00 +01:00
dashedName: extend-one-set-of-css-styles-to-another-element
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
Sass has a feature called `extend` that makes it easy to borrow the CSS rules from one element and build upon them in another.
For example, the below block of CSS rules style a `.panel` class. It has a `background-color` , `height` and `border` .
2019-05-14 05:01:32 -07:00
```scss
.panel{
background-color: red;
height: 70px;
border: 2px solid green;
}
```
2020-11-27 19:02:05 +01:00
Now you want another panel called `.big-panel` . It has the same base properties as `.panel` , but also needs a `width` and `font-size` . It's possible to copy and paste the initial CSS rules from `.panel` , but the code becomes repetitive as you add more types of panels. The `extend` directive is a simple way to reuse the rules written for one element, then add more for another:
2019-05-14 05:01:32 -07:00
```scss
.big-panel{
@extend .panel;
width: 150px;
font-size: 2em;
}
```
2020-11-27 19:02:05 +01:00
The `.big-panel` will have the same properties as `.panel` in addition to the new styles.
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Make a class `.info-important` that extends `.info` and also has a `background-color` set to magenta.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Your `info-important` class should have a `background-color` set to `magenta` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
code.match(
/\.info-important\s*?{[\s\S]*background-color\s*?:\s*?magenta\s*?;[\s\S]*}/gi
)
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
Your `info-important` class should use `@extend` to inherit the styling from the `info` class.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
code.match(/\.info-important\s*?{[\s\S]*@extend \s*?.info\s*?;[\s\S]*/gi)
);
```
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```html
2020-05-09 16:31:18 +02:00
< style type = 'text/scss' >
2018-09-30 23:01:58 +01:00
h3{
text-align: center;
}
.info{
width: 200px;
border: 1px solid black;
margin: 0 auto;
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
< / style >
< h3 > Posts< / h3 >
< div class = "info-important" >
< p > This is an important post. It should extend the class ".info" and have its own CSS styles.< / p >
< / div >
< div class = "info" >
< p > This is a simple post. It has basic styling and can be extended for other uses.< / p >
< / div >
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
2019-05-01 01:55:22 +02:00
```html
2020-05-09 16:31:18 +02:00
< style type = 'text/scss' >
2019-05-01 01:55:22 +02:00
h3{
text-align: center;
}
.info{
width: 200px;
border: 1px solid black;
margin: 0 auto;
}
.info-important{
@extend .info;
background-color: magenta;
}
< / style >
< h3 > Posts< / h3 >
< div class = "info-important" >
< p > This is an important post. It should extend the class ".info" and have its own CSS styles.< / p >
< / div >
< div class = "info" >
< p > This is a simple post. It has basic styling and can be extended for other uses.< / p >
< / div >
2018-09-30 23:01:58 +01:00
```