Skip to content

CSS for JS

Posted on:September 23, 2022 at 03:22 PM

0: Fundamentals recap

CSS building blocks

/* rule/style */
.code-snippet {
  padding: 32px; /* First Declaration */
  white-space: pre-wrap; /* Second Declaration */
}
/* code-snippet: selector
   padding: property */

media queries

/* CSS */
@media (condition) {
  /* Some CSS that'll run if the condition is met. */
}

Responsive design

What is an iframe

Pseudo classes

/* Target the last paragraph child in a container */
p:last-child {
  margin-bottom: 0px;
}

Pseudo elements

Pseudo classes vs pseudo elements

Children vs descendents

Combinators in selectors

Color formats

Units

Note:

Where to use rem or px

Browser zooming vs font scaling

Typography

Font style/category

Web fonts

/* notice the quote */
font-family: "Roboto", Arial, sans-serif;

Font stack

Typical text formatting

Styles and semantics

Alignment

Text transforms

Spacing

1:Rendering logic-1

user-agent stylesheet

Inheritance

Forcing inheritance

The Cascade

Specificity

const appliedStyles = {
  ...inheritedStyles,
  ...tagStyles,
  ...classStyles,
  ...idStyles,
  ...inlineStyles,
  ...importantStyles,
};

Block and Inline Directions

Logical properties

Box model

The four aspects that make up the box model are:

Box Sizing

Note

The default value for the width property is auto. By default, for most block elements, this means automatically grow to fill as much space as possible.

Padding

Shorthand properties

Border

Flow Layout

Two main element types in Flow layout

Block elements

Inline elements

Inline-block

Replaced elements

Inline elements have magic space

Width algorithms

Keyword values

Intrinsic vs extrinsic value

min-content

max-content

fit-content

min-width max-width

Figures and captions

Height Algorithms

Setting the minimum height to 100% of browser window

Why does height: 100vh doesn’t work

Important difference between width and height

Note: margin:auto behaves differently in flex layout.

Margin vs padding

Margin Collapse

Rules of Margin Collapse

Rule:1

Rule:2

Rule:3

Rule:4

Rule:5

Rule:6

Rule:7

Rule:8

Rule:9

Rule:10

Using Margin Effectively

2: Rendering Logic II

Positioned layouts

Relative positioning

Moving elements with margin vs positioning

Absolute Positioning

Note: Any time you want an element to be “floating above” the content, like a tooltip or a dropdown or a modal, absolute positioning is your friend.

Default placement of absolute positioned elements

Absolute sizes

Centering Trick: Absolute positioning

.box {
  position: absolute;
  top: 0px;
  left: 0px;
  right: 0px;
  bottom: 0px;
  width: 100px;
  height: 100px;
  margin: auto;
  background: deeppink;
}

The “inset” property

