Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❀ đŸ’»
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

CSS - fill/cover with image all div container keeping image ratio

0 contributions
3 points

In this article we are going to show how in CSS fill with image all div to cover all available space keeping image ratio. 

Note: for JavaScript version with img element read this article.

CSS styling example

In this section we cover all available div space with image keeping the image ratio.

background-size: cover; with image centering.

Note: it is important to provide div configuration to have size.

// ONLINE-RUNNER:browser;

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

    #image1 {
      border: 1px solid red;
      background-repeat: no-repeat;
      background-size: cover;
      background-position: 50%;
      background-image: url('https://dirask.com/static/bucket/1590005168287-pPLQqVWYa9--image.png');
      width: 120px; height: 200px;
    }
    
    #image2 {
      border: 1px solid red;
      background-repeat: no-repeat;
      background-size: cover;
      background-position: 50%;
      background-image: url('https://dirask.com/static/bucket/1590005168287-pPLQqVWYa9--image.png');
      width: 300px; height: 120px;
    }

  </style>
</head>
<body>
  <div id="image1"></div>
  <br />
  <div id="image2"></div>
</body>
</html>

Used resource:

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more