When putting text over an image, readability can be a concern. By combining the responsive HTML5
<picture> container with some best practices for image overlays, we can achieve a good result.
The initial decision when adding text over an image is color. Black or white on their own aren’t very good options, as they’re likely to have a low contrast with the image.
While it’s certainly an option to add a bright, contrasting color such as hot pink or bright red with a border, there are more subtle ways to accomplish this.
For this, we can look toward black and white text overlays. These can be difficult to read over many images, but by adding a linear-gradient background over the image of black to white in the case of black text at the bottom or white to black for white text at the bottom of the image an attractive, readable text overlay is possible. To further improve readability, especially as the text reaches larger sizes or is longer, covering more of the image and benefiting less from the linear-gradient, a text shadow can be used which offsets the text even more from the image.
All of this is usually achieved with a
<div> wrapper around
<img> and the text element
<div>, etc.) and an
::after pseudo-element sized to 100% of the parent and absolutely positioned over the wrapper:
<picture> element allows multiple representations of the same image to be offered to the browser, which it can select based on size, file type, or other preference. Each option is provided as a
<source> child element specifying the
srcset which contains URLs to the resources and information to help the browser to determine which source to select. The
<picture> element also requires an
<img> element which acts as fallback both for cases where no matching
<source> is found by the browser and when the browser does not support
<picture>. This makes it an excellent choice for providing responsive images at different file and image sizes and aspect ratios, as well as maintaining backward compatibility as older browsers will simply ignore
<picture> element actually wraps the
<img> (whose styles apply regardless of which
<source> is selected), we can do away with the more common but less semantic
div.img-wrap above. The styles remain the same, with selectors for
.img-wrap changing to
Our HTML is much more expressive. The markup does not contain an element that only exists to support the visual display of information. The CSS styles extend the markup to add our nicer overlap.
Photo by Ricardo Gomez Angel