blob: 6bc13d87d00428b7d76eee825792405a600b5c1f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
---
title: ':placeholder-shown'
slug: Web/CSS/:placeholder-shown
tags:
- CSS
- Pseudo-classe
- Reference
translation_of: Web/CSS/:placeholder-shown
---
{{CSSRef}}
La [pseudo-classe](/fr/docs/Web/CSS/Pseudo-classes) **`:placeholder-shown`** permet de représenter n'importe quel élément {{htmlElement("input")}} ou {{htmlElement("textarea")}} affichant [un texte de substitution](/fr/docs/Web/Guide/HTML/Forms_in_HTML#The_placeholder_attribute).
```css
/* Cible tout élément <input> ou <textarea> avec un */
/* attribut placeholder actuellement affiché */
:placeholder-shown {
border: 2px solid silver;
}
```
## Syntaxe
{{csssyntax}}
## Exemples
### Exemple simple
#### CSS
```css hidden
input:-ms-input-placeholder {
border-color: silver;
}
input:-moz-placeholder {
border-color: silver;
}
```
```css
input {
border: 2px solid black;
padding: 3px;
}
:placeholder-shown {
border-color: silver;
}
```
#### HTML
```html
<input placeholder="Saisir quelque chose ici">
```
#### Résultat
{{EmbedLiveSample("Exemples", 200, 60)}}
### Dépassement du texte
Sur certains écrans plus étroits (tels que ceux des smartphones), la largeur des boîtes de recherche et celle des champs de formulaire peut être réduite fortement. Le texte de substitution peut donc être tronqué de façon indésirable. On peut alors utiliser {{cssxref("text-overflow")}} pour gérer cela gracieusement.
#### HTML
```html
<input placeholder="Veuillez saisir quelque chose dans ce champ s'il vous plaît !">
```
#### CSS
```css hidden
input:-ms-input-placeholder {
text-overflow: ellipsis;
}
input:-moz-placeholder {
text-overflow: ellipsis;
}
```
```css
input:placeholder-shown {
text-overflow: ellipsis;
}
```
#### Résultat
{{EmbedLiveSample("Dépassement_du_texte", 200, 60)}}
### Texte coloré
#### HTML
```html
<input placeholder="Saisir quelque chose ici">
```
#### CSS
```css hidden
input:-ms-input-placeholder {
color: red;
font-style: italic;
}
input:-moz-placeholder {
color: red;
font-style: italic;
}
```
```css
input:placeholder-shown {
color: red;
font-style: italic;
}
```
#### Résultat
{{EmbedLiveSample("Texte_coloré")}}
### Champ de saisie personnalisé
#### HTML
```html
<form id="test">
<p>
<label for="name">Enter Student Name:</label>
<input id="name" placeholder="Student Name"/>
</p>
<p>
<label for="branch">Enter Student Branch:</label>
<input id="branch" placeholder="Student Branch"/>
</p>
<p>
<label for="sid">Enter Student ID:</label>
<input type="number" pattern="[0-9]{8}" title="8 digit ID" id="sid" class="studentid" placeholder="8 digit id"/>
</p>
<input type="submit"/>
</form>
```
#### CSS
```css hidden
input.studentid:-ms-input-placeholder {
background-color: yellow;
color: red;
font-style: italic;
}
input.studentid:-moz-placeholder {
background-color: yellow;
color: red;
font-style: italic;
}
```
```css
input {
background-color: #E8E8E8;
color: black;
}
input.studentid:placeholder-shown {
background-color: yellow;
color: red;
font-style: italic;
}
```
#### Résultat
{{EmbedLiveSample("Champ_de_saisie_personnalisé", 200, 180)}}
## Spécifications
| Spécification | État | Commentaires |
| -------------------------------------------------------------------------------------------- | ------------------------------------ | -------------------- |
| {{SpecName("CSS4 Selectors", "#placeholder", ":placeholder-shown")}} | {{Spec2("CSS4 Selectors")}} | Définition initiale. |
## Compatibilité des navigateurs
{{Compat("css.selectors.placeholder-shown")}}
## Voir aussi
- {{cssxref("::placeholder")}}
- {{cssxref("::-moz-placeholder")}}
- {{HTMLElement("input")}}
- {{HTMLElement("textarea")}}
- [Les formulaires HTML](/fr/docs/Web/Guide/HTML/Formulaires)
|