Bookmarked CSS

Page Container

Basic

limit width of page content for desktop and center it

.container {
  padding: 0 20px;
  background-color: #333;
}

.content {
  max-width: 1170px;
  margin: 0 auto;
}

Advanced

such implementation economizes one wrapper, however it is not possible to set special background color on full screen width in such container

.container {
  width: calc(100% - 20px);
  max-width: 960px;
  margin: 0 auto;
}

there is a common issue when footer jumps to the middle of the page, when there is no enough content in main part. this implementation fixes this

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

header {
  flex-shrink: 0;
}

main {
  flex-grow: 1;
  flex-shrink: 0;
}

footer {
  flex-shrink: 0;
}

only make sure to toggle overflow when modal is opened and closed

body {
  overflow: hidden;
}

.modal-container {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 10;
  overflow-y: auto;
  background-color: rgba(0, 0, 0, 0.8);
}

Center Vertically Without Flex

this is a common trick from past. is often asked for by old-school interviewers ;)

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

Switch

like the one for light mode toggling on this website. just make sure to replace checkbox with JS logic

<input type="checkbox" />
<button type="button">
  <span>🤝</span>
  <span>👎</span>
  <div />
</button>
button {
  position: relative;
  display: flex;
  justify-items: space-between;
  align-items: center;
  column-gap: 1.5rem;
  width: max-content;
  padding: 0.5rem 1rem;
  background-color: lightgrey;
  border-radius: 999px;

  span {
    z-index: 1;
    padding: 0 0.25rem;
    font-size: 1.5rem;
    line-height: 1;
    transition: transform 0.7s;
  }

  div {
    position: absolute;
    width: 2rem;
    height: 2rem;
    background-color: purple;
    border-radius: 50%;
    transition: transform 0.3s;
  }
}

input:checked + button {
  span {
    transform: rotate(360deg);
  }

  div {
    transform: translateX(3.5rem);
  }
}

Border

Dotted Reversed

nice visual solution: 2 samely colored sections are divided from each other by dotted border, and division happens not via dots, but via spacings between dots

.div1,
.div3 {
  height: 50px;
  margin: -1px 0;
  background-color: pink;
}

.div2 {
  border-top: 5px dotted pink;
}

Animations

Button Border

<button class="button"">
  hover me
  <span class="top"></span>
  <span class="right"></span>
  <span class="bottom"></span>
  <span class="left"></span>
</button>
button {
  position: relative;
}

.top,
.right,
.bottom,
.left {
  position: absolute;
  background-color: currentColor;
  transition: transform 0.3s;
}

.top,
.bottom {
  left: 0;
  width: 100%;
  height: 1px;
  transform: scaleX(1);
}

.top {
  top: 0;
  transform-origin: left center;
}

.bottom {
  bottom: 0;
  transform-origin: right center;
}

.left,
.right {
  top: 0;
  width: 1px;
  height: 100%;
  transform: scaleY(0.6);
}

.left {
  left: 0;
  transform-origin: top center;
}

.right {
  right: 0;
  transform-origin: bottom center;
}

button:hover .top,
button:hover .bottom {
  transform: scaleX(0.6);
}

button:hover .right,
button:hover .left {
  transform: none;
}

Button Border 2

button {
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: -4px;
    left: 0;
    z-index: -1;
    background-color: green;
  }

  &::after {
    content: '';
    position: absolute;
    bottom: -4px;
    right: 0;
    z-index: -1;
    background-color: green;
  }

  &:hover::before,
  &:hover::after {
    animation-name: button;
    animation-duration: 0.6s;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
  }
}

@keyframes button {
  0% {
    width: 0;
    height: 4px;
  }
  70% {
    width: calc(100% + 4px);
    height: 4px;
  }
  100% {
    width: calc(100% + 4px);
    height: calc(100% + 8px);
  }
}

Link Underline

a {
  position: relative;

  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: currentColor;
    transform: scaleX(0);
    transform-origin: left center;
    transition: transform 0.3s;
  }

  &:hover::after {
    transform: scaleX(1);
    transform-origin: right center;
  }
}

Hidden Content

this CSS animates opacity to show hidden element when some parent element is hovered

section {
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0s 0.3s;
}

article:hover section {
  opacity: 1;
  visibility: visible;
  transition-delay: 0s;
}

1 Highlight for 2 Elements

<main>
  <div>hello</div>
  <div>world</div>
</main>
main {
  display: flex;

  > div {
    flex-basis: 20%;
  }
}

div {
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
    width: 100%;
    height: 100%;
    background-color: orange;
    transform: scaleX(0);
    transform-origin: right;
    transition: transform 0.6s;
  }

  &:hover::before {
    transform: scaleX(1);
  }

  &:nth-child(2)::before {
    transform-origin: left;
  }
}

This website uses cookies to enhance user's experience