如何调整具有底部SVG的元素的高度,以使其在所有宽度下看起来美观?

huangapple go评论69阅读模式
英文:

How can I adjust the height of an element that has an SVG at the bottom to make it look good on all widths?

问题

如何调整具有底部SVG的元素的高度,以使其在所有宽度上看起来良好?

我试图在我的元素底部实现一个“波浪”效果,该元素在CSS中具有background-image,但是我已经通过height: auto;使其正常工作,但是我希望图像的高度更大。但是,当我添加高度时,SVG不再粘附到元素的底部,而是略高于底部。

我对SVG不太熟悉,所以这对我来说有点头痛。

.svg-container {
    top: 100;
    left: 0;
    width: 100%;
    background-image: url('essex-builder-bathroom.png');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

#visual {
    width: 100%;
    height: auto; 
    /* 如果设置为固定值,例如500px,将导致不正确缩放 */
}
<div class="svg-container bg-primary">
    <svg id="visual" viewBox="0 0 3840 800" width="3840" xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
        <path
        d="M0 551L91.5 557.5C183 564 366 577 548.8 573.8C731.7 570.7 914.3 551.3 1097.2 542.7C1280 534 1463 536 1645.8 538.3C1828.7 540.7 2011.3 543.3 2194.2 552C2377 560.7 2560 575.3 2742.8 584C2925.7 592.7 3108.3 595.3 3291.2 594.5C3474 593.7 3657 589.3 3748.5 587.2L3840 585L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z"
        fill="#2c2b2b"></path>
        <path
        d="M0 654L91.5 657.3C183 660.7 366 667.3 548.8 668.7C731.7 670 914.3 666 1097.2 648.2C1280 630.3 1463 598.7 1645.8 585.7C1828.7 572.7 2011.3 578.3 2194.2 593.5C2377 608.7 2560 633.3 2742.8 643.5C2925.7 653.7 3108.3 649.3 3291.2 652C3474 654.7 3657 664.3 3748.5 669.2L3840 674L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z"
        fill="#1a1919"></path>
        <path
        d="M0 716L91.5 711.8C183 707.7 366 699.3 548.8 689.2C731.7 679 914.3 667 1097.2 664.3C1280 661.7 1463 668.3 1645.8 670C1828.7 671.7 2011.3 668.3 2194.2 673.8C2377 679.3 2560 693.7 2742.8 695.5C2925.7 697.3 3108.3 686.7 3291.2 684.8C3474 683 3657 690 3748.5 693.5L3840 697L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z"
        fill="#000000"></path>
    </svg>
</div>
英文:

How can I adjust the height of an element that has an SVG at the bottom to make it look good on all widths?

I am trying to have a 'wave' effect at the bottom of my element that has a background-image in CSS, however I have made it work fine with height: auto; however I want the image's height to be bigger. However when I add a height, the SVG no longer sticks to the bottom of the element and sits a little above the bottom.

I'm new to SVGs so this is a bit of a headache to me.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.svg-container {
    top: 100;
    left: 0;
    width: 100%;
    background-image: url(&#39;essex-builder-bathroom.png&#39;);
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

    #visual {
        width: 100%;
        height: auto; 
        /* If set to a fixed value, eg 500px, it will cause it to not scale properly */
    }

<!-- language: lang-html -->

&lt;div class=&quot;svg-container bg-primary&quot;&gt;
    &lt;svg id=&quot;visual&quot; viewBox=&quot;0 0 3840 800&quot; width=&quot;3840&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;
        xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; version=&quot;1.1&quot;&gt;
        &lt;path
        d=&quot;M0 551L91.5 557.5C183 564 366 577 548.8 573.8C731.7 570.7 914.3 551.3 1097.2 542.7C1280 534 1463 536 1645.8 538.3C1828.7 540.7 2011.3 543.3 2194.2 552C2377 560.7 2560 575.3 2742.8 584C2925.7 592.7 3108.3 595.3 3291.2 594.5C3474 593.7 3657 589.3 3748.5 587.2L3840 585L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z&quot;
        fill=&quot;#2c2b2b&quot;&gt;&lt;/path&gt;
    &lt;path
        d=&quot;M0 654L91.5 657.3C183 660.7 366 667.3 548.8 668.7C731.7 670 914.3 666 1097.2 648.2C1280 630.3 1463 598.7 1645.8 585.7C1828.7 572.7 2011.3 578.3 2194.2 593.5C2377 608.7 2560 633.3 2742.8 643.5C2925.7 653.7 3108.3 649.3 3291.2 652C3474 654.7 3657 664.3 3748.5 669.2L3840 674L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z&quot;
        fill=&quot;#1a1919&quot;&gt;&lt;/path&gt;
    &lt;path
        d=&quot;M0 716L91.5 711.8C183 707.7 366 699.3 548.8 689.2C731.7 679 914.3 667 1097.2 664.3C1280 661.7 1463 668.3 1645.8 670C1828.7 671.7 2011.3 668.3 2194.2 673.8C2377 679.3 2560 693.7 2742.8 695.5C2925.7 697.3 3108.3 686.7 3291.2 684.8C3474 683 3657 690 3748.5 693.5L3840 697L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z&quot;
        fill=&quot;#000000&quot;&gt;&lt;/path&gt;
    &lt;/svg&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

