AI CSS Animations

Code Block Hover Effect

A code snippet where the line under the pointer lights up with an accent edge and number, and a copy button slides in once you hover the block. Line numbers come from a CSS counter.

Also known as: code line highlight on hover, copy code button on hover, line numbers hover css.

.card {  display: grid;  gap: 1rem;  transition: transform 0.3s;}

HTML

<figure class="hfx-code-block-lines">
  <button aria-label="Copy code"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V5.5A1.5 1.5 0 0 0 14.5 4h-9A1.5 1.5 0 0 0 4 5.5v9A1.5 1.5 0 0 0 5.5 16H8"/></svg></button>
  <pre><code><span><b>.card</b> {</span><span>  <i>display</i>: <u>grid</u>;</span><span>  <i>gap</i>: <u>1rem</u>;</span><span>  <i>transition</i>: <u>transform 0.3s</u>;</span><span>}</span></code></pre>
</figure>

CSS

.hfx-code-block-lines {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  position: relative;
  width: 18em;
  margin: 0;
  overflow: hidden;
  border-radius: 0.8em;
  background: #0f172a;
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--c) 30%, transparent);
  color: #e2e8f0;
}
.hfx-code-block-lines pre {
  margin: 0;
  padding: 0.7em 0;
  counter-reset: line;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
  line-height: 1.7;
}
.hfx-code-block-lines code > span {
  display: block;
  padding-right: 1em;
  counter-increment: line;
  white-space: pre;
  transition: background-color 0.15s ease, box-shadow 0.15s ease;
}
.hfx-code-block-lines code > span::before {
  content: counter(line);
  display: inline-block;
  width: 1.6em;
  margin-right: 1.1em;
  color: #475569;
  text-align: right;
  transition: color 0.15s ease;
}
.hfx-code-block-lines b { color: color-mix(in srgb, var(--c) 45%, #fff); font-weight: 600; }
.hfx-code-block-lines i { color: color-mix(in srgb, var(--c) 30%, #f0abfc); font-style: normal; }
.hfx-code-block-lines u { color: color-mix(in srgb, var(--c) 20%, #fcd34d); text-decoration: none; }
.hfx-code-block-lines code > span:hover {
  background: color-mix(in srgb, var(--c) 20%, transparent);
  box-shadow: inset 0.2em 0 0 var(--c);
}
.hfx-code-block-lines code > span:hover::before { color: color-mix(in srgb, var(--c) 50%, #fff); }
.hfx-code-block-lines > button {
  position: absolute;
  top: 0.55em;
  right: 0.55em;
  display: grid;
  place-items: center;
  width: 2em;
  height: 2em;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 0.45em;
  background: rgba(255, 255, 255, 0.08);
  color: #cbd5e1;
  cursor: pointer;
  opacity: 0;
  transform: translateY(-0.35em);
  transition: opacity 0.2s ease, transform 0.25s cubic-bezier(0.3, 0.7, 0.4, 1), background-color 0.15s ease;
}
.hfx-code-block-lines > button svg { width: 1em; height: 1em; }
.hfx-code-block-lines:is(:hover, :focus-within) > button {
  opacity: 1;
  transform: none;
}
.hfx-code-block-lines > button:is(:hover, :focus-visible) {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
}

@media (prefers-reduced-motion: reduce) {
  .hfx-code-block-lines, .hfx-code-block-lines *, .hfx-code-block-lines::before, .hfx-code-block-lines::after, .hfx-code-block-lines *::before, .hfx-code-block-lines *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --hfx-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --hfx-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 16px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar hover effects