javascript - 缩放 svg 背景图像以适合容器

标签 javascript jquery html css svg

我有一个简单的 div 容器,具有一定的高度和宽度。我需要一个 svg 图像作为 div 的 ::after 的背景图像。

这些是我的::after的样式

    &:after{
        content:'';
        background-image: url('../images/svg/triangle_blue.svg');
        background-repeat: no-repeat;
        background-size: auto auto;
        position:absolute;
        right: 0;
        top: 0;
        transform: translateX(100%);
        display:inline-block;
        width: 50px;
        height: 100%;
    }

这就是我的 svg 头部的样子:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewbox="0 0 50 100" width="100%" height="100%" preserveAspectRatio="none">

现在我希望 svg 始终缩放比例以适合 ::after 容器。 我错过了什么?

最佳答案

找到了一个可行的解决方案。这是我现在的 svg 代码:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="Ebene_1" x="0px" y="0px" viewBox="0 0 50 100" width="100%" height="100%" enable-background="new 0 0 50 100" xml:space="preserve" preserveAspectRatio="none"><g><polygon fill="#009EE0" points="0 100 50 50 0 0 "/></g></svg>

CSS:

background-size: auto auto;

似乎这里唯一的区别是

xml:space="preserve"

svg

关于javascript - 缩放 svg 背景图像以适合容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33781235/

相关文章:

javascript - 多个元素的jQuery UI类切换

PHP 表单发布以防止导航/重新发布问题

javascript - 根据字符数突出显示子文本

javascript - 在js脚本中获取mongodb数据类型

javascript - 使用单击功能关闭数据弹出窗口时出现问题

javascript - 有没有办法检查用户是否单击了 JS 打开的窗口中的按钮?

javascript - Chrome 扩展消息作为对象传递

jquery - 我无法让 jquery .width() 工作

jquery - html 或 css 与 jquery 简洁

php - 复选框或下拉筛选结果