Skip to content

Latest commit

 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

CSS Tips

Collection of CSS tips

Selectors

Positioning

inset

inset allows to specify top, right, bottom and left with a single CSS line. Documentation

anchor()

image

<body>
  <div id="box1"></div>
  <div id="box2" anchor="box1"></div>
</body>
. {
  position: absolute;
  bottom: anchor(top);
  left: anchor(center);
}

Reference video

@position-try (fallback positioning)

Dropdown items positioning example (Video):

image

Scrolling

Ability to provide scrolling features (carousel, table of content links) without the need for JavaScript:

Sizing

  • min
  • max
  • clamp, combine width, min-width and max-width into a single command
  • minmax
  • stretch (better alternative to 100%). Ref
  • min-block-size: 100svh (for body, to make sure that it uses at least the full page height)

Text

Responsive font-size

font-size: clamp(1.8rem, calc(10 + 1rem), 5vw);

Youtube Short

Avoid wrapping text

You can use min-width: fit-content; instead of white-space: nowrap; Reference Video

Text Gradients

image

Can't directly use color: linear-gradient. Instead we can use this tip:

h1 {
  background: linear-gradient(to right, red, blue); /* apply gradient to background */
  background-clip: text; /* limit background painting area */
  color: transparent; /* make text transparent */
}

Counters

:root {
  counter-reset: headings; /* headings is a name */
}
h2 {
  counter-increment: headings; /* increment counter via `counter-increment` */
}
h2:before {
  content: counter(headings); /* show the value of the counter prior to the element
}

Allows styled counters: image

Auto increasing inputs (textarea)

textarea {
  field-sizing: content;
}

Official Doc. Youtube Video

Typewriter animation

p {
  margin-inline: auto;
  overflow: hidden;
  white-space: nowrap;
  dorder-right: 1px solid;
  animation: typing 3s steps(22) forwards, blink 1s step-end infinite;
}

@keyframes typing {
  from { width: 0; }
  to { width: 100% }
}
@keyframes blink {
  50% { border-color: transparent; }
}

Youtube Short

Transitions/Animations

Custom CSS transition timings/curves

Websites to create your own curves:

Transition to and from display: none

Youtube video

Circular outset animation

background: conic-gradient (color 100%, transparent 100%);
image

Page Transitions (multiple examples, including Pixelate dissolve)

Reference

image

Scrolling/Snapping

Snap scrolling:

.wrapper {
  display: flex;
  gap: 20px;
  width: 300px;
  overflow-x: scroll;
  scroll-snap-type: x mandatory; /* children need to define `scroll-snap-align`. Could also use `proximity` */
  .card {
    scroll-snap-align: center;
    box-sizing: border-box;
    flex-shrink: 0;
    width: 300px; /* needs to be the same size as the wrapper */
  }
}

image

Scroll Animations

animation: <name>;
animation-timeline: view();
animation-range: entry 0% cover 40%;
image

Youtube Video

Images

CSS Reset

Minimal Reset

Reference

*, *::before, *::after {
  box-sizing: border-box;
}

* {
  margin: 0;
  padding: 0;
  font-inherit;
}

html {
  color-scheme: dark light;
  scrollbar-gutter: stable;
}

body {
  /*min-height: 100vh;*/
  min-block-size: 100svh;
}

img, picture, svg, video {
  display: block;
  max-width: 100%
}

About

Collection of CSS tips

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors