CSS
Things have change a lot since the last few years. Here are my quick notes on using CSS3.
Nesting
CSS can now be nested (https://www.w3.org/TR/css-nesting-1/#nesting-selector). You can either use & (anything that is within), > (any immediate children).
| CSS2 | CSS3 |
|---|---|
.outside .inside {
# Styles A
}
.outside .inside img {
# Styles B
}
.outside .inside p {
# Styles C
}
|
.outside {
> .inside {
# Styles A
> img {
# Styles B
}
> p {
# Styles C
}
}
}
|
Selection
A > B selects immediate children.
A ~ B selects siblings
A + B selects immediate siblings
Square brackets select based on attribute. Eg. input[type=checkbox]. a[href=xxx].
Pseudo Classes
For form elements:
:enabled,:disabled:checked,:indeterminate:required,:optional:read-write,:read-only:in-range,:out-of-range(form validation with html5):valid,:invalid(form validation with html5):focus,:focus-within,:default
Targeting elements
:first-child,:last-child,:only-child:first-of-type,:last-of-type,:only-of-type, where type is a tag name:nth-child,:nth-last-child,:nth-of-type,:nth-last-of-type, to select every 2nd, 3rd, 4th, .. nth element:empty, for empty elements with no text
Pseudo elements, defined with two colons.
::after,::beforeto create a pseudo element before or after something.::selectioncustomizes how text appears when selected::placeholdercustomizes placeholder text (in text fields)
Layouts
Grid layout
Grid layouts are like tables with more features. You define a list of elements which can then be laid out into a grid using grid column/grid row.
Flex box
Flex boxes lays out items by sharing space amongst all child elements to avoid overflowing the parent.
Text columns
Texts can now be formatted to span multiple columns using CSS multi-column layout.
Media queries
The prefers-color-scheme can be used to determine the user's system theme (light or dark)
CSS variables
Also called custom properties.
Features
Transitions
Animations
Transformations
transform
filter
linear-gradient, radial-gradient, conic-gradient
background-size: cover and contain will scale a background image proportionally big enough or small enough tot fit inside.
Text decorations
CSS counters
Define a counter that can be incremented with counter-increment and referenced like a variable. Can also use counter-set: counter N to set the counter some value.
body { counter-reset: section; } /* Initial value is 0 */
h3::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
Shadows
box-shadow adds shadow effects around an element. Use the shadow box generator tool to help construct these rules.
text-shadow adds shadows to text.
Other tips
::markerpseudo elements can change a list item's marker.pointer-events:nonemakes an element ignore the mouse. Clicks go through to the element below.text-overflowis used to detect overflow. It can be used to show an ellipsis or some custom character.text-overflow: ellipsis ".."
Less
Rules can be inherited with &:extend()
img {
&:extend(.img-responsive);
}
See Also
| ||||||