Achieve Responsiveness for background image with CSS

You can try to run the following code to achieve responsiveness for background image with CSS −


Live Demo

<!DOCTYPE html>
      <meta name = "viewport" content="width = device-width, initial-scale = 1.0">
         div {
            width: 100%;
            height: 300px;
            border: 2px dashed blue;
            background-image: url('');
            background-repeat: no-repeat;
            background-size: contain;
      <p>To check the effect, you need to resize the browser.</p>