Languages
[Edit]
EN

CSS - display only last image in div

6 points
Created by:
Joann-Calderon
383

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; } with img:last-child { display: inline; }

// ONLINE-RUNNER:browser;

<style>

    div img {
        display: none;
    }

    div img:last-child
    {
       display: inline;
    }

</style>
<div>
  <img src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
  <img src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
  <img src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
</div>

Where: display: inline can be replaced with any other values, e.g. block, flex, itp.

 

Practical example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    div.container img.image {
        display: none;
    }

    div.container img.image:last-child
    {
       display: inline;
    }

  </style>
</head>
<body>
  <div class="container">
    <img class="image" src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
    <img class="image" src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
    <img class="image" src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
  </div>
  <div class="container">
    <img class="image" src="https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png" />
    <img class="image" src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
    <img class="image" src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
  </div>
</body>
</html>

See also

  1. CSS - display only first image in div

Alternative titles

  1. CSS - display only last img element in div
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join