CSS: Difference between revisions

From Leo's Notes
This page was last edited on 17 December 2021, at 02:05.
m Added category
No edit summary
 
(2 intermediate revisions by the same user not shown)
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 using the {{code|>}} character.
<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:
|}
|}


The nesting must be immediate. It will not be inherited if the path does not match completely.
===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)
 
<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 layouts====
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].
 
<code>[https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow text-overflow]</code> is used to detect overflow. It can be used to show an ellipsis or some custom character. Eg. <code>text-overflow: ellipsis ".."</code>
 
<code>[https://developer.mozilla.org/en-US/docs/Web/CSS/white-space white-space]: pre-wrap</code> is like <code>pre</code> but wraps the text when necessary. <code>pre-line</code> is similar, but collapses white spaces.
 
==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 and animations====
Normally, CSS changes apply immediately. [https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions CSS transitions] allow these changes to be tweened over a period of time. Not all properties can be transitioned -- [https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties review the list of animated properties] for more information.
 
[https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Using_CSS_animations CSS animations] builds on top of the idea of CSS transitions by defining the start/intermediate/end state and controlling when these transitions are played.
 
====Transformations and image effects====
The [https://developer.mozilla.org/en-US/docs/Web/CSS/transform transform] property allows an element to be rotated, scaled, skewed, or translated. You can stack as many transforms as you'd like.
 
The [https://developer.mozilla.org/en-US/docs/Web/CSS/filter filter] property applies graphical effects like blurring, color adjustments (by changing contrast, brightness, hue, opacity, and more), and drop shadows (which is also available using box-shadow, but this might have better performance by leveraging hardware acceleration).
 
There are also CSS functions for [https://developer.mozilla.org/en-US/docs/Web/CSS/gradient gradients]: [https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient() linear-gradient], [https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/radial-gradient() radial-gradient], [https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/repeating-linear-gradient() repeating-linear-gradient], and [https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/conic-gradient() conic-gradient].
 
Image scaling can be accomplished by using <code>[https://developer.mozilla.org/en-US/docs/Web/CSS/background-size background-size]: cover</code> or <code>contain</code> and will scale a background image proportionally big enough or small enough to fit inside the parent container.
 
====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.
 
====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
}}


Rules can be isolated for certain viewports.
<br />


==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.
*


== Less ==
==Less==
Rules can be inherited with {{code|&:extend()}}
Rules can be inherited with {{code|&:extend()}}


Line 48: Line 143:




== 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]]

Latest revision as of 02:05, 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, ::before to create a pseudo element before or after something.
  • ::selection customizes how text appears when selected
  • ::placeholder customizes 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 layouts

Texts can now be formatted to span multiple columns using CSS multi-column layout.

text-overflow is used to detect overflow. It can be used to show an ellipsis or some custom character. Eg. text-overflow: ellipsis ".."

white-space: pre-wrap is like pre but wraps the text when necessary. pre-line is similar, but collapses white spaces.

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 and animations

Normally, CSS changes apply immediately. CSS transitions allow these changes to be tweened over a period of time. Not all properties can be transitioned -- review the list of animated properties for more information.

CSS animations builds on top of the idea of CSS transitions by defining the start/intermediate/end state and controlling when these transitions are played.

Transformations and image effects

The transform property allows an element to be rotated, scaled, skewed, or translated. You can stack as many transforms as you'd like.

The filter property applies graphical effects like blurring, color adjustments (by changing contrast, brightness, hue, opacity, and more), and drop shadows (which is also available using box-shadow, but this might have better performance by leveraging hardware acceleration).

There are also CSS functions for gradients: linear-gradient, radial-gradient, repeating-linear-gradient, and conic-gradient.

Image scaling can be accomplished by using background-size: cover or contain and will scale a background image proportionally big enough or small enough to fit inside the parent container.

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.

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) ": ";
}


Other tips

  • ::marker pseudo elements can change a list item's marker.
  • pointer-events:none makes an element ignore the mouse. Clicks go through to the element below.

Less

Rules can be inherited with &:extend()

img {
  &:extend(.img-responsive);
}


See Also