【问题标题】:Fixed SVG height & variable (100%) width固定 SVG 高度和可变 (100%) 宽度
【发布时间】:2015-02-04 15:40:55
【问题描述】:

我正在尝试在我的 html 页面上放置一个 svg 对象,我希望它具有 100% 的宽度和固定的高度。

在我的小提琴中,您可以看到深灰色对象的高度根据窗口比例而变化。 (这不是我想要的)

http://jsfiddle.net/Lq207ery/6/

HTML

<body>
<!-- HEADER -->
<header>
    <div class="logo"></div>
    <div class="triangle">
        <svg data-type="vertical_parallax" data-speed="2" x="0px" y="0px" width="410" height="410" viewBox="0 0 310 310" style="-webkit-transform: translate3d(0px, 0px, 0px); transform: translate3d(0px, 0px, 0px);">
            <g>
                <!--<polyline stroke="#222" fill="none" stroke-width="1" points="0,210 0,210 310,0 "></polyline>-->
                <polyline fill="#CC0000" points="0,0 0,200 300,0    "></polyline>
            </g>
        </svg>
    </div>
    <div class="nav">
        <svg width="100%" viewBox="0 0 10 10" preserveAspectRatio="xMinYmin">
            <polygon fill="#222" stroke-width="0" points="0,1.5 0,0 10,0 15,0 " />
        </svg>
    </div>
</header>
<!-- CONTENT -->

CSS

body {
margin: 0;
}
header svg {
    display: block;
    margin: 0;
}
header .triangle {
    z-index: 200;
}
header .logo {
    margin-top: -90px;
}
header .nav {
    position: absolute;
    top:0;
    left:0;
    width:100%;
    z-index:-1;
}

【问题讨论】:

    标签: html css svg


    【解决方案1】:
    1. 给 SVG 元素一个固定的高度。如果您不这样做,元素的高度将与宽度成比例地变化。
    2. 调整您的 viewBox 以裁剪到内容的高度。
    3. 修复您的 preserveAspectRatio 值,使其具有正确的区分大小写的值,例如xMinYMin(不是xMinYmin)。

    演示:http://jsfiddle.net/Lq207ery/8/

    如果您希望您的深灰色三角形拉伸(不保留其纵横比),请改用preserveAspectRatio="none"

    演示:http://jsfiddle.net/Lq207ery/9/

    【讨论】:

    • 谢谢 Phrogz,我正在寻找“preserveAspectRatio”方法。我之前尝试过,但 svg css 的变化可以解决问题。
    • 不客气!将来,请记住不仅要显示错误(您做得很好),还要清楚地描述您想要/期望的内容。我不清楚你想要的最终外观是什么。
    【解决方案2】:

    你的解释有点短,但你可以这样做:

    HTML 代码:

    <header>
        <div class="triangle">
            <svg x="0px" y="0px" width="410" height="410" viewBox="0 0 310 310">
                <g>
                    <polyline fill="#CC0000" points="0,0 0,200 300,0"/>
                </g>
            </svg>
        </div>
        <div class="nav"></div>
    </header>
    

    CSS 代码:

    html, body {
        margin: 0;
    }
    header {
        position: relative;
    }
    header .triangle {
        position: relative;
        z-index: 2;        
    }
    header .nav {
        position: absolute;
        top:0;
        left:0;
        width: 2000px; // big screens
        height: 100px;
        background-image:url('data:image/svg+xml;utf8,%3Csvg%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20x%3D%220px%22%20y%3D%220px%22%20width%3D%22150px%22%20height%3D%2215px%22%20viewBox%3D%220%200%20150%2015%22%3E%3Cg%3E%3Cpolygon%20fill%3D%22%23222%22%20points%3D%220%2C15%200%2C0%20100%2C0%20150%2C0%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E');
        background-size: cover;
        background-position: bottom left;
        z-index: 1;
    }
    

    url编码前的SVG:

    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="150px" height="15px" viewBox="0 0 150 15">
        <g>
            <polygon fill="#222" points="0,15 0,0 100,0 150,0"/>
        </g>
    </svg>
    

    演示: http://jsfiddle.net/sparkup/21uaffpy/

    或者这样写:http://jsfiddle.net/sparkup/21uaffpy/18/

    【讨论】:

    • 谢谢 Sparkup,我可能会将这个技巧用于另一个项目。你如何获得 background-image:url(....) ?
    • 你只需在前面加上data:image/svg+xml;utf8, urlencode &lt;svg&gt;...&lt;/svg&gt;
    猜你喜欢
    • 2018-10-08
    • 1970-01-01
    • 2018-02-26
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多