home blog portfolio Ian Fisher

CSS and HTML cheatsheet

top/right/bottom/left order

/* Apply to all four sides */
padding: 1em;

/* top and bottom | left and right */
padding: 5% 10%;

/* top | left and right | bottom */
padding: 1em 2em 2em;

/* top | right | bottom | left */
padding: 5px 1em 0 2em;


/* top-left-and-bottom-right | top-right-and-bottom-left */
border-radius: 10px 5%;

/* top-left | top-right-and-bottom-left | bottom-right */
border-radius: 2px 4px 2px;

/* top-left | top-right | bottom-right | bottom-left */
border-radius: 1px 0 3px 4px;

Variables

:root {
  --font-size: 14px;
}

p {
  font-size: var(--font-size);
}

Media queries

@media screen and (min-width: 800px) {
    .foo {
        /* ... */
    }
}

@media screen and (max-width: 799px) {
    .foo {
        /* ... */
    }
}

Scrollbar

.foo {
    overflow: auto;
    height: /* ... */
}

Right-align element

/*
<div class="container">
  <span></span>
  <span class="right"></span>
</div>
*/

.container {
    display: flex;
}

.right {
    margin-left: auto;
}

HTML boilerplate

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1"/>

    <link rel="icon" href="/static/favicon.ico">
</head>
</html>

Dark theme

@media (prefers-color-scheme: dark) {
  body {
    background-color: #222;
    color: #e0e0e0;
  }
}

Fancy button

.fancy-button {
  display: inline-block;
  text-align: center;
  padding: .3em .9em;
  cursor: pointer;
  background-color: #0074d9;
  color: #fff;
  border: 0;
  width: auto;
  box-shadow: 0 0 rgba(0,0,0,0) inset;
  border-radius: .2em;
  transition: all 0.3s;
  font-weight: normal;
  text-decoration: none;
}

.fancy-button:hover {
  box-shadow: inset 0 0 0 99em rgba(255,255,255,0.2);
}

UI polish

:root {
  --page-background-dark: #1a1a1a;
  --card-background-dark: #2d2d2d;
  --card-border-dark: #404040;
  --blue: #0d6efd;
  --text-color: #e0e0e0;
  --hover-background-dark: var(--card-border-dark);
}

.glow {
  transition: all 0.2s ease;
  border-color: #ffc107;
  box-shadow: 0 2px 12px rgba(255,193,7,0.4);
  background-color: #3a3520;
}

.card {
  transition: all 0.2s ease;
}

.card:hover {
  border-color: var(--blue);
  box-shadow: 0 2px 8px rgba(13,110,253,0.3);
}

.button {
  transition: all 0.2s ease;
}

.button:hover {
  transform: scale(1.1);
  background-color: var(--hover-background-dark);
}

.loading-spinner {
  width: 80px;
  height: 80px;
  border: 8px solid var(--card-border-dark);
  border-top: 8px solid var(--blue);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.error {
  border: 1.5px solid rgba(255, 99, 99, 0.3);
  background-color: rgba(255, 99, 99, 0.15);
  backdrop-filter: blur(10px);
}

See also