CSS-only Gallery with Skewed Images and Hover Effects
It utilizes modern CSS properties like flexbox, border-radius, and the experimental corner-shape to morph images from skewed parallelograms into full rectangles when hovering over.
corner-shape: bevel for unique geometric shapes with automatic fallback support.@supports query for graceful degradation in browsers without corner-shape support.1. Create a container div with the class gallery. Place your img tags directly inside this container. You can add as many images as needed. Flexbox handles the distribution.
<div class="gallery"> <img src="1.jpg" alt="A Bear"> <img src="2.jpg" alt="A wolf"> <img src="3.jpg" alt="A lioness"> ... </div>
<!-- Right-to-left gallery for RTL languages --> <div class="gallery" style="direction: rtl;"> <img src="1.jpg" alt="A Bear"> <img src="2.jpg" alt="A wolf"> <img src="3.jpg" alt="A lioness"> ... </div>
2. Apply the following CSS rules to create the skewed gallery effect with hover animations.
.gallery {
--s: 50px; /* Controls the skew angle - increase for more dramatic effect */
display: flex; /* Creates horizontal layout */ gap: 10px; /* Spacing between images */ margin: 10px;
}
.gallery > img {
flex: 1; /* Each image takes equal space */ min-width: 0; /* Prevents flex items from overflowing */ height: 300px; /* Fixed height for uniform appearance */ object-fit: cover; /* Crops images to fill space without distortion */
/* Creates the skewed/beveled corner effect */ border-start-start-radius: var(--s) 100%;
border-end-end-radius: var(--s) 100%;
margin-inline-end: calc(-1*var(--s)); /* Overlaps images slightly */ corner-shape: bevel; /* Modern CSS property for beveled corners */ cursor: pointer; /* Indicates interactivity */ transition: .3s linear; /* Smooth animation on hover */}
/* Expands the hovered image */.gallery > img:hover {
flex: 1.6; /* Makes hovered image 60% larger */}
/* Removes top-left radius on first image and adjacent to hover */.gallery > img:is(:first-child,:hover),
.gallery > img:hover + * {
border-start-start-radius: 0 100%;
}
/* Removes bottom-right radius on last image and before hover */.gallery > img:is(:last-child,:hover),
.gallery > img:has(+ :hover) {
border-end-end-radius: 0 100%;
margin-inline-end: 0; /* Removes overlap for proper alignment */}
body {
background: #efd994; /* Optional background color */}
/* Fallback for browsers without corner-shape support */@supports not (corner-shape: bevel) {
.gallery > img {
border-radius: 0; /* Removes rounded corners */ margin: 0; /* Removes overlap effect */ }
} Q: Why don’t the beveled corners appear in Firefox or Safari?
A: The corner-shape: bevel property is currently only supported in Chromium-based browsers (Chrome, Edge, Opera). The @supports fallback automatically removes the skewed effect in unsupported browsers.
Q: How do I prevent layout shifts when images load?
A: Add explicit width and height attributes to your img tags. This reserves space before images load. You can also set min-height on the gallery container or use aspect-ratio properties.
Q: How can I make this gallery work vertically instead of horizontally?
A: Change display: flex to display: flex; flex-direction: column. Then swap the logical properties: use border-start-start-radius and border-start-end-radius for horizontal corners instead of diagonal ones. The hover expansion will work vertically.
The post CSS-only Gallery with Skewed Images and Hover Effects appeared first on CSS Script.
It’s coming… For years, governments, businesses and organizations have speculated on the impact of AI…
Let's make this simple: You want to know if there are any mid- or post-credits…
Secretlab recently opened up preorders for its highly anticipated lineup of Titan Evo Pokémon gaming…
One night last week, Terese Bastarache — the conservative activist who led the successful campaign…
WASHINGTON, DC - JANUARY 29: U.S. Secretary of War Pete Hegseth (C) speaks during a…
There’s a sale happening at Woot that’s delivering Black Friday-esque deals on video games through…
This website uses cookies.