Skip to content

Latest commit

 

History

History
390 lines (318 loc) · 9.99 KB

css.md

File metadata and controls

390 lines (318 loc) · 9.99 KB

CSS

Gradients

Patterns

Tips & Tricks

CSS Variables

:root {
  --primary: #0f0;
  --secondary: #fff;
}
html {
  font-family: Arial, Helvetica, sans-serif;
  color: var(--primary);
}
html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}

modern-normalize already applies this.

* {
  margin: 0;
  padding: 0;
}
p.legibility {
  text-rendering: optimizeLegibility;
}
p.speed {
  text-rendering: optimizeSpeed;
}
/* maybe not performant */
body {
  text-rendering: optimizeLegibility;
}
.row {
  max-width: 1140px;
}

Align icons and text

.icon-small {
  display: inline-block;
  width: 30px;
  text-align: center;
  font-size: 120%;
  margin-right: 10px;
  /* trick */
  line-height: 120%;
  vertical-align: middle;
  margin-top: -5px;
}

Fixed background image (cool effect)

.section-with-bg-image {
  background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)),
    url(img/bg.jpg);
  background-size: cover;
  color: white;
  background-attachment: fixed;
}

White box with hollow text over a background image

h1.white {
  background: white;
  color: black;
  mix-blend-mode: screen;
}

Black box with hollow text over a background image

h1.black {
  background: black;
  color: white;
  mix-blend-mode: multiply;
}

Title with image as text color

h1.image {
  background-image: url(//unsplash.it/800);
  background-size: cover;
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}

Center text on page

body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

Render QR or thumbnails

img {
  image-rendering: pixelated;
}

Fluid font size

:root {
  font-size: calc(1vw + 1vh + 0.5vmin);
}

Clearfix

/* old way */
.clearfix {
  overflow: auto;
}
/* new way */
.clearfix::after {
  content: '';
  clear: both;
  display: table;
}
/* future way */
.clearfix {
  display: flow-root;
}

H1 with logo (SEO)

<h1 class="logo">My Company and what we do</h1>
.logo {
  position: relative;
  height: 50px;
  width: 50px;
  overflow: hidden;
}
.logo:after {
  content: url(logo.jpg);
  position: absolute;
  top: 0;
  left: 0;
}

Image tips

  • img width and height in percentage units does not work if img is inside an inline element. Wrapping element has to be changed to inline-block
  • if img does not fit well in the wrapping div (white stripe at the bottom), use vertical-align: bottom or modify the wrapping tag to be displayed as inline-block or block.

Responsive design without queries (using grid)

main {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(19rem, 1fr));
  /* grid-gap: 1rem; */
}

Block element only properties

  • width
  • margin: auto

Screen readers only content

.sr-only {
  border: 0;
  clip: rect(0 0 0 0);
  clip-path: polygon(0px 0px, 0px 0px, 0px 0px);
  -webkit-clip-path: polygon(0px 0px, 0px 0px, 0px 0px);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
  white-space: nowrap;
}

Shorter way to make responsive width

width: 70%;
max-width: 500px;

can be done shorter way:

width: min(500px, 70%);

Besides min(), there are also max() and clamp() functions.

Problem with no background color after inside elements set to float

This happens because a wrapping block element collapses when all inside block elements are float (to left or right). Solution for this is to set overflow: auto to the parent container.

Properly display image in any rectangle area (show image central area not disturbing aspect ratio)

object-fit: cover;

Push footer to the page bottom

On a short page, html actually may not occupy the whole viewport height so when using percentages, it is important to use them for html element as well. Using percentages in this case gives better compatibility with smaller devices compared to wh.

html {
  height: 100%;
}
body {
  min-height: 100%;
}
footer {
  margin-top: auto;
}

Common bootstrap

@import url(//cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css);

:root {
  font-size: calc(1vw + 1vh + 0.5vmin);
}

html {
  box-sizing: border-box;
}

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

* {
  margin: 0;
  padding: 0;
}

body {
  height: 100vh;
  /* max-width: 1140px; */
  display: flex;
  justify-content: center;
  align-items: center;
  text-rendering: optimizeLegibility;
}

Selective load of CSS (HTML media Attribute)

<link rel="stylesheet" type="text/css" href="print.css" media="print">
<link rel="stylesheet" type="text/css" href="mobile.css" media="screen and (max-width: 768)">

Smooth Scroll-to-top Button

html {
  scroll-behavior: smooth;

  @media (prefers-reduced-motion: reduce) {
    scroll-behavior: auto;
  }
}
<button
  onClick={() => {
    window.scrollTo(0, 0);

    // focus management
    const title = document.getElementById('article-title');
    title.focus();
  }}
>
  Scroll to top
</button>