.box {
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.box {
  inset: 0;
}

Containing Blocks

Stacking Contexts

Summary of stacking

Z-index

Stacking contexts

Managing z-index

Isolated stacking contexts

Portals

Fixed positioning

Centering with fixed position.

Fixed without anchor points

Incompatibility of fixed with certain CSS properties

Overflow

Overflow property values

Note: overflow: hidden is identical to overflow: scroll, but with the scrollbars removed.

Scroll Containers

Horizontal Overflow

Positioned Layout and overflow

Fixed positioning and overflow

Sticky Positioning

Sticky element stays in their box

Offset

Horizontal stickiness

Sticky positioning and browser support

Troubleshooting sticky positioning

Hidden Content

Accessibility

3: Modern Component Architecture

What are CSS in JS libraries

Tagged Template Literals

details & summary tags

CSS variables aka Custom properties

Component libraries

Design systems and design tokens

Design tokens

4: Flexbox

Directions and Alignment in Flexbox

Alignment tricks

Growing and shrinking

Ratios

The flex shorthand

Effect of flex basis when flex-grow is 1 or any number

Constraints

Fortunately, a familiar set of properties can help us out here: min-width/max-width and min-height/max-height

Flex wrap

Content vs items

Groups and Gaps

Ordering

Flexbox Interactions

Positioned flex children

Margin collapse in flex container

z-index in flex container

5: Responsive and Behavioural CSS

Working with mobile devices

The magical meta tag

Mobile Testing

Media Queries

Other Queries

Hovering

Boolean logic in media queries

/* Allows us to apply styles based on
   input mechanism the user is using. */
@media (hover: hover) and (pointer: fine) {
  button:hover {
    text-decoration: underline;
  }
}
@media (prefers-color-scheme: dark) {
  /* Dark-mode styles here */
}
@media (prefers-reduced-motion: no-preference) {
  /* Animations here */
}
@media (orientation: portrait) {
  /* Styles for windows that are taller 
  than they are wide */
}
@media (orientation: landscape) {
  /* Styles for windows that are 
  wider than they are tall */
}

Breakpoints

@media (min-width: 550px) {
  /* Tablets */
}
@media (min-width: 1100px) {
  /* Laptop */
}
@media (min-width: 1500px) {
  /* Desktop */
}

Managing breakpoints

Rem breakpoints

CSS variables

Disabling inheritance

@property --text-color {
  syntax: "<color>";
  inherits: false;
  initial-value: black;
}

main {
  --text-color: deeppink;
  color: var(--text-color);
}

section {
  color: var(--text-color);
}

Default values

/* If our .btn element or one of its ancestors 
   assigns a value to the --inner-spacing property, 
   that value will be used. Otherwise, it'll use a 
   fallback of 16px.
   */
.btn {
  padding: var(--inner-spacing, 16px);
}

Reactive properties

Responsive values and CSS variables

Variable Fragments

Dark Mode

Calc in CSS

.something {
  width: calc(100px + 24px);
  height: calc(50px + 25px * 4);
}

Viewport Units

Scrollburglars

Responsive Typography

/* We should not do this, It's a widely-accepted 
   convention that 1rem is equal to 16px. We're 
   just making things more complicated if we change 
   that convention. 
*/
html {
  font-size: 125%;
}

Fluid design

6: Typography and Images

Text Rendering

Kerning

Text Rasterization and anti-aliasing

Bitmap fonts

Vector fonts

Rasterization and Anti-aliasing

Font Smoothing

HTML entities

Text Overflow

Hyphenation

p {
  overflow-wrap: break-word;
  hyphens: auto;
  /* Prefix for Safari */
  -webkit-hyphens: auto;
}

Balanced text

Ellipsis

Single-line ellipsis

Multi-line ellipsis

p {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  margin-bottom: 1em;
}

Multi-line ellipsis

Multi-Column Layout

Floats

Indentation

p {
  text-indent: 2rem;
}

Justified alignment

Masonry Grid with Columns

Text styling

Line length

Text alignment

Font Stacks

.title {
  font-family: "Lato", Futura, Helvetica, Arial, sans-serif;
}

Web Fonts

Downside of Google fonts

Font Loading UX

The font-display property

Variable Fonts

/* Get variable fonts from google */
<link rel="preconnect" href="https://fonts.gstatic.com">
<link
  rel="stylesheet"
  href="https://fonts.googleapis.com/css2?family=Raleway:ital,wght@0,100..900;1,100..900&display=swap"
>

7: CSS Grid

Grid mental model

Intrinsic vs extrinsic sizing in CSS

Grid browser support

.wrapper {
  display: flex;
}

@supports (display: grid) {
  .wrapper {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

Grid Flow and Layout Modes

Grid auto flow

Grid vs Flexbox

Grid construction

Implicit rows

Explicit rows

Placing children in grid

// First two columns
.item1 {
  grid-column-start: 1;
  grid-column-end: 3;
}

// First two columns, short hand
.item1 {
  grid-column: 1/3;
}

// Entire row, works only with explicit rows/columns
// defined with grid-template-columns/grid-template-rows
.item1 {
  grid-column: 1/-1;
}

Out-of-bounds items

Gaps

The “repeat” function

.calendar {
  grid-template-columns: 250px 1fr 1fr 1fr 1fr 1fr;
}
.calendar {
  grid-template-columns: 250px repeat(5, 1fr);
}

Grid alignment

What to use and when

minmax function

auto-fill

repeat(auto-fill, minmax(150px, 1fr));:

auto-fill vs. auto-fit

Centering using grid

.wrapper {
  display: grid;
  justify-content: center;
  align-content: center;
}

.wrapper {
  display: grid;
  place-content: center;
}

Sticky grids

Grid Quirks

Margin collapse

Z-index works with grid children

9: Animations

Transform(translate)

transform: translate(0px, 0px);
/* transform: translateY(20px);
   transform: translateX(20px); */

Transform(scale)

transform: scale(0.95);
transform: scale(1.55, 0.4);

Transform(rotate)

transform: rotate(122deg);
transform: rotate(0.5turn);

Transform(skew)

transform: skew(0deg);
transform: skewX(22deg);

Transform (origin)

transform: rotate(289deg);
/*
transform-origin: left top;
transform-origin: center;
transform-origin: 25px bottom;
transform-origin: 0% 150%;
*/

Combining multiple operations

Inline Elements

CSS Transitions

.btn {
  transition: transform 250ms, opacity 400ms;
}

.btn:hover {
  transform: scale(1.2);
  opacity: 0;
}

Timing functions

.btn {
  transition: transform 250ms;
  transition-timing-function: linear;
  /* transition: transform 250ms linear; */
}

Delays

.dropdown {
  opacity: 0;
  transition: opacity 400ms;
  transition-delay: 300ms;
  /* Shortcut
  transition: opacity 250ms 300ms; 
  */
}

.dropdown-wrapper:hover .dropdown {
  opacity: 1;
  transition: opacity 100ms;
  transition-delay: 0ms;
}

Note: Hover listeners should be attached to interactive elements as it is desirable to have the focus effect join the hover effect.

Keyframe Animations

@keyframes slide-in {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0%);
  }
}

Looped animations

Shorthand values for amimation property

.box {
  /*
  From this:
    animation: grow-and-shrink 2000ms;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;
    animation-direction: alternate;

  ...to this:
  */
  animation: grow-and-shrink 2000ms ease-in-out infinite alternate;
}

Fill Modes

@keyframes slide-in {
  from {
    transform: translateX(-100%);
    opacity: 0.25;
  }
  to {
    transform: translateX(0%);
    opacity: 1;
  }
}

.box {
  animation: slide-in 1000ms;
  animation-delay: 500ms;
  animation-fill-mode: backwards;
}

Dynamic Updates

Animations vs. transitions

keyframes and styled-components

Animation performance

The pixel pipeline

Action-Driven Animation

Orchestration

Accessibility

Motion vs. Animation

3D Transforms

Applying perspective

transform: rotateX(188deg);
perspective: 250px;
/* measure of how close the user
  is to the screen */

The perspective function

Rendering Contexts

transform-style: preserve-3d

9: Little Big Details

CSS Filters

Color Manipulation using filters

filter: brightness(150%);
filter: contrast(60%);
filter: sepia(100%);
filter: contrast(200%) grayscale(90%);

Hue rotation

Blur Filter

Backdrop Filters

Border Radius

Nested radius

Circular radius

Shadows

Box shadow

filter: drop-shadow

text-shadow

Contoured Shadows

Single-Sided Shadows

Inset shadows

%!&^*#$%#^*#(*#_(#)_)(#) !(*&#*(&

^*&@^*&^#*&)*#&()#&(&#(*&_(#())))

Replaced elements

Note:

Check