2021-06-15 00:49:18 -07:00
---
id: 587d7fa5367417b2b2512bbd
2021-06-26 21:42:30 +05:30
title: Estendere un gruppo di stili CSS ad un altro elemento
2021-06-15 00:49:18 -07:00
challengeType: 0
forumTopicId: 301456
dashedName: extend-one-set-of-css-styles-to-another-element
---
# --description--
2021-06-26 21:42:30 +05:30
Sass ha una caratteristica chiamata `extend` che rende facile prendere in prestito le regole CSS da un elemento e partire da esse in un altro.
2021-06-15 00:49:18 -07:00
2021-06-26 21:42:30 +05:30
Per esempio, il seguente blocco di regole CSS stilizza una classe `.panel` . Esso specifica un `background-color` , un'`height` e un `border` .
2021-06-15 00:49:18 -07:00
```scss
.panel{
background-color: red;
height: 70px;
border: 2px solid green;
}
```
2021-06-26 21:42:30 +05:30
Supponi ora di volere un altro pannello chiamato `.big-panel` . Ha le stesse proprietà di base di `.panel` , ma ha anche bisogno di `width` e `font-size` . È possibile copiare e incollare le regole CSS iniziali da `.panel` , ma il codice diventa ripetitivo mano a mano che si aggiungono altri tipi di pannelli. La direttiva `extend` è un modo semplice per riutilizzare le regole scritte per un elemento, aggiungendone alcune per un altro:
2021-06-15 00:49:18 -07:00
```scss
.big-panel{
@extend .panel;
width: 150px;
font-size: 2em;
}
```
2021-08-25 09:12:11 -07:00
`.big-panel` avrà le stesse proprietà di `.panel` in aggiunta ai nuovi stili.
2021-06-15 00:49:18 -07:00
# --instructions--
2021-06-26 21:42:30 +05:30
Crea una classe `.info-important` che estende `.info` e ha anche un `background-color` impostato su magenta.
2021-06-15 00:49:18 -07:00
# --hints--
2021-06-26 21:42:30 +05:30
La tua classe `info-important` dovrebbe avere un `background-color` impostato su `magenta` .
2021-06-15 00:49:18 -07:00
```js
assert(
code.match(
/\.info-important\s*?{[\s\S]*background-color\s*?:\s*?magenta\s*?;[\s\S]*}/gi
)
);
```
2021-06-26 21:42:30 +05:30
La tua classe `info-important` dovrebbe usare `@extend` per ereditare gli stili dalla classe `info` .
2021-06-15 00:49:18 -07:00
```js
assert(
code.match(/\.info-important\s*?{[\s\S]*@extend \s*?.info\s*?;[\s\S]*/gi)
);
```
# --seed--
## --seed-contents--
```html
< style type = 'text/scss' >
h3{
text-align: center;
}
.info{
width: 200px;
border: 1px solid black;
margin: 0 auto;
}
< / 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 >
```
# --solutions--
```html
< style type = 'text/scss' >
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 >
```