DEV Community

Cover image for CSS Tip to align icons with text
Baby Wolf Codes
Baby Wolf Codes

Posted on

2

CSS Tip to align icons with text

Did you know about the "cap" unit in CSS? 1cap is equal to the height of a capital letter (relative to current font size).

This is especially helpful to align icons to accompanying text, such as inside button - by setting the icon height to 1cap.

Example with a gear icon aligned to a text

HTML

<div class="container">
  <div class="icon"><!-- icon --></div>
  <div class="text">Settings</div>
</div>
Enter fullscreen mode Exit fullscreen mode

CSS

.container {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}
.container .icon {
  height: 1cap;
  aspect-ratio: 1;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay