日本語

Tilting and Overlapping Photos with CSS rotate

​​CSS​Flexbox,HTML,Playwright

To arrange photos as if they were scattered on a desk, give each photo a white border and tilt it by a few degrees with rotate. To overlap a photo with its neighbor, place them side by side and make the left margin negative so the photos move closer together. The tilt angle for each photo is passed in with a custom property.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened an HTML and CSS page with Playwright and recorded the getComputedStyle() values, element positions and sizes, and the element and pixel colors at overlapping points. I used four pictures I drew as SVG in place of real photos. I did not test Firefox, Safari, or real smartphones. The browser versions cited below come from the compatibility data (@mdn/browser-compat-data 8.1.8).

On a pale blue background, four pictures with white borders sit in a row. From left to right they show a sea, a mountain, a flower, and a night sky. Each is tilted at a slightly different angle, and the left and right edges of neighboring photos overlap.
The four photos arranged with the CSS in this article. From left to right, the tilts are -6, 4, -3, and 7 degrees.

Adding white borders and changing the angle for each photo

In the HTML, each photo is wrapped in a <figure>, and its tilt angle is passed in with a custom property called --tilt.

html
<div class="photos">
  <figure class="photo" style="--tilt: -6deg"><img src="img/sea.svg" alt="海" width="400" height="300"></figure>
  <figure class="photo" style="--tilt: 4deg"><img src="img/mountain.svg" alt="山" width="400" height="300"></figure>
  <figure class="photo" style="--tilt: -3deg"><img src="img/flower.svg" alt="花" width="400" height="300"></figure>
  <figure class="photo" style="--tilt: 7deg"><img src="img/night.svg" alt="夜空" width="400" height="300"></figure>
</div>

The CSS is as follows. This produces the arrangement shown in the image above.

css
.photos {
  --w: min(160px, 22vw);
  display: flex;
  justify-content: center;
  padding: 32px 16px;
}

.photo {
  flex: 0 0 auto;
  width: var(--w);
  margin: 0;
  padding: 8px 8px 28px;
  background: #fff;
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.25);
  rotate: var(--tilt, 0deg);
  transition: rotate 0.2s, scale 0.2s;
}

.photo + .photo {
  margin-left: calc(var(--w) * -0.25);
}

.photo img {
  display: block;
  width: 100%;
  height: auto;
}

.photo:hover {
  z-index: 1;
  rotate: 0deg;
  scale: 1.1;
}

The white background and padding of .photo form the photo's border. Making only the bottom padding wider, at 28px, gives the look of a printed photo. The box-shadow makes the photo appear to float slightly above the background.

The tilt comes from the single line rotate: var(--tilt, 0deg). Positive angles rotate clockwise, and negative angles rotate counterclockwise. In the test environment, reading rotate with getComputedStyle() gave -6deg, 4deg, -3deg, and 7deg for the four photos, so the values passed in the style attribute were used as they were. I kept every angle under 10 degrees and alternated the direction.

Overlapping a neighboring photo with a negative margin

.photo + .photo selects the second and later photos. Its left margin is set to a negative value equal to one quarter of --w (the photo width excluding the border), so it overlaps the photo on its left. I set the photo width in --w, capped at 160px, and let it shrink to 22% of the screen width. On a 680px-wide screen, every photo was 166px wide (including the border), and the left edges were at 65px, 193px, 321px, and 450px, so each was offset by 128 to 129px.

In the overlapping areas, the photo written later in the HTML is drawn on top. I used document.elementFromPoint() to check the point where the first and second photos overlap. It returned the second photo (the mountain), and the pixel at that point was the white border of the second photo.

Tilting does not change the surrounding layout. The first photo's offsetWidth stayed at 166px, but the box that encloses the tilted shape (getBoundingClientRect()) grew to 180.2px wide. The overlap is determined by the position set with the margin, and the rotation only changes how that position looks. The CSS Transforms specification also says that a transform changes only the visual appearance and, apart from overflow, does not affect the surrounding layout.

Straightening and bringing a hovered photo to the front

.photo:hover uses rotate: 0deg to remove the tilt and scale: 1.1 to enlarge the photo slightly. z-index: 1 brings the hovered photo in front of the overlapping photo on its right. z-index works on flex items even without a position value.

