Skip to main content
CSS

CSS #

My notes on CSS & HTML from ages ago (in Japanese): notes: html 5 & css 3

Colours #

mix-blend-mode - CSS: Cascading Style Sheets | MDN

Methodologies #

Atomic CSS #

BEM #

Refs:

Queries #

Refs:

Snippets #

Centre something #

Ref: What does 100% mean in CSS?

.self {
  position: absolute;
  top: 50%;
  left: 50%;
  translate-top: -50%;
  translate-left: -50%;
  transform: translate(-50%, -50%);
}

Descriptive list in grid #

dl {
  display: grid;
  gap: 0.5rem 1rem;
  align-items: end;
}

dl dt {
  margin-top: 0;
  grid-column: 1;
}

dl dd {
  margin-bottom: 0;
  margin-left: 1rem;
}

@media screen and (min-width: 900px) {
  dl {
    grid-template-columns: fit-content(50%) auto;
  }

  dl dd {
    grid-column: 2;
    margin-left: 0;
  }
}

Half-height highlight #

Method 1:

mark {
  background: linear-gradient(to top, Mark 40%, transparent 40%);
}

Method 2:

mark {
  background-color: unset;
  background-position: bottom;
  background-size: 100% 40%;
  background-repeat: no-repeat;
  background-image: linear-gradient(0deg, Mark, Mark);
  /* can combine any other gradient, like:
  background-image: linear-gradient(5deg, Mark 0%, Mark 75%, transparent 100%);
  */
}

Image cropping #

Ref: How To Scale and Crop Images with CSS object-fit | DigitalOcean

For <img src="..."> elements:

img {
    height: 450px;
    object-fit: cover;
    width: 800px;
    object-position: 0 80%; /* x: centring; y: align to the top */
}

Responsive iframe #

New version (aspect-ratio) #

.video-container {
  width: 100%; /* this is not the actual width */
}

.video {
  width: 95%; /* this is the actual width */
}

.video--16x9{ aspect-ratio: 16/9; }
.video--4x3{ aspect-ratio: 4/3; }

Usage:

<div class="video-container">
  <iframe
  class="video video--4x3"
  src="https://www.youtube-nocookie.com/embed/<video_id>?cc_load_policy=1&cc=1" allowfullscreen title="YouTube video" frameborder="0" sandbox="allow-same-origin allow-scripts" loading="lazy"></iframe>
</div>

Old version (padding-bottom) #

Ref: Responsive iframes — The Right Way (CSS Only)! | Ben Marshall

.video-container{
  position: relative;
  height: 0;
}
    
.video-container--4x3{ padding-bottom: 75%; }
.video-container--16x9{ padding-bottom: 56.25%; }

.video-container iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

Usage:

<div class="video-container video-container--4x3">
  <iframe src="https://www.youtube-nocookie.com/embed/<video_id>?cc_load_policy=1&cc=1" allowfullscreen title="YouTube video" frameborder="0" sandbox="allow-same-origin allow-scripts" loading="lazy"></iframe>
</div>

Random shadow animation #

…that I somehow made for something.

.shadow {
  box-shadow: 0 .5rem 0 rgba(0,0,0,.15);
  transition: transform 0.2s, box-shadow 0.2s;
}

.shadow:hover {
  box-shadow: 0 0.7rem 0 rgba(0,0,0,.2);
  transform: translateY(-0.2rem);
  transition: transform 0.2s, box-shadow 0.2s;
}

Skip-to-main button #

Credit: Hidden content for better a11y | Go Make Things

CSS:

.screen-reader {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

.screen-reader-focusable:active,
.screen-reader-focusable:focus {
  display: block;       /* added */
  text-align: center;   /* added */
  clip: auto;
  height: auto;
  margin: 0;
  overflow: visible;
  position: static;
  white-space: normal;
  width: auto;
}

Usage:

<body>
  <a class="screen-reader screen-reader-focusable" href="#main">Skip to the main content</a>
  <nav>Navigation elements...</nav>
  <main id="main">The main content</main>
  ...
</body>

SVG words watermark background #

Ref: Watermark text repeated diagonally css/html - Stack Overflow

body {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='350' width='350'><text transform='translate(50, 300) rotate(-45)' fill='WhiteSmoke' font-size='100' font-weight='bold'>DRAFT</text></svg>");
}

SVG code: (see EPUB editing #Image sizing for a longer expedition of what properties should be what values & with or without units)

<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='350' width='350'>
  <text transform='translate(50, 300) rotate(-45)' fill='WhiteSmoke' font-size='100' font-weight='bold'>DRAFT</text>
</svg>

WordPress quirks #

Ref: my brain and this page

/* no space around br */
/* margin of p is 1.5rem */
.wp-block-post-content br {
  display: block;
  content: " ";
  margin-bottom: .5rem;
}
/* pictures too wide on mobile */
.wp-block-post-content img {
  max-width: 100%;
  height: auto !important; /* needs important here for some new block templates */
}
/* youtube iframe too wide on mobile */
.wp-block-embed__wrapper {
  width: 100%;
  aspect-ratio: 16/9;
}

.wp-block-embed__wrapper iframe {
  width: 100%;
  height: 100%;
}

(Maybe) Useful resources #

Guides #

Exercises #

Inspiration #

Mondrian #

Brutalism #

Tools/Generators #

Moved to 工具箱. Also see: Web #Tools; Other Online Tools

Entertainment #