Media Queries are used to style elements depending on certain features, like ...
... width of screen
.card-container {
display: flex;
flex-direction: row;
@media (max-width: 600px) {
flex-direction: column;
}
}
... how accurate the pointer is
div {
margin: 0 1.4rem;
@media (pointer: coarse) { /* touch device */
margin: 0;
}
}
... print view
@media print {
.advertisement {
display: none;
}
}
Container Queries are similar to Media Queries, but depend on a defined container
...
...
.card {
container-type: inline-size;
container-name: card;
}
@container card (inline-size < 235px) {
.details {
font-size: 1rem;
}
}
With CSS complex animations can be created. Two main types:
Normally, style changes are instant. With transitions it is possible to transition smoothly between the two styles
A transition needs a state (style) change and an event that triggers it. Examples of possible triggers:
<div class="colored-border">Hover over me!</div>
.colored-border {
background-color: #ff9900;
transition: background-color 1s;
}
.colored-border:hover {
background-color: #0099cc;
}
The property transition (also a shorthand) allows for some simple animation between different states of other properties:
transition: property duration easing timing-function delay;
@keyframes move {
0% { transform: translateY(-100%); }
70% {
background-color: #ccff00; /* green */
transform: translateY(0);
}
100% { background-color: #0099cc; } /* blue */
}
.moving-container {
background-color: #ccff00;
animation-name: move;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-duration: 2s;
}
Many different configuration options, we don't go into detail here
Check out the good documentation on MDN
Animations can be eased. The interpolation is by default slightly eased but can be configured with animation-timing-function.
Generally all properties that have a 'middle state' can be interpolated. Example:
If possible: animate properties which do not trigger layout or paint
@keyframes animate-fast {
0% { transform: scaleY(0); }
100% { transform: scaleY(100%); }
}
@keyframes animate-slower {
0% { height: 0; }
100% { height: 100%; }
}
Which properties? Check here
What if we want to display an element above another?
The z-index specifies the stack level of the element in the current stacking context
<div style="z-index: 1">z-index: 1</div>
<div style="z-index: 3">z-index: 3</div>
<div style="z-index: 2">z-index: 2</div>
<div style="z-index: 1">z-index: 1</div>
Last element does not appear on top because of z-index 1
z-index is not global, it is in relation to the current stacking context. A stacking context is formed on the following elements:
In this example, every positioned element creates its own stacking context, because of their positioning and z-index values
Accessibility is a big subject. We cover some tips here
Semantic elements help screen readers (and also SEO)
Example: the order of headings should always be consistent:
Wrong: <h1> → <h3> → <h4> → <h5>
Correct: <h1> → <h2> → <h3> → <h4>
Check that click / touch targets are at least 9mm high by 9mm wide
WGAC Target Size Spec
Properties to set colors:
They take: