EN
CSS - style only required input HTML elements
0 points
In this article, we would like to show you how to style only required input HTML elements using CSS.
Quick solution:
xxxxxxxxxx
1
input:required {
2
background: yellow;
3
}
In this example, we present how to use :required
pseudo class to style only required HTML input elements.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
input:required {
7
background: #ffe973;
8
border: 2px solid red;
9
}
10
11
</style>
12
</head>
13
<body>
14
<input type="text" placeholder="username" required />
15
<input type="password" placeholder="password" required />
16
<input type="text" placeholder="e-mail" />
17
</body>
18
</html>
This solution may be useful when we want to style only input elements that have required
HTML attribute.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
input[required] {
7
background: #ffe973;
8
border: 2px solid red;
9
}
10
11
</style>
12
</head>
13
<body>
14
<input type="text" placeholder="username" required />
15
<input type="password" placeholder="password" required />
16
<input type="text" placeholder="e-mail" />
17
</body>
18
</html>