Blog
Change Text to an Image on Hover with CSS
To change text to an image on hover with CSS, place the image and a real text label in the same fixed-size link, then swap their opacity when the surrounding card is hovered. Add :focus-visible for keyboard users and show the image with its label on devices that cannot hover. No JavaScript is needed for this effect.
This tutorial replaces the older margin-based slide example with a stable, responsive card. The text remains the link’s accessible name even while its visual opacity is zero. The demo includes original illustrations, keyboard focus, a touch-first fallback and reduced-motion support.
Live demo: change text to an image with CSS
Text-to-image hover playground
Hover a card, Tab to its link, or enable the preview switch. Select a card to jump to its sample description. On touchscreens, illustrations and labels stay visible.
Explore the coast
Explore the mountains
Explore the forestCoastal illustration
A flat-color coastal scene. The image is a decorative preview; this heading remains a normal link destination.
Alpine illustration
A mountain-and-lake illustration. Keyboard focus reveals the same preview as pointer hover.
Forest illustration
An evergreen scene. On a touchscreen, the image and caption are visible without requiring hover.
Pure HTML and CSS. No JavaScript, framework, account or external image service is required.
1. Put the text and image inside a real link
<figure class="swap">
<a class="swap-link" href="#coast-details">
<img class="swap-image" src="coast.png" alt="" width="800" height="500">
<span class="swap-label">Explore the coast</span>
</a>
<figcaption>Coast: cliffs and turquoise water.</figcaption>
</figure>
<h2 id="coast-details">Coastal illustration</h2>
The anchor has a real destination and a readable text label. The sample destinations are descriptions further down the page; in a project, replace them with the appropriate page or section. Use a button instead when the control performs an action rather than navigation.
The preview image is decorative because the persistent text already names the link’s destination. Its empty alt attribute avoids repeating that name. If your image communicates extra information, provide an appropriate alternative and make essential information available outside the hover state.
2. Use opacity to swap the visible layers
.swap { margin: 0; }
.swap-link { display: block; position: relative; aspect-ratio: 8 / 5;
overflow: hidden; border-radius: 14px; background: #e3ecfc; }
.swap-image, .swap-label { position: absolute; inset: 0; width: 100%; height: 100%;
transition: opacity .2s ease; }
.swap-image { object-fit: cover; opacity: 0; }
.swap-label { display: flex; align-items: center; justify-content: center;
background: #e3ecfc; color: #17314f; opacity: 1; }
.swap-link:focus-visible { outline: 3px solid #173d9e; outline-offset: 4px; }
.swap-link:focus-visible .swap-image { opacity: 1; }
.swap-link:focus-visible .swap-label { opacity: 0; }
@media (hover: hover) {
.swap:hover .swap-image { opacity: 1; }
.swap:hover .swap-label { opacity: 0; }
}
@media (hover: none) {
.swap .swap-link .swap-image { opacity: 1; }
.swap .swap-link .swap-label { opacity: 1; inset: auto 0 0; height: auto;
min-height: 48px; background: #0d223ddd; color: white; }
}
@media (prefers-reduced-motion: reduce) {
.swap-image, .swap-label { transition: none; }
}
Both layers occupy the same card. Opacity changes their appearance without changing the card’s layout dimensions. The illustration uses object-fit: cover to fill the area; choose contain if cropping would hide important parts of your replacement asset. The figure is the hover target, so hovering its caption also reveals the image.
The MDN :hover reference describes the pointer-hover state. The keyboard rule uses :focus-visible, which lets browsers show a visible focus indication when appropriate. Do not remove a focus outline without providing a clear replacement.
3. Keep the label accessible while it fades
Opacity zero hides text visually but keeps it in the document and accessibility tree. Do not replace the real label with only a CSS-generated content string, and do not use display: none or visibility: hidden on the label if it is the link’s accessible name. The card’s destination and name should not change simply because the pointer entered it.
Reserve the preview for supplementary imagery. Essential prices, instructions or status information should remain visible in the normal page content. The persistent captions and sample descriptions help readers understand this demo without interacting with a hover effect.
4. Provide a touchscreen and reduced-motion fallback
A touchscreen may not provide reliable hover. The hover: none rule displays the illustration and a readable bottom label together. On a hybrid device, the media query describes the primary input mechanism; every link should remain usable regardless of whether a particular tap triggers a preview. MDN explains the hover media feature.
The demo’s Keep images visible checkbox adds a convenient CSS-only preview using :has() in current browsers. It is optional; the core hover and keyboard-focus rules work independently. A prefers-reduced-motion rule removes the fade when the device requests reduced motion.
Download and run the complete demo
- Download and extract the source ZIP.
- Open index.html in a current browser. No build, framework, account or server is required.
- Hover each card, then use Tab and Enter to check the keyboard interaction and sample destinations.
- Try Keep images visible, a touch-first device and a reduced-motion setting.
The ZIP includes HTML, CSS and three local illustrations in PNG and editable SVG formats. All visual assets required for the standalone demo are bundled; it does not depend on a third-party image host.
Troubleshooting the hover effect
- The image does not load: check its relative path, filename case and network response.
- The card collapses: give the link a size or aspect ratio because absolutely positioned children do not create its normal height.
- The image is cropped: change object-fit or use an asset with the same aspect ratio.
- The text disappears on a phone: keep both the image and visible label in the hover: none rule.
- Theme styles interfere: scope selectors to your component and inspect computed opacity, positioning and dimensions.
Frequently asked questions
Can I replace text with an image on hover without JavaScript?
Yes. CSS :hover and opacity handle the visual swap. Use real HTML text and a real image rather than injecting the whole control through generated content.
Does this change the actual text content?
No. It changes which layer is visible. The link’s text remains in the document, which keeps the destination understandable to assistive technology.
Can I use a logo instead of a scene?
Yes. Supply a local logo asset, choose suitable alternative text based on its purpose, and use object-fit: contain when the whole mark must remain visible.
Does a mobile-width browser always use hover: none?
No. Width and input capability are separate. A narrow desktop window can still have a mouse; test the media feature on an appropriate input device.
Conclusion
A practical text-to-image hover effect keeps its layout and link name stable, supports keyboard focus and leaves touchscreen users with a readable label. Use the small example to understand the technique, or download the full CSS-only playground with its local assets.