EN
CSS - display only first image in div
3 points
In this short article, we would like to show how to display only the first img element in the div element using CSS.
Quick solution:
Use
img { display: none; }
withimg:first-child { display: inline; }
xxxxxxxxxx
1
<style>
2
3
div img {
4
display: none;
5
}
6
7
div img:first-child
8
{
9
display: inline;
10
}
11
12
</style>
13
<div>
14
<img src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
15
<img src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
16
<img src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
17
</div>
Where: display: inline
can be replaced with any other values, e.g. block
, flex
, itp.
In the below examples, you can find multiple div
container elements where for each one only first image is displayed.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
div img {
7
display: none;
8
}
9
10
div img:first-child
11
{
12
display: inline;
13
}
14
15
</style>
16
</head>
17
<body>
18
<div class="container">
19
<img class="image" src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
20
<img class="image" src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
21
<img class="image" src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
22
</div>
23
<div class="container">
24
<img class="image" src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
25
<img class="image" src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
26
<img class="image" src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
27
</div>
28
</body>
29
</html>
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
div.container img.image+img.image
7
{
8
display: none;
9
}
10
11
</style>
12
</head>
13
<body>
14
<div class="container">
15
<img class="image" src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
16
<img class="image" src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
17
<img class="image" src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
18
</div>
19
<div class="container">
20
<img class="image" src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
21
<img class="image" src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
22
<img class="image" src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
23
</div>
24
</body>
25
</html>
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
div.container img.image~img.image
7
{
8
display: none;
9
}
10
11
</style>
12
</head>
13
<body>
14
<div class="container">
15
<img class="image" src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
16
<img class="image" src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
17
<img class="image" src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
18
</div>
19
<div class="container">
20
<img class="image" src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
21
<img class="image" src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
22
<img class="image" src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
23
</div>
24
</body>
25
</html>