MyInternships.in

CSS Examples & Practice

CSS Snippets

These are the small CSS recipes you reach for again and again. Each snippet solves one common problem and comes with a one-line explanation. Copy the code, drop it into your stylesheet, and adjust the values to fit your design.


1. Center anything (grid)

The shortest way to center a child both horizontally and vertically.

.center {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

2. Center with flexbox

The flexbox equivalent, useful when you also need to space multiple children.

.center-flex {
  display: flex;
  align-items: center;
  justify-content: center;
}

3. Truncate text with an ellipsis

Cut off a single line of overflowing text and add three dots.

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

4. Clamp text to multiple lines

Limit a paragraph to a fixed number of lines, then ellipsis.

.clamp-3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

5. Custom scrollbar

Style the scrollbar in WebKit browsers (Chrome, Edge, Safari).

.scroll::-webkit-scrollbar { width: 10px; }
.scroll::-webkit-scrollbar-track { background: #f1f5f9; }
.scroll::-webkit-scrollbar-thumb {
  background: #94a3b8;
  border-radius: 999px;
}

6. Gradient text

Paint text with a gradient by clipping the background to the text.

.gradient-text {
  background: linear-gradient(90deg, #06b6d4, #8b5cf6);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

7. Fixed aspect-ratio box

Keep an element at a set ratio (great for video thumbnails).

.ratio-16-9 {
  aspect-ratio: 16 / 9;
  width: 100%;
  background: #e2e8f0;
}

8. Responsive auto-fit grid

A grid whose columns reflow automatically with no media queries.

.auto-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

9. Sticky footer

Push the footer to the bottom even when content is short.

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

10. Visually hidden (accessible)

Hide content visually but keep it for screen readers.

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

11. Smooth focus ring

A clear, accessible focus outline for keyboard users only.

:focus-visible {
  outline: 3px solid #6366f1;
  outline-offset: 2px;
  border-radius: 4px;
}

12. Respect reduced motion

Turn off animations for users who prefer less motion.

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
💡

Snippets are starting points — always adjust colors, sizes and vendor prefixes to match your project and browser targets.

Related CSS Topics

Keep learning with these closely related tutorials.

Ready to use your CSS skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships