26
loading...
This website collects cookies to deliver better user experience
img
ARIA role:<div role="img">
<!-- HTML of the image -->
</div>
Author's note: from now on, our CSS art will be identified as an image, so the terms "CSS art", "drawing", "illustration", or "image" may be used interchangeably.
alt
attribute.aria-label
attribute to the image, and providing the alternative text there:<div aria-label="The alternative text goes here">
<!-- HTML of the image -->
</div>
aria-labelledby
attribute instead:<div aria-labelledby="alt-image">
<div id="alt-image">The alternative text goes here</div>
<!-- HTML of the image -->
</div>
#alt-image {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
<img />
would be displayed on the page if the image fails to load.px
, cm
, pt
, etc. This way, our image will have a fixed size and won't be responsive (although it will scale better than raster images as it is a vectorial drawing), and we should be able to use any CSS property and value.%
, vmin
, em
, etc. if we want to achieve scalability.box-shadow
or border
)clip-path
can be relative with polygon()
but not with path()
)<div>
for every part of our drawing, and especially for the container. The real question would be "Which one?"Author's note: I understand my pick of semantic elements could be debatable. I will try to keep them as neutral/objective as possible.
<article>
and <figure>
/<figcaption>
. While the latter may seem the obvious choice, the former has additional semantic advantages, as we'll soon see.<figure>
. A self-contained content with an optional caption (<figcaption>
) that could be used as alternative text:<figure aria-labelledby="alt-image">
<figcaption id="alt-image">Here goes the alternative text</figcaption>
<!-- HTML of the image -->
</figure>
<article>
: a complete, self-contained, and independent composition intended to be distributed or reused. Precisely what our CSS art is!<article>
<!-- HTML of the image -->
</article>
<article>
within an <article>
isn't allowed, but that's incorrect. An article can contain another article. When there are nested articles, the inner article must be related to the outer article.<article aria-labelledby="image-alt"
aria-describedby="image-info">
<!-- The whole header would be accessibly hidden -->
<header>
<h2 id="image-alt">Title/Alternative text</h2>
<p id="image-info">
<address>
Created by
<a href="https://twitter.com/alvaro_montoro">
Alvaro Montoro
</a>
</address>
on
<time datetime="2021-06-10">June 10th, 2021</time>
</p>
</header>
<!-- HTML of the image -->
</article>
prefers-reduced-motion
media query that allows developers to do exactly that.@media (prefers-reduced-motion) {
.animated {
animation: none;
}
}
prefers-contrast
: to indicate if the user wants a higher or lower contrast.prefers-color-scheme
: to detect if the user requested dark/light color theme (often used for night/day modes.)forced colors
: to specify if the user chose a limited color palette.
@media (prefers-contrast: more) {
/* more: higher contrast colors, borders, no transparencies */
/* less: lower contrast colors */
}
@media (prefers-colors-scheme: dark) {
/* dark: drawing with more contrast vs dark background */
/* light: drawing with more contrast vs light background */
}
@media (forced-colors: active) {
/* override properties like box-shadow, add borders, etc. */
}
box-shadow
, which is forced to 'none' in forced-colors mode.aspect-ratio
. With it, we will be able to set the preferred aspect ratio for the image, which will be used for calculating the auto sizes when scaling:/* This CSS art will be squared */
#myCSSart {
aspect-ratio: 1 / 1;
}
/* This CSS art will be twice as tall as it is wide */
#myCSSart {
aspect-ratio: 1 / 2;
}