DEV Community

Software Developer
Software Developer

Posted on

50 Most Useful CSS3 Snippets

Basics & Typography

1. Reset Margin and Padding

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

2. Font Family Setup

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

3. Custom Font Import

@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

body {
  font-family: 'Roboto', sans-serif;
}

4. Text Centering

.center-text {
  text-align: center;
}

5. Text Shadow

h1 {
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

Colors & Backgrounds

6. Gradient Background

.background-gradient {
  background: linear-gradient(45deg, #6a11cb, #2575fc);
}

7. Background Image Cover

.hero {
  background-image: url('hero.jpg');
  background-size: cover;
  background-position: center;
}

8. Opacity Control

.transparent {
  opacity: 0.7;
}

9. Box Shadow

.card {
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

10. CSS Variables

:root {
  --main-color: #3498db;
}

.button {
  background-color: var(--main-color);
}

Layout & Flexbox

11. Flex Container

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

12. Flex Item Grow

.flex-item {
  flex-grow: 1;
}

13. Centering with Flexbox

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

14. Flex Wrap

.flex-wrap {
  display: flex;
  flex-wrap: wrap;
}

15. Nesting Flex Items

.parent {
  display: flex;
}

.child {
  flex: 1 1 auto;
}

Grid Layout

16. Basic Grid Setup

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 10px;
}

17. Named Grid Areas

.grid-container {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar content content"
    "footer footer footer";
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer { grid-area: footer; }

18. Auto-Fit Columns

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

19. Align Items in Grid

.grid-container {
  display: grid;
  align-items: center;
  justify-items: center;
}

20. Grid Row Span

.grid-item {
  grid-row: span 2;
}

Buttons & Interactions

21. Simple Button Style

.button {
  background-color: #007bff;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

22. Button Hover Effect

.button:hover {
  background-color: #0056b3;
}

23. Smooth Transitions

.transition {
  transition: all 0.3s ease;
}

24. Disabled Button

.button:disabled {
  background-color: grey;
  cursor: not-allowed;
}

25. Focus Outline Removal (Use With Caution)

.button:focus {
  outline: none;
}

Animations & Effects

26. Fade In Animation

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.fade-in {
  animation: fadeIn 1s ease forwards;
}

27. Bounce Animation

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

.bounce {
  animation: bounce 2s infinite;
}

28. Rotate Animation

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

.rotate {
  animation: rotate 2s linear infinite;
}

29. Hover Grow Effect

.grow:hover {
  transform: scale(1.1);
  transition: transform 0.3s ease;
}

30. Text Blink Animation

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.blink {
  animation: blink 1s step-start infinite;
}

Responsive Design

31. Media Query Example

@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }
}

32. Responsive Image

img {
  max-width: 100%;
  height: auto;
}

33. Hide on Mobile

@media (max-width: 600px) {
  .desktop-only {
    display: none;
  }
}

34. Fluid Typography

h1 {
  font-size: clamp(1.5rem, 2vw, 2.5rem);
}

35. Container Max Width

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

Shadows & Borders

36. Rounded Corners

.rounded {
  border-radius: 10px;
}

37. Circular Image

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
}

38. Border Gradient

.border-gradient {
  border: 5px solid;
  border-image-slice: 1;
  border-width: 5px;
  border-image-source: linear-gradient(to left, #743ad5, #d53a9d);
}

39. Inset Box Shadow

.inset-shadow {
  box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.3);
}

40. Outline on Focus

input:focus {
  outline: 2px solid #4caf50;
}

Miscellaneous

41. Cursor Pointer

.pointer {
  cursor: pointer;
}

42. Hide Scrollbar (Webkit)

.hide-scrollbar::-webkit-scrollbar {
  display: none;
}

43. Text Overflow Ellipsis

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

44. Vertical Align Middle

.vertical-middle {
  display: inline-block;
  vertical-align: middle;
}

45. CSS Clip Path Circle

.clip-circle {
  clip-path: circle(50%);
}

Programmers Known Features

46. CSS Grid with Fraction Units (fr)

.grid-fr {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
}

47. CSS Variables with Fallback

.element {
  color: var(--primary-color, blue);
}

48. Centering Absolute Element

.absolute-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

49. Flexbox Column Reverse

.flex-column-reverse {
  display: flex;
  flex-direction: column-reverse;
}

50. Clamp for Responsive Spacing

.spacing {
  padding: clamp(10px, 2vw, 20px);
}

Top comments (0)