DEV Community

Cover image for Why a 20px SVG icon shrinks in a flex sidebar
Svg/icons
Svg/icons

Posted on

Why a 20px SVG icon shrinks in a flex sidebar

The Overview icon looked fine. The icon beside a longer translated label looked smaller, even though both SVGs had a width of 20px.

I put the same SVG in two flex rows and measured the rendered width in Chromium. Each row had 176px of available width, an 8px gap and a 16px label. The long label was allowed to wrap.

Label Default flex behavior With flex: none on the SVG
Overview 20px 20px
Workspace permissions 17.7px 20px
Arbeitsbereichsberechtigungen 14.0px 20px

The SVG and the label are flex items. When space runs short, the flex layout can shrink the SVG even with an explicit width. The viewBox stays the same while the box on screen gets narrower.

.nav-link {
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav-link svg {
  width: 20px;
  height: 20px;
  flex: none;
}

.nav-label {
  min-width: 0;
  overflow-wrap: anywhere;
}
Enter fullscreen mode Exit fullscreen mode

flex: none keeps the icon at its specified size. The label can still wrap, including long words without spaces. Try it with your longest navigation label at the narrowest width your sidebar supports. In DevTools, select the rendered SVG and check $0.getBoundingClientRect().width.

Open the dashboard SVG used in the test on SVGicons.

Icon artwork by Lucide Contributors under the Lucide license.

Top comments (0)