Skip to content

lluchez/css-tips

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

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

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

Text

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

Transitions

Custom CSS transition timings/curves

Websites to create your own curves:

Transition to and from display: none

Youtube video

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

Images

CSS Reset

Minimal Reset

Reference

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

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

html {
  color-scheme: dark light;
}

body {
  min-height: 100vh;
}

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

About

Collection of CSS tips

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors