Image functions and object positioning
Definition and purpose
CSS image values can be generated or chosen from candidates. image-set() offers alternatives to the browser; object-fit and object-position instead control how the content of a replaced element sits in its own box.
Offer two generated candidates
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 100px; height: 100px; background-image: linear-gradient(red, red); background-image: image-set(linear-gradient(red, red) 1x, linear-gradient(blue, blue) 2x); } </style> </head> <body> <div id="a" aria-hidden="true"></div> </body></html>The candidates are local gradients; resolution selection is delegated to the browser.
Syntax and mental model
In image-set(), each candidate can have a resolution descriptor and a type() hint. The browser chooses a suitable candidate, not necessarily the largest. Supply a plain background-image first as a fallback. For meaningful images use img with alt text. With a 200 by 100 image in a 100 by 100 box, cover keeps the image ratio and crops 100 pixels horizontally. object-position: left center exposes the red half; right center exposes blue. These percentages align corresponding points of the object and box, not coordinates of an arbitrary crop.
Choose the visible half
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { display: block; width: 100px; height: 100px; object-fit: cover; object-position: left center; } </style> </head> <body> <img id="a" alt="Red and blue test pattern" src="data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 width=%27200%27 height=%27100%27%3E%3Cpath fill=%27red%27 d=%27M0 0H100V100H0Z%27/%3E%3Cpath fill=%27blue%27 d=%27M100 0H200V100H100Z%27/%3E%3C/svg%3E"> </body></html>The left half fills the square without distortion.
Common mistakes
Do not use background candidates as a substitute for meaningful HTML images. object-position does not select a file or alter box dimensions. A computed image-set value does not prove which candidate was painted.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Border images and opacity, Ruby, SVG and MathML styling. Next lesson: Filters and backdrop filters.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Image functions and object positioning — knowledge check
Five questions on image functions and object positioning.
What chooses an image-set candidate?
What does cover preserve?
Which setting shows the right half in this square?
Which element should carry meaningful alternative text?
What does object-position change?