简体   繁体   English

如何调整大svg的大小以适应div

[英]how to resize a large svg to fit inside a div

I have the following div inside <body> . 我在<body>里面有以下div。

<div style="width:320px; height:480px;">
    <svg id="svg1"></svg>            
</div>

I want to fit a 640X480 svg inside this div. 我想在这个div中安装一个640X480 svg。 I have tried this: 我试过这个:

var paper=Snap("#svg1");
Snap.load("somesvg.svg",function(f){ 
 paper.append(f);
 }); 
 paper.attr({ 
 width:320, height:480
 }) 

But the svg is cut off from the right size. 但svg与正确尺寸相隔。

I was wondering about adjusting the viewBox for this, so something like... 我想知道为此调整viewBox,所以像...

<div style="width:320px; height:480px;">
    <svg id="svg1" width="100%" height="100%" viewBox="0 0 640 480" preserveAspectRatio="xMaxYMax"></svg>
</div>

jsfiddle http://jsfiddle.net/9zRR8/5/ jsfiddle http://jsfiddle.net/9zRR8/5/

The SVG will not scale because it does not have a viewBox defined. SVG不会缩放,因为它没有定义viewBox。 The viewBox tells the browser what the dimensions of the content are. viewBox告诉浏览器内容的尺寸是什么。 Without it, there is no way to determine how much it should be scaled up or down to fit. 没有它,就无法确定它应该按比例放大或缩小。

This is kind of a failing of svg-edit. 这是svg-edit的失败。 It really should be adding one, ideally. 理想情况下,它确实应该添加一个。

What you could try is loading the SVG into Inkscape or another SVG editor which should add a viewBox. 您可以尝试将SVG加载到Inkscape或其他应添加viewBox的SVG编辑器中。 Alternatively, you could add one manually, as per Ian's answer. 或者,您可以根据Ian的回答手动添加一个。

如何调整羽毛笔编辑器的大小以适应<div> ?</div><div id="text_translate"><p> 我希望羽毛笔编辑器自动适应父&lt;div&gt;元素。</p><p> 我创建了&lt;div&gt;元素,并在其中再次创建了一个&lt;div&gt;元素,该元素将成为 quill 编辑器的容器。 工具栏是自动创建的。 我使用 css 将父&lt;div&gt;元素的大小调整为width:50%和height:50% ,我希望其中的所有内容(羽毛笔编辑器和工具栏)都适合其中,但是羽毛笔编辑器的某些部分会出现父&lt;div&gt;元素的。</p><p> <strong>HTML:</strong></p><pre class="lang-html prettyprint-override"> &lt;,-- parent div element: in which quill editor should fit --&gt; &lt;div id="parent" style="width;50%:height;70%:border:2px solid green"&gt; &lt;!-- Quill editor div --&gt; &lt;div id="editor"&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> <strong>Javascript:</strong></p><pre> var tool = [[{ 'header': '1' }, { 'header': '2' }], [{ size: ['small', false, 'large', 'huge'] }], ['bold', 'italic', 'underline', 'strike'], [{ 'color': [] }, { 'background': [] }, { 'font': [] }, { 'align': [] }], ['image',]]; var option = { placeholder: 'Compose an epic...', theme: 'snow', modules: { toolbar: tool }, bounds:'#parent' }; var quill = new Quill('#editor', option);</pre><p> 我在父 div 周围放了一个绿色边框,我发现编辑器从边框出来了。 请看截图:</p><p> <a href="https://i.stack.imgur.com/RlASQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RlASQ.png" alt="截屏"></a></p><p> 注意:我只想保持父 div 的大小固定。 所以,请告诉我该怎么办?</p><p> 编辑:我将整个代码(html 文件)上传到我的服务器(<a href="http://tu.unaux.com/game.html" rel="nofollow noreferrer">这里</a>)。 问题仍然存在。 请检查一下,您可以使用“检查元素”查看整个代码。</p></div> - How to resize quill editor to fit inside a <div>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调整svg的大小以适应div? - How to resize a svg to fit inside a div? 如何调整羽毛笔编辑器的大小以适应<div> ?</div><div id="text_translate"><p> 我希望羽毛笔编辑器自动适应父&lt;div&gt;元素。</p><p> 我创建了&lt;div&gt;元素,并在其中再次创建了一个&lt;div&gt;元素,该元素将成为 quill 编辑器的容器。 工具栏是自动创建的。 我使用 css 将父&lt;div&gt;元素的大小调整为width:50%和height:50% ,我希望其中的所有内容(羽毛笔编辑器和工具栏)都适合其中,但是羽毛笔编辑器的某些部分会出现父&lt;div&gt;元素的。</p><p> <strong>HTML:</strong></p><pre class="lang-html prettyprint-override"> &lt;,-- parent div element: in which quill editor should fit --&gt; &lt;div id="parent" style="width;50%:height;70%:border:2px solid green"&gt; &lt;!-- Quill editor div --&gt; &lt;div id="editor"&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> <strong>Javascript:</strong></p><pre> var tool = [[{ 'header': '1' }, { 'header': '2' }], [{ size: ['small', false, 'large', 'huge'] }], ['bold', 'italic', 'underline', 'strike'], [{ 'color': [] }, { 'background': [] }, { 'font': [] }, { 'align': [] }], ['image',]]; var option = { placeholder: 'Compose an epic...', theme: 'snow', modules: { toolbar: tool }, bounds:'#parent' }; var quill = new Quill('#editor', option);</pre><p> 我在父 div 周围放了一个绿色边框,我发现编辑器从边框出来了。 请看截图:</p><p> <a href="https://i.stack.imgur.com/RlASQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RlASQ.png" alt="截屏"></a></p><p> 注意:我只想保持父 div 的大小固定。 所以,请告诉我该怎么办?</p><p> 编辑:我将整个代码(html 文件)上传到我的服务器(<a href="http://tu.unaux.com/game.html" rel="nofollow noreferrer">这里</a>)。 问题仍然存在。 请检查一下,您可以使用“检查元素”查看整个代码。</p></div> - How to resize quill editor to fit inside a <div>? 使溢出调整大小以适应div内部 - Making overflow resize to fit inside div 如何调整png图像的大小以适合div? - How to resize a png image to fit in a div? 如何调整 CKEditor 的大小以适合其父 div - How to resize CKEditor to fit its parent div CSS - 添加更多正方形时,如何使正方形/正方形适合并在 div 内调整大小? - CSS - How to make a square/squares fit and resize inside a div when added more squares? 调整文字大小以适合div - Resize text to fit div 调整div以适应屏幕 - Resize div to fit screen 如何在 SVG 多边形内放置和换行文本 - How to fit and wrap text inside SVG polygon 跨浏览器方式将SVG强制插入div - Cross-browser way to force SVG fit inside div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM