CKTIS / CSS
Images and media in CSS
CSS controls how images and media content are displayed, from background images and gradients to fitting the content of img and video elements. The key distinction is between an element’s size, its content’s size, and its decorative background.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
background-image: <image> | none;
background-size: auto | contain | cover | <length-percentage [0,∞]>{1,2};
object-fit: fill | contain | cover | none | scale-down;
/* object-fit: none uses the default sizing algorithm with no specified size and the element’s content-box size as the default object size. If the object has a natural aspect ratio but neither a natural width nor a natural height, the algorithm applies a contain constraint to fit the object within the content box. */
/* object-fit: scale-down uses whichever of none and contain produces the smaller object size. */
object-position: <position>;In CSS, “images and media” is a topic area, not a keyword or a single property. The <image> type includes images referenced by url() and gradients such as linear-gradient(). A gradient is a CSS-generated image, not a color. CSS controls media presentation but does not replace the HTML mechanisms for selecting sources and controlling playback.
An image set with background-image is painted as an element’s background and does not itself give that element a size. Its scaling, position, and repetition are controlled by background-size, background-position, and background-repeat, respectively. Treat backgrounds as decoration; meaning important to the reader should also be available through content or an appropriate text alternative.
Replaced elements, such as img and video, have content whose internal rendering is not ordinary CSS descendant layout. width and height determine the box size, while object-fit sizes the replaced content relative to the element’s content box. If the object has a natural aspect ratio, both contain and cover preserve it: contain uses the largest size that fits within the content box, and cover uses the smallest size that is at least as wide and as tall as the content box. If the object has no natural aspect ratio, both values size it to the width and height of the element’s content box. fill can distort the aspect ratio. object-position positions the content: shifting it can cause part of the content to be clipped with contain or leave part of the content box uncovered with cover. These properties do not scale background images.
The example creates an image without downloading a file: two color stops at 50% produce a sharp boundary. The syntax shown is an abbreviated selection of declarations for one background layer, not the complete grammar. CSSWG documents are editor’s drafts; a feature’s presence in a draft does not establish browser support. The example requires separate verification in the target environment.
Examples
<!doctype html>
<meta charset="utf-8">
<title>CSS image</title>
<style>
.sample {
width: 200px;
height: 100px;
background-color: white;
background-image: linear-gradient(to right, red 50%, blue 50%);
background-size: 100% 100%;
background-repeat: no-repeat;
}
</style>
<div class="sample" role="img" aria-label="Red left half and blue right half"></div>Result
A 200 × 100 CSS-pixel rectangle: the left 100 pixels are red and the right 100 pixels are blue, with a sharp vertical boundary.
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.