Aligning Text and Icons Using CSS Display
The CSS display property is essential for aligning text and icons. By choosing the right display value for the container and its children, you can achieve proper horizontal and vertical alignment.
display: inline-flex – Allows elements to sit inline while providing flex alignment for children. Perfect for icons next to text without breaking line flow.
display: flex – Makes a container a flex box. Use align-items: center to vertically align text and icons.
display: inline-block – Lets elements flow inline while controlling vertical alignment using vertical-align.
display: grid – Provides a two-dimensional layout for precise horizontal and vertical alignment of text and icons.
Here, the icon and text are vertically centered using inline-flex and align-items: center. The gap property adds spacing between them.
Use flex or inline-flex to align icons and text horizontally and vertically.
Use align-items: center to vertically center elements in a flex container.
Use gap for consistent spacing instead of individual margins.
For more complex layouts, grid can precisely align multiple icons and text elements.
Avoid mixing inline-block and flex in the same container to prevent misalignment.
You have a button with an SVG icon and a label. How would you use CSS display properties to place the icon left of the text and keep them vertically centered?
If you set the container to display:inline-block, what effect does that have on the alignment of an inline icon and its accompanying text?
What happens when you use display:table-cell for the icon and the text elements, and when would that be useful?
Our navigation bar mixes items with and without icons. After switching the container to display:flex, some icons appear misaligned in Chrome. How would you debug and fix the alignment?
The design spec says the icon must be 24 px tall and the text baseline should line up with the icon’s vertical center. Which display value and CSS properties would you choose, and why?
Why might using display:inline-block on the icon introduce extra whitespace, and how would you eliminate that whitespace?
You’re leading a redesign of a shared component library that includes buttons, inputs, and cards with icons. How would you standardize alignment across the library using display values while supporting legacy browsers and theming?
A performance audit shows that using display:flex on many nested components adds layout thrashing on mobile. What trade‑offs would you consider, and how might you restructure the CSS to keep alignment correct but reduce reflow?
When integrating a third‑party icon font, the icons appear offset from the text in some RTL locales. How would you adjust the display strategy to handle LTR/RTL and maintain pixel‑perfect alignment?
Our monolithic app has thousands of hard‑coded tables and inline icons. What architectural approach would you take to replace those display:inline/table hacks with a consistent flex‑based alignment strategy, and how would you roll it out without breaking existing pages?
How would you design a CSS‑in‑JS theming solution that enforces correct icon‑text alignment across multiple product teams while still allowing custom overrides of display values?
If we need to support both legacy IE11 and a new React Native Web build, how would you reconcile differing display capabilities to ensure icons and text stay aligned across platforms?