我已经明白你想要实现的目标。

如果你将以下属性添加到你的SVG HTML中:

preserveAspectRatio=&quot;none&quot;

width=&quot;100vw&quot;

height=&quot;100vh&quot;

然后你的代码如下:

* {
  margin: 0;
  padding: 0;
}
.svg-container {
    position: relative;
    height: 100%;
    top: 100;
    left: 0;
    width: 100%;
    background-image: url('essex-builder-bathroom.png');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}
<div class="svg-container bg-primary">
    <svg 
         id="visual" 
         viewBox="0 0 3840 800" 
         preserveAspectRatio="none"
         width="100vw"
         height="100vh"
         xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
        <path
        d="M0 551L91.5 557.5C183 564 366 577 548.8 573.8C731.7 570.7 914.3 551.3 1097.2 542.7C1280 534 1463 536 1645.8 538.3C1828.7 540.7 2011.3 543.3 2194.2 552C2377 560.7 2560 575.3 2742.8 584C2925.7 592.7 3108.3 595.3 3291.2 594.5C3474 593.7 3657 589.3 3748.5 587.2L3840 585L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z"
        fill="#2c2b2b"></path>
    <path
        d="M0 654L91.5 657.3C183 660.7 366 667.3 548.8 668.7C731.7 670 914.3 666 1097.2 648.2C1280 630.3 1463 598.7 1645.8 585.7C1828.7 572.7 2011.3 578.3 2194.2 593.5C2377 608.7 2560 633.3 2742.8 643.5C2925.7 653.7 3108.3 649.3 3291.2 652C3474 654.7 3657 664.3 3748.5 669.2L3840 674L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z"
        fill="#1a1919"></path>
    <path
        d="M0 716L91.5 711.8C183 707.7 366 699.3 548.8 689.2C731.7 679 914.3 667 1097.2 664.3C1280 661.7 1463 668.3 1645.8 670C1828.7 671.7 2011.3 668.3 2194.2 673.8C2377 679.3 2560 693.7 2742.8 695.5C2925.7 697.3 3108.3 686.7 3291.2 684.8C3474 683 3657 690 3748.5 693.5L3840 697L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z"
        fill="#000000"></path>
    </svg>
</div>

以上是你要的翻译内容,没有其他内容。

英文:

I have I have figured out what you were trying to achieve.

if you add the following attributes to your to the SVG html

preserveAspectRatio=&quot;none&quot;

width=&quot;100vw&quot;

height=&quot;100vh&quot;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
}
.svg-container {
    position: relative;
    height: 100%;
    top: 100;
    left: 0;
    width: 100%;
    background-image: url(&#39;essex-builder-bathroom.png&#39;);
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

<!-- language: lang-html -->

&lt;div class=&quot;svg-container bg-primary&quot;&gt;
    &lt;svg 
         id=&quot;visual&quot; 
         viewBox=&quot;0 0 3840 800&quot; 
         preserveAspectRatio=&quot;none&quot;
         width=&quot;100vw&quot;
         height=&quot;100vh&quot;
         xmlns=&quot;http://www.w3.org/2000/svg&quot;
        xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; version=&quot;1.1&quot;&gt;
        &lt;path
        d=&quot;M0 551L91.5 557.5C183 564 366 577 548.8 573.8C731.7 570.7 914.3 551.3 1097.2 542.7C1280 534 1463 536 1645.8 538.3C1828.7 540.7 2011.3 543.3 2194.2 552C2377 560.7 2560 575.3 2742.8 584C2925.7 592.7 3108.3 595.3 3291.2 594.5C3474 593.7 3657 589.3 3748.5 587.2L3840 585L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z&quot;
        fill=&quot;#2c2b2b&quot;&gt;&lt;/path&gt;
    &lt;path
        d=&quot;M0 654L91.5 657.3C183 660.7 366 667.3 548.8 668.7C731.7 670 914.3 666 1097.2 648.2C1280 630.3 1463 598.7 1645.8 585.7C1828.7 572.7 2011.3 578.3 2194.2 593.5C2377 608.7 2560 633.3 2742.8 643.5C2925.7 653.7 3108.3 649.3 3291.2 652C3474 654.7 3657 664.3 3748.5 669.2L3840 674L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z&quot;
        fill=&quot;#1a1919&quot;&gt;&lt;/path&gt;
    &lt;path
        d=&quot;M0 716L91.5 711.8C183 707.7 366 699.3 548.8 689.2C731.7 679 914.3 667 1097.2 664.3C1280 661.7 1463 668.3 1645.8 670C1828.7 671.7 2011.3 668.3 2194.2 673.8C2377 679.3 2560 693.7 2742.8 695.5C2925.7 697.3 3108.3 686.7 3291.2 684.8C3474 683 3657 690 3748.5 693.5L3840 697L3840 801L3748.5 801C3657 801 3474 801 3291.2 801C3108.3 801 2925.7 801 2742.8 801C2560 801 2377 801 2194.2 801C2011.3 801 1828.7 801 1645.8 801C1463 801 1280 801 1097.2 801C914.3 801 731.7 801 548.8 801C366 801 183 801 91.5 801L0 801Z&quot;
        fill=&quot;#000000&quot;&gt;&lt;/path&gt;
    &lt;/svg&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月28日 04:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定