Of the four photos, only the second, the mountain, is straight, slightly larger, and overlapping the photos on its left and right.
The mouse is over the second photo. Its tilt is removed, and it is in front of the flower photo on its right.

When I hovered over the second photo in the test environment, rotate became 0deg, scale became 1.1, and z-index became 1. Before hovering, the point where the second and third photos overlapped returned the third photo (the flower). After hovering, it returned the second photo (the mountain).

Because transition changes the values over 0.2 seconds, I also read the intermediate values. When I stopped the transition at 0.1 seconds, rotate was 0.790386deg and scale was 1.08024, so the photo was partway through returning from 4 degrees to straight.

Writing with rotate and scale instead of transform

You can also tilt a photo with transform: rotate(-6deg). However, if you write only transform: scale(1.1) on hover, the whole transform value is replaced and the tilt disappears. In the test environment, I tilted a photo with transform: rotate(var(--tilt)), changed it to transform: scale(1.1) on :hover, and hovered over the first photo. The value after hovering was matrix(1.1, 0, 0, 1.1, 0, 0), and the rotation component was gone.

If you write rotate and scale as separate properties, you can change one without affecting the other. I changed only scale on :hover and left rotate as var(--tilt). After hovering, rotate was still -6deg, and only scale became 1.1. This is also why the CSS in this article writes rotate: 0deg to straighten the photo and scale: 1.1 to enlarge it as separate declarations.

The CSS Transforms Level 2 specification defines the order in which translate, rotate, and scale are combined. Unlike when functions are listed inside transform, you do not need to worry about the order in which you write them.

According to the compatibility data, the rotate and scale properties are supported in Chrome 104, Edge 104, Firefox 72, Safari 14.1, and Safari on iOS 14.5.

Giving untilted photos rotate: 0deg as well

Suppose you want to straighten only one of the arranged photos. If you set that photo's rotate to none, the part of it that overlaps the photo on its left goes underneath that photo. I set only the second photo to rotate: none and checked the point where the first and second photos overlap. document.elementFromPoint() returned the first photo (the sea), and the pixel color was the sea color of the first photo, rgb(31, 163, 198). The second photo comes later in the HTML, yet the first photo is painted on top.

Of the four photos, only the second, the mountain, is straight. The right edge of the sea photo on its left overlaps the mountain photo.
Only the second photo is set to rotate: none. The first photo, which comes earlier, is painted on top of the second.

The CSS Transforms Level 2 specification says that when rotate is none, no stacking context is created. Any value other than none creates one, even a value like 0deg that does not change the appearance. An element given a transform through transform, rotate, or similar properties is painted in the same order as an element with a position value and z-index: 0 (CSS Transforms specification). As a result, the tilted first photo is painted after the untransformed second photo and ends up on top.

If you set rotate: 0deg on the photo you want straight, it overlaps in HTML order. I set rotate: 0deg on the second photo and checked the same point. The second photo (the mountain) was returned, and the pixel was the white border of the second photo. In the CSS in this article, a photo without --tilt also gets 0deg through rotate: var(--tilt, 0deg).

Preventing horizontal scrolling caused by tilted corners

The corners of a tilted photo extend outside the element's rectangle. The CSS Transforms specification includes the transformed area in the scrollable overflow. If you arrange the photos across the full width of the screen, the corners of the rightmost photo stick out and cause horizontal scrolling.

On a 375px-wide screen, I set the left and right padding of .photos to 0 and made each photo 25% of the screen width. The corner of the rightmost photo, tilted 7 degrees, extended to 377.9px, and the page's scrollWidth became 378px, which is larger than the screen width of 375px.

The CSS in this article gives .photos 16px of padding on the left and right and makes each photo 22% of the width, leaving room for the corners. On a 375px-wide screen, the corners stayed between 16.6px on the left and 359.2px on the right, scrollWidth was 375px, and no horizontal scrolling appeared. The amount by which the corners extend grows with the angle. On a 680px-wide screen, the box enclosing a 166px-wide photo was 173.1px at 3 degrees, 175.5px at 4 degrees, 180.2px at 6 degrees, and 182.4px at 7 degrees. If you use larger angles, leave more margin as well.

​Posted by 86