EN
CSS - display only last image in div
6 points
In this short article, we would like to show how to display only the last img element in the div element using CSS.
Quick solution:
Use
img { display: none; }
withimg:last-child { display: inline; }
xxxxxxxxxx
1
<style>
2
3
div img {
4
display: none;
5
}
6
7
div img:last-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.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
div.container img.image {
7
display: none;
8
}
9
10
div.container img.image:last-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>