CSS cannot change an image’s src attribute. To change the source of an image, edit the HTML or set the property with JavaScript. CSS can still change how the image appears, or display a separate background when the graphic is decorative.
CSS Change Image Src: Can It Work?
Can CSS change image src directly? No. The src attribute belongs to the HTML element; CSS rules control presentation. A CSS image src property does not exist. A rule that changes a background or renders generated content changes what is displayed, but leaves the element’s attribute intact. CSS cannot update an image source attribute, even when its appearance changes.
That distinction matters when inspecting the DOM or using the image URL elsewhere in code. Choose the mechanism based on whether the underlying content needs to change or only the visual styling does.
Change the HTML or Use JavaScript
For a fixed content image, put the desired URL in the markup and write alternative text that conveys its purpose:
<img id="product" src="blue-mug.jpg" alt="Blue ceramic mug">
When a user action changes the product, JavaScript can update both the source and the description:
const image = document.querySelector("#product");
image.src = "red-mug.jpg";
image.alt = "Red ceramic mug";
Run this code after the element exists, for example in a deferred script. The new src starts an image load; provide a valid URL and handle a loading failure if the interface depends on it. Use a real button for the action so keyboard users can operate it. If the image conveys a different meaning after the change, update its alt text to match.
Check srcset and any enclosing <picture> as well. The browser may select a candidate supplied there instead of the src URL. Inspect image.currentSrc to see the selected resource; changing src alone may not select the file you expect. The HTML image reference describes source selection.
Use a CSS Background for Decoration
For an ornament that adds no information, set a background on a sized element:
.hero {
min-height: 16rem;
background: #243342 url("hero.jpg") center / cover no-repeat;
}
@media (max-width: 40rem) {
.hero { background-image: url("hero-small.jpg"); }
}
The media rule switches the background image at the chosen viewport width. cover fills the box and may crop the picture; use contain if the entire image must remain visible, allowing space around it. The fallback color remains visible if the image fails to load. Keep overlaid text readable against both the image and the color.
Changing an image source with CSS backgrounds is suited to visual decoration because background images do not provide the same text alternative as an <img>. Put meaningful information in HTML, including a text alternative for a content image. Do not rely on a decorative background alone to communicate a status or instruction.
Other Visual and Responsive Options
For responsive content images, use <picture> with <source media="..."> and a fallback <img>. The browser selects a resource based on the declared conditions while the <img> supplies the alternative text. Use srcset and sizes when the same image is available at different resolutions. These HTML features select resources; they do not make CSS rewrite src.
CSS can also change image presentation with object-fit, opacity, filters, or visibility. A pseudo-element can display a decorative graphic, and content: url(...) can replace rendered content in supported cases. These techniques affect rendering rather than the DOM attribute; generated imagery also needs careful accessibility review. Test the result in the browsers and assistive technology your site supports.
For a meaningful image, use <img> and update its HTML source or JavaScript properties, keeping the alternative text accurate. For a decorative background, use CSS and retain sufficient contrast if text sits above it. That choice keeps the content model aligned with what the visitor needs to understand.
