html - 随文本宽度缩放的 SVG

标签 html css svg

我正在尝试在 SVG 中构建一个简单的横幅,它会随着其中的文本被填充而缩放宽度。

这是我当前的代码:

#container {
    height: 60px;
    position: relative;
}

#container > svg {
    width: 100%;
    height: 100%;
}

#container > p {
    position: absolute;
    left: 0;
    margin: 5% 10%;
    text-align: center;
    color: white;
}

和 HTML:

<div id="container">
    <span>Strawberry Mango</span>
    <svg viewBox="0 0 10 10" preserveAspectRatio="none">
        <path d="M 0,0 L 10,0 9.5,5 10,10 0,10 0.5,5 z" fill="black"></path>
        <path d="M 0.25,0.75 L 9.75,0.75 9.3,5 9.75,9.25 0.25,9.25 0.75,5 z" fill="orange"</path>
        </svg>
</div>

这会生成一个宽度为父元素 100% 的横幅,但如果可能的话,我希望它能缩放到 p 标签的大小。

Here is a fiddle of it

最佳答案

尝试给 #container display:inline-block 让它缩小到它的 child ,然后让 svg 位置: absolute 而不是段落,还给它 z-index:-1; 把它放在段落后面。这是因为您的 SVG 元素默认为 300px,这是您不希望发生的

Demo

关于html - 随文本宽度缩放的 SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22055223/

相关文章:

javascript - 在 .html 文件之外执行函数/脚本?

jquery - 使用jquery求和div内元素的值

javascript - 鼠标移动时显示/隐藏 div

javascript - jQuery 响应式多页菜单鼠标悬停/离开交互

svg - 在SVG中画一个空心圆

javascript - Facebook 标签(在文本区域内突出显示)

html - 如何正确 float 列表

css - Django CSS 相对路径在 Amazon S3 中不起作用

python - Matplotlib imshow 与 EPS

html - 从 CSS 调整 SVG 的大小和颜色