DEV Community

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

Posted on

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)