How to Turn a Rectangular Image into a Cropped Square Image with CSS

In this snippet, we’ll show how you can turn a rectangular image into a cropped square. We’ll use CSS and turn an image into a square without distorting it.

We’ll start with creating HTML.

Create HTML

  • Use two <div> elements with class attributes.
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <div class="original-img"></div>
    <div class="square"></div>
  </body>
</html>

Add CSS

First, we’ll demonstrate the original image and then the cropped square to show the difference between them.

  • Style the "original-img" class by adding an image with the background property and specifying its width, height and margin-bottom.
  • Style the "square" class. Add the image with the background property and set it to "50% 50% no-repeat". Add the width and height properties.
  • You can also add a :hover pseudo-class.
.original-img {
  width: 500px;
  height: 330px;
  margin-bottom: 20px;
  background: url("https://images.unsplash.com/photo-1522204538344-922f76ecc041?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60") no-repeat center center;
}

.square {
  background: url("https://images.unsplash.com/photo-1522204538344-922f76ecc041?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60") 50% 50% no-repeat;
  width: 300px;
  height: 300px;
}

.square:hover {
  opacity: 0.5;
}

Example of turning a rectangular image into a square:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        border: 2px solid #CCCCCC;
      }
      .original-img {
        width: 500px;
        height: 330px;
        margin-bottom: 20px;
        background: url("https://images.unsplash.com/photo-1522204538344-922f76ecc041?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60") no-repeat center center;
      }
      .square {
        background: url("https://images.unsplash.com/photo-1522204538344-922f76ecc041?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60") 50% 50% no-repeat;
        width: 300px;
        height: 300px;
      }
      .square:hover {
        opacity: 0.5;
      }
    </style>
  </head>
  <body>
    <div class="original-img"></div>
    <div class="square"></div>
  </body>
</html>

Result

Original Image
Cropped Square Image

If you need to add a link inside the <div>, use the following example, where we add an <a> tag.

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .square {
        background: url("https://images.unsplash.com/photo-1546146830-2cca9512c68e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60") 50% 50% no-repeat;
        width: 300px;
        height: 300px;
      }
      .square a {
        display: block;
        width: 300px;
        height: 300px;
      }
    </style>
  </head>
  <body>
    <div class="square">
      <a href="https://www.w3docs.com/">W3Docs</a>
    </div>
  </body>
</html>