Figma导出的SVG在网页上显示错位,如何解决?
最近在用Figma导出SVG图标到项目里,但发现有些图标在页面上显示位置总是不对,比如文字和图形不在同一水平线。我按设计稿导出的SVG尺寸是24×24,但实际渲染时却溢出容器了。
我尝试用JS动态插入SVG,设置了width/height样式,但没用:
const svgContainer = document.getElementById('icon-wrapper');
const svgUrl = 'path/to/icon.svg';
fetch(svgUrl)
.then(response => response.text())
.then(svgData => {
const parser = new DOMParser();
const svg = parser.parseFromString(svgData, 'image/svg+xml').documentElement;
svg.setAttribute('style', 'width:24px;height:24px');
svgContainer.appendChild(svg);
});
检查原始SVG代码发现有viewBox属性,但数值和设计稿画板尺寸不一致。是不是Figma导出时需要特别设置?或者代码里需要处理viewBox和尺寸的关系?
如果不想改SVG内容,代码里也可以动态处理,比如: