Image fitting and transforms
W3C CSS Snapshot 2026
Fit an image into a box and change where a box is painted.
On this page
Fit a picture with object-fit
Give the image box a width and height. contain shows the full image and may leave blank space. cover fills the box and may crop the edges. Both preserve the aspect ratio.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fit a picture with object-fit</title>
<style>
img { width: 94px; height: 94px; background: #def5f5; border: 1px solid teal; }
.cover { object-fit: cover; }
.contain { object-fit: contain; }
</style>
</head>
<body>
<img class="cover" src="data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20viewBox=%270%200%20160%2080%27%3E%3Crect%20width=%27160%27%20height=%2780%27%20fill=%27teal%27/%3E%3Ccircle%20cx=%2780%27%20cy=%2740%27%20r=%2730%27%20fill=%27orange%27/%3E%3C/svg%3E" alt="Cropped illustration">
<img class="contain" src="data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20viewBox=%270%200%20160%2080%27%3E%3Crect%20width=%27160%27%20height=%2780%27%20fill=%27teal%27/%3E%3Ccircle%20cx=%2780%27%20cy=%2740%27%20r=%2730%27%20fill=%27orange%27/%3E%3C/svg%3E" alt="Full illustration">
</body>
</html>
What to expect The first square is filled. The second shows the whole picture with space above and below.
Move, turn, and scale with transform
translate() moves the painted element, rotate() turns it, and scale() changes its drawn size. It keeps its original space in normal flow. Function order can change the result.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Move, turn, and scale with transform</title>
<style>
.badge { display: inline-block; margin: 8px 44px 8px 4px; padding: 5px; background: teal; color: white; transform: translateX(24px) rotate(-10deg) scale(1.15); }
</style>
</head>
<body>
<p><span class="badge">NEW</span> Desk Lamp</p>
</body>
</html>
What to expect The badge moves right, tilts, and grows. The text after it stays at its original position.