作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Stackblitz link here.
所以我在应用程序组件中使用一次加载的全局图标定义,然后使用这个可重用的图标在整个应用程序中指向它。
在大多数情况下,它运行良好且很好,但出于某种原因,我注意到 SVG 的位置在图标组件的边界之外,而是显示为 SVG 底部的一些空 div,截图:
这是相关的图标组件代码:
<svg
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
[style.width]="width ? width : size"
[style.height]="height ? height : size"
[style.margin]="margin ? margin : '0'"
[style.padding]="padding ? padding : '0'"
>
<use [style.fill]="color ? color : 'white'" [attr.xlink:href]="absUrl + '#' + name"></use>
</svg>
import { Component, ChangeDetectionStrategy, Input } from "@angular/core";
@Component({
selector: "icon",
templateUrl: "./icon.component.html",
styleUrls: ["./icon.component.scss"],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class IconComponent {
@Input() name: string;
@Input() size: string;
@Input() color: string;
@Input() width?: string;
@Input() height?: string;
@Input() margin?: string;
@Input() padding?: string;
get absUrl() {
return window.location.href.split("#")[0];
}
}
最佳答案
Svg 是一个内联元素。内联元素留下空白。如果您在 svg 元素或图标组件上设置 display:block 它将被修复。
试试这个:
icon.component.scss
:host{
display: block;
}
关于css - 显示 SVG 图标的 Angular 可重用组件的宽度/高度未设置在任何位置并位于图标正下方,stackblitz 位于内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62216712/
我是一名优秀的程序员,十分优秀!