Skip to content

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.

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

html
<!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. Include background-size: cover; to crop the image to fit the container, and overflow: hidden; to prevent any visual overflow.
  • You can also add a :hover pseudo-class.

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

css
.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;
  background-size: cover;
}

.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;
  background-size: cover;
  overflow: hidden;
}

.square:hover {
  opacity: 0.5;
}

Example of turning a rectangular image into a square:

Example of turning a rectangular image into a square:

html
<!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;
        background-size: cover;
      }
      .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;
        background-size: cover;
        overflow: hidden;
      }
      .square:hover {
        opacity: 0.5;
      }
    </style>
  </head>
  <body>
    <div class="original-img"></div>
    <div class="square"></div>
  </body>
</html>

Result

**Original Image**<div class="original-img"></div> **Cropped Square Image** <div class="square"></div>

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

Example of a cropped square image with a link:

html
<!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;
        background-size: cover;
        overflow: hidden;
      }
      .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>

Dual-run preview — compare with live Symfony routes.