CSS: Difference between revisions
mNo edit summary |
|||
| Line 1: | Line 1: | ||
Things have change a lot since the last few years. Here are my quick notes on using CSS3. | Things have change a lot since the last few years. Here are my quick notes on using CSS3. | ||
CSS can now be nested | <br /> | ||
=== Nesting === | |||
CSS can now be nested (https://www.w3.org/TR/css-nesting-1/#nesting-selector). You can either use <code>&</code> (anything that is within), <code>></code> (any immediate children). | |||
{| class="wikitable" | {| class="wikitable" | ||
! CSS2 | !CSS2 | ||
! CSS3 | !CSS3 | ||
|- | |- | ||
| <syntaxhighlight lang="css"> | |<syntaxhighlight lang="css"> | ||
.outside .inside { | .outside .inside { | ||
# Styles A | # Styles A | ||
| Line 17: | Line 20: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| <syntaxhighlight lang="css"> | |<syntaxhighlight lang="css"> | ||
.outside { | .outside { | ||
> .inside { | > .inside { | ||
| Line 32: | Line 35: | ||
|} | |} | ||
=== Selection === | |||
<code>A > B</code> selects immediate children. | |||
<code>A ~ B</code> selects siblings | |||
<code>A + B</code> selects immediate siblings | |||
Square brackets select based on attribute. Eg. <code>input[type=checkbox]</code>. <code>a[href=xxx]</code>. | |||
=== Pseudo Classes === | |||
For form elements: | |||
* <code>:enabled</code>, <code>:disabled</code> | |||
* <code>:checked</code>, <code>:indeterminate</code> | |||
* <code>:required,</code> <code>:optional</code> | |||
* <code>:read-write</code>, <code>:read-only</code> | |||
* <code>:in-range</code>, <code>:out-of-range</code> (form validation with html5) | |||
* <code>:valid</code>, <code>:invalid</code> (form validation with html5) | |||
* <code>:focus</code>, <code>:focus-within</code>, <code>:default</code> | |||
Targeting elements | |||
* <code>:first-child</code>, <code>:last-child</code>, <code>:only-child</code> | |||
* <code>:first-of-type</code>, <code>:last-of-type</code>, <code>:only-of-type</code>, where type is a tag name | |||
* <code>:nth-child</code>, <code>:nth-last-child</code>, <code>:nth-of-type</code>, <code>:nth-last-of-type</code>, to select every 2nd, 3rd, 4th, .. nth element | |||
* <code>:empty</code>, for empty elements with no text | |||
Pseudo elements, defined with two colons. | |||
* <code>::after</code>, <code>::before</code> to create a pseudo element before or after something. | |||
* <code>::selection</code> customizes how text appears when selected | |||
* <code>::placeholder</code> customizes placeholder text (in text fields) | |||
== Less == | <br /> | ||
=== Layouts === | |||
==== Grid layout ==== | |||
[https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_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 ==== | |||
[https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout 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 [https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Columns 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 ==== | |||
==== [https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Counter_Styles/Using_CSS_counters CSS counters] ==== | |||
Define a counter that can be incremented with counter-increment and referenced like a variable. Can also use <code>counter-set: counter N</code> to set the counter some value. | |||
{{Highlight | |||
| code = body { counter-reset: section; } /* Initial value is 0 */ | |||
h3::before { | |||
counter-increment: section; | |||
content: "Section " counter(section) ": "; | |||
} | |||
| lang = css | |||
}} | |||
=== Shadows === | |||
[https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow box-shadow] adds shadow effects around an element. Use the [https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Backgrounds_and_Borders/Box-shadow_generator shadow box generator tool] to help construct these rules. | |||
[https://developer.mozilla.org/en-US/docs/Web/CSS/text-shadow text-shadow] adds shadows to text. | |||
== Other tips == | |||
* <code>::marker</code> pseudo elements can change a list item's marker. | |||
* <code>pointer-events:none</code> makes an element ignore the mouse. Clicks go through to the element below. | |||
* <code>text-overflow</code> is used to detect overflow. It can be used to show an ellipsis or some custom character. <code>text-overflow: ellipsis ".."</code> | |||
==Less== | |||
Rules can be inherited with {{code|&:extend()}} | Rules can be inherited with {{code|&:extend()}} | ||
| Line 48: | Line 136: | ||
== See Also == | ==See Also== | ||
* https://eev.ee/blog/2020/02/01/old-css-new-css/ | |||
*https://eev.ee/blog/2020/02/01/old-css-new-css/ | |||
{{Navbox Web}}[[Category:WebApp]] | {{Navbox Web}} | ||
[[Category:WebApp]] | |||
Revision as of 01:40, 17 December 2021
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
| ||||||