ChatGPT解决这个技术问题 Extra ChatGPT

How to make a <svg> element expand or contract to its parent container?

The goal is to have the <svg> element expand to the size of its parent container, in this case a <div>, no matter how big or small that container may be.

The code:

<style>
    svg, #container{
        height: 100%;
        width: 100%;
    }
</style>

<div id="container">
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1" >
         <rect x="0" y="0" width="100" height="100" />
    </svg>
</div>

The most common solution to this problem seems to be setting the viewBox attribute on the <svg> element.

viewBox="0 0 widthOfContainer heightOfContainer"

However, this does not seem to work in cases where elements within the <svg> element have predefined widths and/or heights. For example, the <rect> element, in the above code, has its width and height explicitly set.

So the obvious solution is to use % widths and % heights on those elements as well. But does this even have to be done? Especially, since <img src=test.svg > works fine and expands/contracts without any problems with explicitly set <rect> heights and widths.

If elements like <rect>, and other elements like it, have to have their widths and heights defined in percentages, is there a way in Inkscape to set it so that all elements with the <svg> document use percentage widths, heights, etc.. instead of fixed dimensions?

Save the svg as a file, than reference it as an image, like and that way you can use width:100% or height:100%

r
robertc

The viewBox isn't the height of the container, it's the size of your drawing. Define your viewBox to be 100 units in width, then define your rect to be 10 units. After that, however large you scale the SVG, the rect will be 10% the width of the image.


But the question is: How to scale the SVG?
@AdrianLang The scaling happens automatically when you set a size on the SVG object, as is already done in the code in the question. Here's a fixed version of the example.
You’re right, great, that actually works for me. I guess my problem was the height and width properties in my svg root element.
I made some minor tweaks to that fiddle to make that resizing be forced horizontally and vertically without preserving the aspect ratio, and made it work in IE, while the fiddle @robertc submitted wasn't. Hope it'll help someone down the road!
By default your svg will scale as large as possible so that it's completely visible but preserves it's aspect ratio (so a square viewBox won't completely fill a rectangular parent). If you really want to force it to completely cover the parent container, add preserveAspectRatio="none" to the SVG element.
R
Reza Baradaran Gazorisangi

https://i.stack.imgur.com/6sjbk.jpg

And I want to put it in a div and make it fill the div responsively. My way of doing it is as follows:

First I open the SVG file in an application like inkscape. In File->Document Properties I set the width of the document to 800px and and the height to 600px (you can choose other sizes). Then I fit the SVG into this document.

https://i.stack.imgur.com/YRrFU.jpg

Then I save this file as a new SVG file and get the path data from this file. Now in HTML the code that does the magic is as follows:

<div id="containerId">    
    <svg
    id="svgId" 
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    version="1.1"
    x="0"
    y="0"
    width="100%"
    height="100%"
    viewBox="0 0 800 600"
    preserveAspectRatio="none">
       <path d="m0 0v600h800v-600h-75.07031l-431 597.9707-292.445315-223.99609 269.548825-373.97461h-271.0332z" fill="#f00"/>
    </svg>
</div>

Note that width and height of SVG are both set to 100%, since we want it to fill the container vertically and horizontally ,but width and height of the viewBox are the same as the width and height of the document in inkscape which is 800px X 600px. The next thing you need to do is set the preserveAspectRatio to "none". If you need to have more information on this attribute here's a good link. And that's all there is to it.

One more thing is that this code works on almost all the major browsers even the old ones but on some versions of android and ios you need to use some javascrip/jQuery code to keep it consistent. I use the following in document ready and resize functions:

$('#svgId').css({
    'width': $('#containerId').width() + 'px',
    'height': $('#containerId').height() + 'px'
});

Hope it helps!


+1 for the preserveAspectRatio note. After hunting high and low to get an SVG to actually stretch (not scale) to a div, this was the correct answer.
@Gazoris, Thanks for this wonderful answer. have question on the same. What if my svg is nested. for e.g., i want to display svg 1 and 2 conditionally and it should take width and height of container.
can you please help me out on this.
@Reza Baradaran: Genius.
m
mikemaccana

What's worked for me recently is to remove all height="" and width="" attributes from the <svg> tag and all child tags. Then you can use scaling using a percentage of the parent container's height or width.

Before:

<svg width="3212" height="3212" viewBox="0 0 3212 3212" fill="none" xmlns="http://www.w3.org/2000/svg">
   circle cx="1606" cy="1606" r="1387" stroke="black" stroke-width="438"/>
</svg>

After:

<svg viewBox="0 0 3212 3212" fill="none" xmlns="http://www.w3.org/2000/svg">
   circle cx="1606" cy="1606" r="1387" stroke="black" stroke-width="438"/>
</svg>

Can you give a working example? I can't get this to do anything.
This worked for me too. I've edited this question to add an example.
J
Justin Putney

@robertc has it right, but you also need to notice that svg, #container causes the svg to be scaled exponentially for anything but 100% (once for #container and once for svg).

In other words, if I applied 50% h/w to both elements, it's actually 50% of 50%, or .5 * .5, which equals .25, or 25% scale.

One selector works fine when used as @robertc suggests.

svg {
  width:50%;
  height:50%;
}

That's because your selector svg, #container matches both the svg dom element and the #container dom element. I believe you were instead looking for svg #container, but the specification of a parent isn't necessary if you're using an ID anyway.
Isn't that exactly what I said, "once for #container and once for svg"?
Oh pardon, you're right, but your answer is a bit oddly worded leading to the misunderstanding. It sounds as if you're talking about a potential gotcha at first.
A
Ashkan Mobayen Khiabani

For your iphone You could use in your head balise :

"width=device-width"