Background images are usually treated as texture or contrast for legible content rather than content itself — if something really is content, an <img> is the safer choice. But there is a middle ground: when the position or scale of a background image shifts, the image can carry more context than a static one. The examples below sit on a fine line between decoration and content, which matters for accessibility, since backgrounds are not announced to screen readers. If the image is genuinely an image, use an <img> with proper alt text. It is also worth respecting a user's motion preference.
Panning and scaling on hover
Chris Coyier's demo puts a sales pitch and an enticing image together as a practical way to display ads inside content. Ads are typically constrained by limited real estate — advertisers are usually asked for assets at exact pixel dimensions so they fit the space. This demo sidesteps that limit: hovering the image moves and scales it, revealing more of the product than the original crop allowed.
The markup is a container with a linked-up <div> for the background image and another <div> for the content:
<div class="ad-container">
<a href="#" target="_blank" rel="noopener">
<!-- Background image container -->
<div class="ad-image"></div>
</a>
<div class="ad-content">
<!-- Content -->
</div>
</div>
In CSS, the container gets dimensions and a background image that does not repeat and is positioned by its bottom-left edge:
.container {
background-image: url("/path/to/some/image.png");
background-repeat: no-repeat;
background-position: 0 0;
height: 400px;
width: 350px;
}
The JavaScript does the real work: it reads the mouse position and the container's offset, then converts that value into a scale used for background-position. First, listen for mouse movements on the .container element:
let container = document.querySelector(".container");
container.addEventListener("mousemove", function(e) {
// Our function
}
);
The container's offsetX and offsetY properties are the starting point, but not used directly — the X value is smaller than needed and the Y value larger. A multiplier has to be tuned by feel; 1.32 and 0.455 work well for X and Y respectively. Multiply the offsets by those values, append a px unit, and apply the result to the background position:
let container = document.querySelector(".container");
container.addEventListener("mousemove", function(e) {
container.style.backgroundPositionX = -e.offsetX * 1.32 + "px";
container.style.backgroundPositionY = -e.offsetY * 0.455 + "px";
}
);
The background positions can also be reset to their original values when the pointer leaves the container:
container.addEventListener("mouseleave", function() {
container.style.backgroundPosition = "0px 0px";
}
);
A much cheaper version is possible with a hover transition in vanilla CSS, though it does not track the pointer.
A magnifier that paints a bigger picture
The zoom-on-hover pattern familiar from online clothing stores gives the user more context — the stitching on a sweater, for instance. It has two pieces: the container and the magnifier. Only the container needs markup, since the magnifier element is injected during interaction:
<div class="container"></div>
The CSS defines width and height variables for the magnifier glass itself, then gives the .container some shape and a background-image:
:root {
--magnifer-width: 85;
--magnifer-height: 85;
}
.container {
width: 500px;
height: 400px;
background-size: cover;
background-image: url("/path/to/image.png");
background-repeat: no-repeat;
position: relative;
}
Some magnifier styles can be declared up front, specifically the previously defined width and height variables:
.magnifier {
position: absolute;
width: calc(var(--magnifer-width) * 1px);
height: calc(var(--magnifer-height) * 1px);
border: 3px solid #000;
cursor: none;
background-image: url("/path/to/image.png");
background-repeat: no-repeat;
}
The magnifier is an absolutely-positioned square using the same background image file as the .container. The calc() function there exists solely to convert the unit-less variable value to pixels; arrange that however you prefer to avoid repetition.
In JavaScript, the first step is to access the CSS variable defined earlier, since it is used in several places. Then the mouse position within the container is needed, because that value drives the magnifier's background position:
// Get the css variables
let root = window.getComputedStyle(document.documentElement);
let magnifier_width = root.getPropertyValue("--magnifer-width");
let magnifier_height = root.getPropertyValue("--magnifer-height");
let container = document.querySelector(".container");
let rect = container.getBoundingClientRect();
let x = (e.pageX - rect.left);
let y = (e.pageY - rect.top);
// Take page scrolling into account
x = x - window.pageXOffset;
y = y - window.pageYOffset;
The core is a mousemove listener on the .container using event.pageX and event.pageY. To get the exact relative position of the mouse on the element, subtract the position of the parent element from that mouse position — getBoundingClientRect() returns an element's size and its position relative to the viewport. Subtracting the window pageX and pageY offsets accounts for scrolling when there is overflow.
The magnifier div is created first, then a mousemove function is added to the image container. That function assigns the magnifier a class attribute and sets its left and top values from the calculated mouse position:
// create the magnifier
let magnifier = document.createElement("div");
container.append(magnifier);
The class name gives the CSS something to scope to:
// run the function on `mousemove`
container.addEventListener("mousemove", (e) => {
magnifier.setAttribute("class", "magnifier");
}
Rather than sitting outside the container (as in the referenced video), the magnifier overlays the container and follows the mouse. if statements set its position only when X and Y are greater than or equal to zero and less than the container's width or height, subtracting the magnifier's own width and height from the X and Y values to keep it in bounds:
// Run the function on mouse move.
container.addEventListener("mousemove", (e) => {
magnifier.setAttribute("class", "magnifier");
// Get mouse position
let rect = container.getBoundingClientRect();
let x = (e.pageX - rect.left);
let y = (e.pageY - rect.top);
// Take page scrolling into account
x = x - window.pageXOffset;
y = y - window.pageYOffset;
// Prevent magnifier from exiting the container
// Then set top and left values of magnifier
if (x >= 0 && x <= container.clientWidth - magnifier_width) {
magnifier.style.left = x + "px";
}
if (y >= 0 && y <= container.clientHeight - magnifier_height) {
magnifier.style.top = y + "px";
}
});
Finally, the magnifier's background needs a scale so the user gets a bigger view of the image based on where the hover occurs. Define the magnifier, then variables for the background image's width and height as the basis for that scale, and set those values on the .magnifier styles:
// Magnifier image configurations
let magnify = 2;
let imgWidth = 500;
let imgHeight = 400;
magnifier.style.backgroundSize = imgWidth * magnify + "px " + imgHeight * magnify + "px";
The X and Y coordinates of the magnifier's image then become the .magnifier element's background-position. As with the magnifier position, the magnifier's width and height must be subtracted from X and Y using the CSS variables:
// the x and y positions of the magnifier image
let magnify_x = x * magnify + 15;
let magnify_y = y * magnify + 15;
// set backgroundPosition for magnifier if it is within image
if (
x <= container.clientWidth - magnifier_width &&
y <= container.clientHeight - magnifier_height
) {
magnifier.style.backgroundPosition = -magnify_x + "px " + -magnify_y + "px";
}
Slow pans: the Ken Burns effect
An image larger than its container slides and scales very slowly — the Ken Burns effect, a staple of documentary stills and Apple TV screen savers, with plenty of CodePen examples. There are several ways to build it; if the goal is simply a subtle scale, CSS is sufficient and JavaScript is unnecessary except for particular use cases. Multiple backgrounds can spice things up, or the rules can be expanded to elements instead of background images, applying the same animation to each background with a dash of animation-delay to stagger the effect. Sass and/or CSS variables can optimize it.
Layered backgrounds at varying speeds
Sarah Drasner's 2016 "Happy Halloween" pen layers backgrounds and moves them at varying speeds for an almost cinematic result, driven mainly by GSAP. A boiled-down version could simply translate each background layer from left to right at different speeds; the start and end of each background image must stay consistent so it repeats seamlessly when the animation loops.



