You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 8-web-components/6-shadow-dom-style/article.md
+70-70Lines changed: 70 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,21 @@
1
1
# Shadow DOM styling
2
2
3
-
Shadow DOM may include both `<style>`and`<link rel="stylesheet" href="…">` tags. In the latter case, stylesheets are HTTP-cached, so they are not redownloaded for multiple components that use same template.
3
+
Lo shadow DOM può includere i tags `<style>`e`<link rel="stylesheet" href="…">`. Nel secondo caso, i fogli di stile sono HTTP-cached, e non verranno ricaricati per più componenti che usino lo stesso template.
4
4
5
-
As a general rule, local styles work only inside the shadow tree, and document styles work outside of it. But there are few exceptions.
5
+
Come regola generale, gli stili locali hanno effetto solamente all'interno dello shadow tree, mentre quelli del documento hanno effetto al di fuori di esso. Con qualche eccezione.
6
6
7
7
## :host
8
8
9
-
The `:host`selector allows to select the shadow host (the element containing the shadow tree).
9
+
Il selettore `:host`permette di selezionare lo shadow host (l'elemento contenente lo shadow tree).
10
10
11
-
For instance, we're making `<custom-dialog>`element that should be centered. For that we need to style the `<custom-dialog>` element itself.
11
+
Ad esempio, se stiamo creando un elemento `<custom-dialog>`da centrare sulla pagina. Per fare ciò dovremo creare lo stile all'interno dello stesso `<custom-dialog>`.
12
12
13
-
That's exactly what `:host` does:
13
+
Questo è ciò che fa `:host`:
14
14
15
15
```html run autorun="no-epub" untrusted height=80
16
16
<templateid="tmpl">
17
17
<style>
18
-
/*the style will be applied from inside to the custom-dialog element*/
18
+
/*lo stile viene applicato all'interno dell'elemento custom-dialog */
19
19
:host {
20
20
position: fixed;
21
21
left: 50%;
@@ -38,36 +38,36 @@ customElements.define('custom-dialog', class extends HTMLElement {
38
38
</script>
39
39
40
40
<custom-dialog>
41
-
Hello!
41
+
Ciao!
42
42
</custom-dialog>
43
43
```
44
44
45
45
## Cascading
46
46
47
-
The shadow host (`<custom-dialog>` itself) resides in the light DOM, so it's affected by document CSS rules.
47
+
Lo shadow host (`<custom-dialog>`) risiede dentro il light DOM, e quindi viene influenzato dalle regole CSS del documento.
48
48
49
-
If there's a property styled both in `:host` locally, and in the document, then the document style takes precedence.
49
+
Nel caso ci fosse una proprietà stilizzata localmente sia tramite `:host`, che tramite regola nel documento, quest'ultima avrebbe la precedenza.
50
50
51
-
For instance, if in the document we had:
51
+
Per esempio, se nel documento avessimo:
52
52
```html
53
53
<style>
54
54
custom-dialog {
55
55
padding: 0;
56
56
}
57
57
</style>
58
58
```
59
-
...Then the `<custom-dialog>`would be without padding.
59
+
...in questo caso il `<custom-dialog>`non avrebbe nessun padding.
60
60
61
-
It's very convenient, as we can setup "default" component styles in its `:host` rule, and then easily override them in the document.
61
+
Questo comportamento può fare comodo, perché in pratica ci permette di impostare gli stili di "default" di un componente nelle sue regole `:host`, per poterle poi facilmente sovrascrivere nel documento.
62
62
63
-
The exception is when a local property is labelled `!important`, for such properties, local styles take precedence.
63
+
Un'eccezione a questo comportamento si verifica quando una proprietà locale viene contrassegnata come `!important`, di conseguenza gli stili locali avranno la precedenza.
64
64
65
65
66
66
## :host(selector)
67
67
68
-
Same as `:host`, but applied only if the shadow host matches the `selector`.
68
+
Come nel caso di `:host`, ma applicato solo se lo shadow host combacia con il `selettore`.
69
69
70
-
For example, we'd like to center the `<custom-dialog>`only if it has `centered` attribute:
70
+
Ad esempio, se volessimo centrare il `<custom-dialog>`contenente l'attributo `centered`:
71
71
72
72
```html run autorun="no-epub" untrusted height=80
73
73
<templateid="tmpl">
@@ -101,40 +101,40 @@ customElements.define('custom-dialog', class extends HTMLElement {
101
101
102
102
103
103
<custom-dialogcentered>
104
-
Centered!
104
+
Centrato!
105
105
</custom-dialog>
106
106
107
107
<custom-dialog>
108
-
Not centered.
108
+
Non centrato.
109
109
</custom-dialog>
110
110
```
111
111
112
-
Now the additional centering styles are only applied to the first dialog: `<custom-dialog centered>`.
112
+
In tali condizioni gli stili aggiuntivi per centrare l'elemento, verrebbero applicati solamente al primo dialog: `<custom-dialog centered>`.
113
113
114
114
## :host-context(selector)
115
115
116
-
Same as `:host`, but applied only if the shadow host or any of its ancestors in the outer document matches the `selector`.
116
+
Come `:host`, ma applicati solo se lo shadow host o un qualunque dei suoi antenati esterni nel document, combacia con il `selettore`.
117
117
118
-
E.g. `:host-context(.dark-theme)`matches only if there's `dark-theme`class on `<custom-dialog>`on anywhere above it:
118
+
Ad esempio `:host-context(.dark-theme)`combacia solo nel caso ci fosse una classe `dark-theme`su `<custom-dialog>`o su qualunque elemento sopra di esso:
119
119
120
120
```html
121
121
<bodyclass="dark-theme">
122
122
<!--
123
-
:host-context(.dark-theme) applies to custom-dialogs inside .dark-theme
123
+
:host-context(.dark-theme) viene applicata al custom-dialogs dentro .dark-theme
124
124
-->
125
125
<custom-dialog>...</custom-dialog>
126
126
</body>
127
127
```
128
128
129
-
To summarize, we can use `:host`-family of selectors to style the main element of the component, depending on the context. These styles (unless `!important`) can be overridden by the document.
129
+
Riepilogando, possiamo usare la famiglia dei selettori `:host` per stilizzare l'elemento principale del componente, in riferimento al suo contesto. Questi stili (a meno che non vengano dichiarati con `!important`) possono essere sovrascritti con altri stili nel documento.
130
130
131
-
## Styling slotted content
131
+
## Stili sullo slotted content
132
132
133
-
Now let's consider the situation with slots.
133
+
Consideriamo ora la situazione con l'uso degli slot.
134
134
135
-
Slotted elements come from light DOM, so they use document styles. Local styles do not affect slotted content.
135
+
Gli slotted elements arrivano dal light DOM, quindi useranno gli stili del documento. Gli stili locali non avranno alcun effetto sullo slotted content.
136
136
137
-
In the example below, slotted `<span>`is bold, as per document style, but does not take `background`from the local style:
137
+
Nel seguente esempio, lo slotted `<span>`è in grassetto, per via dello stile del documento, ed invece `background`non sortirà nessun effetto perché dichiarato dentro lo stile locale
138
138
```html run autorun="no-epub" untrusted height=80
139
139
<style>
140
140
*!*
@@ -156,18 +156,18 @@ customElements.define('user-card', class extends HTMLElement {
156
156
span { background: red; }
157
157
*/!*
158
158
</style>
159
-
Name: <slot name="username"></slot>
159
+
Nome: <slot name="username"></slot>
160
160
`;
161
161
}
162
162
});
163
163
</script>
164
164
```
165
165
166
-
The result is bold, but not red.
166
+
Il risultato sarà in grassetto, ma non rosso.
167
167
168
-
If we'd like to styleslotted elements in our component, there are two choices.
168
+
Se volessimo stilizzare gli slotted elements all'interno del nostro componente, avremmo due scelte.
169
169
170
-
First, we can style the `<slot>` itself and rely on CSS inheritance:
170
+
La prima sarebbe di utilizzare lo `<slot>` stesso e fare affidamento all'ereditarietà dei CSS:
171
171
172
172
```html run autorun="no-epub" untrusted height=80
173
173
<user-card>
@@ -184,21 +184,21 @@ customElements.define('user-card', class extends HTMLElement {
184
184
slot[name="username"] { font-weight: bold; }
185
185
*/!*
186
186
</style>
187
-
Name: <slot name="username"></slot>
187
+
Nome: <slot name="username"></slot>
188
188
`;
189
189
}
190
190
});
191
191
</script>
192
192
```
193
193
194
-
Here `<p>John Smith</p>` becomes bold, because CSS inheritance is in effect between the `<slot>` and its contents. But in CSS itself not all properties are inherited.
194
+
In questo caso `<p>John Smith</p>` sarà in grassetto, perché l'ereditarietà dei CSS è in effetti tra lo `<slot>` ed il suo contenuto. Ma nel CSS stesso non tutte le proprietà vengono ereditate.
195
195
196
-
Another option is to use`::slotted(selector)` pseudo-class. It matches elements based on two conditions:
196
+
L'altra opzione sarebbe quella di usare la pseudo-classe`::slotted(selector)` Essa controlla la corrispondenza degli elementi basandosi su due condizioni:
197
197
198
-
1. That'saslotted element, that comes from the light DOM. Slotname doesn't matter. Just any slotted element, but only the element itself, not its children.
199
-
2. The element matches the `selector`.
198
+
1. Se si tratta di uno slotted element, proveniente dal light DOM. Lo slotname è irrilevante. È importante invece che sia l'elemento stesso e non i suoi figli.
199
+
2. Se l'elemento combacia con il `selettore`.
200
200
201
-
In our example, `::slotted(div)` selects exactly `<div slot="username">`, but not its children:
201
+
Nel nostro esempio, `::slotted(div)` seleziona esattamente `<div slot="username">`, ma non i suoi figli:
202
202
203
203
```html run autorun="no-epub" untrusted height=80
204
204
<user-card>
@@ -217,61 +217,61 @@ customElements.define('user-card', class extends HTMLElement {
217
217
::slotted(div) { border: 1pxsolidred; }
218
218
*/!*
219
219
</style>
220
-
Name: <slot name="username"></slot>
220
+
Nome: <slot name="username"></slot>
221
221
`;
222
222
}
223
223
});
224
224
</script>
225
225
```
226
226
227
-
Please note, `::slotted` selector can't descend any further into the slot. These selectors are invalid:
227
+
Nota bene, il selettore `::slotted` non può influenzare ciò che discende ulteriormente dentro lo slot. Questi selettori non sono validi:
228
228
229
229
```css
230
230
::slotted(divspan) {
231
-
/*our slotted <div> does not match this*/
231
+
/*il nostro slotted <div> non combacia con questo*/
232
232
}
233
233
234
234
::slotted(div) p {
235
-
/*can't go inside light DOM */
235
+
/*non va dentro il light DOM */
236
236
}
237
237
```
238
238
239
-
Also, `::slotted` can only be used in CSS. We can't use it in `querySelector`.
239
+
Inoltre, `::slotted` può essere usato solamente con iCSS. Non è possibile usarlo in `querySelector`.
240
240
241
-
## CSS hooks with custom properties
241
+
## CSS hooks con proprietà custom
242
242
243
-
How do we style internal elements of a component from the main document?
243
+
Come si possono stilizzare gli elementi interni di un componente, applicando stili dal documento principale?
244
244
245
-
Selectors like `:host` apply rules to `<custom-dialog>` element or `<user-card>`, but how to styleshadow DOM elements inside them?
245
+
I selettori com `:host` applicano le loro regole all'elemento `<custom-dialog>` o `<user-card>`, ma come possiamo stilizzare gli elementi dello shadow DOM al suo interno?
246
246
247
-
There's no selector that can directly affect shadow DOM styles from the document. But just as we expose methods to interact with our component, we can expose CSS variables (custom CSS properties) to style it.
247
+
Non abbiamo dei selettori che possano direttamente influire sugli stili dello shadow DOM dal documento. Ma dal momento che possiamo esporre dei metodi per interagire con i nostri componenti, possiamo anche esporre delle variabili CSS (proprietà CSS personalizzate) per stilizzarli.
248
248
249
-
**Custom CSS properties exist on all levels, both in light andshadow.**
249
+
**Le proprietà Custom CSS risiedono in tutti i livelli, sia nel light che nelloshadow.**
250
250
251
-
For example, inshadow DOM we can use`--user-card-field-color` CSS variable to style fields, and the outer document can set its value:
251
+
Per esempio, nelloshadow DOM, possiamo usare la variabile CSS `--user-card-field-color` per stilizzare i campi, e l'outer document può impostarne il valore:
252
252
253
253
```html
254
254
<style>
255
255
.field {
256
256
color: var(--user-card-field-color, black);
257
-
/*if --user-card-field-color is not defined, use black color */
257
+
/*se --user-card-field-color non è definito, usa il nero*/
<div class="field">Data di nascita: <slot name="birthday"></slot></div>
295
295
</template>
296
296
297
297
<script>
@@ -311,24 +311,24 @@ customElements.define('user-card', class extends HTMLElement {
311
311
312
312
313
313
314
-
## Summary
314
+
## Riepilogo
315
315
316
-
ShadowDOM can include styles, such as `<style>` or `<link rel="stylesheet">`.
316
+
Lo shadowDOM può includere stili, sia con `<style>` che con `<link rel="stylesheet">`.
317
317
318
-
Local styles can affect:
319
-
- shadow tree,
320
-
- shadow host with `:host`-family pseudoclasses,
321
-
- slotted elements (coming from light DOM), `::slotted(selector)` allows to select slotted elements themselves, but not their children.
318
+
Gli stili in locale possono influenzare:
319
+
- lo shadow tree,
320
+
- lo shadow host attraverso la famiglia di pseudoclassi `:host`,
321
+
- gli slotted elements (in arrivo dal light DOM), `::slotted(selector)` permettono di selezionare gli slotted elements stessi, ma non i loro figli.
322
322
323
-
Document styles can affect:
324
-
- shadow host (as it lives in the outer document)
325
-
- slotted elements and their contents (as that's also in the outer document)
323
+
Gli stili del document possono influenzare:
324
+
- lo shadow host (poiché risiede nell'outer document)
325
+
- gli slotted elements ed il loro contenuto (anche qui, poiché risiede nell'outer document)
326
326
327
-
When CSS properties conflict, normally document styles have precedence, unless the property is labelled as `!important`. Then local styles have precedence.
327
+
Quando delle proprietà CSS vanno in conflitto, normalmente gli stili del documento hanno la precedenza, a meno che la proprietà non venga contrassegnata come `!important`, ed in questo caso gli stili locali hanno la precedenza.
328
328
329
-
CSS custom properties pierce throughshadow DOM. They are used as "hooks" to style the component:
329
+
Le proprietà CSS custom permeano attraverso loshadow DOM e vengono usati come "hooks" per stilizzare il componente:
330
330
331
-
1. The component uses a custom CSS property to style key elements, such as `var(--component-name-title, <default value>)`.
332
-
2. Component author publishes these properties for developers, they are same important as other public component methods.
333
-
3. When a developer wants to styleatitle, they assign `--component-name-title` CSS property for the shadow host or above.
334
-
4. Profit!
331
+
1. Il componente usa una proprietà CSS personalizzata, per stilizzare gli elementi chiave, come `var(--component-name-title, <default value>)`.
332
+
2. L'autore del componente pubblica queste proprietà per gli sviluppatori, importanti tanto quanti gli altri metodi del componente.
333
+
3. Quando uno sviluppatore vuole stilizzare un titolo, gli basta assegnare la proprietà CSS `--component-name-title` partendo dallo shadow host o anche più in alto.
0 commit comments