Home > Web Front-end > CSS Tutorial > CSS Tip to align icons with text

CSS Tip to align icons with text

Barbara Streisand
Release: 2025-01-25 20:07:10
Original
635 people have browsed it

Discover the CSS cap unit: a powerful tool for precise typographic alignment! One cap unit equals the height of a capital letter in the current font. This is particularly useful for aligning icons with text, as demonstrated below.

CSS Tip to align icons with text

HTML & CSS

Here's how to use it: Set the icon's height to 1cap for seamless integration with adjacent text, such as within buttons. The example below uses flexbox for easy alignment.

<code class="language-html"><div class="container">
  <img class="icon" src="gear-icon.svg" alt="Gear Icon">
  <h3>CSS</h3>
</div></code>
Copy after login
<code class="language-css">.container {
  display: flex;
  align-items: baseline; /* Aligns items to the baseline of the text */
  gap: 0.5rem;          /* Adds spacing between icon and text */
}

.container .icon {
  height: 1cap;
  aspect-ratio: 1;      /* Maintains the icon's aspect ratio */
}</code>
Copy after login

This technique ensures consistent vertical alignment regardless of font size changes, resulting in cleaner and more professional-looking layouts.

The above is the detailed content of CSS Tip to align icons with text. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template