简体   繁体   English

SVG 元素宽度和高度在 html 中设置但在检查器中显示为 0 0

[英]SVG element width and height set in html but appearing as 0 0 in inspector

I have an svg rect like this:我有一个 svg 这样的矩形:

 <svg class="legend-square"> <defs> <pattern id="pattern1" width="3" height="3" patternunits="userSpaceOnUse" patterntransform="rotate(-45)"> <rect width="2" height="3" transform="translate(0,0)" fill="purple"></rect> </pattern> </defs> <rect width="12" height="12" fill="url(#pattern1)"></rect> </svg>

When I inspect the second rect with Chrome it has no width and height.当我用 Chrome 检查第二个矩形时,它没有宽度和高度。 在此处输入图像描述 There are no CSS rules applying to it.没有适用于它的 CSS 规则。 Why doesn't it get affected by width and height?为什么它不受宽度和高度的影响?

One of the reasons why SVG file is rendered on front-end with zero height and width is missing <svg> tag attributes "height" and "width". SVG 文件在前端以零高度和宽度呈现的原因之一是缺少<svg>标签属性“高度”和“宽度”。

Incorrect:不正确:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 49 30">

Correct:正确的:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 49 30" width="49" height="30">

It works fine.它工作正常。

If the snippet works individually but size of div containing it in you code appears 0x0 then look into : Why is my div's height zero如果代码片段单独工作,但代码中包含它的 div 的大小显示为 0x0,然后查看: 为什么我的 div 的高度为零

Its usually caused when float is set.它通常在设置float时引起。

 <div> <svg class="legend-square"> <defs> <pattern id="pattern1" width="3" height="3" patternunits="userSpaceOnUse" patterntransform="rotate(-45)"> <rect width="2" height="3" transform="translate(0,0)" fill="purple"></rect> </pattern> </defs> <rect width="12" height="12" fill="url(#pattern1)"></rect> </svg> </div>

You really haven't provided enough information.你确实没有提供足够的信息。 For instance, what is the parent element of your SVG?例如,你的 SVG 的父元素是什么?

Just because your <rect> has a width and hight, it doesn't mean your SVG does.仅仅因为你的<rect>有宽度和高度,并不意味着你的 SVG 有。 SVG is not like HTML, where elements expand to fit their children. SVG 不像 HTML,在 HTML 中,元素会扩展以适应它们的子元素。 SVG is like the <canvas> element. SVG 就像<canvas>元素。 You have to make sure it either explicitly (or implicitly) has a size.您必须确保它显式(或隐式)具有大小。

You have not specified width or height attributes for your <svg> element, so they are both defaulting to "100%".您尚未为<svg>元素指定宽度或高度属性,因此它们都默认为“100%”。 What they are 100% of depends on what size the SVG's parent element is.它们的 100% 取决于 SVG 的父元素的大小。 Hence my first question above.因此我上面的第一个问题。

You can wrap your svg element inside the html5 object element.您可以将 svg 元素包装在 html5 对象元素中。

<style type="text/css">
object{
width:300px;
height:200px;
}
object svg{
width:100%;
height:100%;
}
</style>

<object>
<svg class="legend-square">
  <defs>
    <pattern id="pattern1" width="3"
     height="3" patternunits="userSpaceOnUse" patterntransform="rotate(-45)">
      <rect width="2" height="3" transform="translate(0,0)" fill="purple"></rect>
    </pattern>
  </defs>
  <rect width="12" height="12" fill="url(#pattern1)"></rect>
</svg>
</object>



You can set height and width on the object element just link any other html element while you can set height width to 100% for SVG element.您可以在对象元素上设置高度和宽度,只需链接任何其他 html 元素,而您可以将 SVG 元素的高度宽度设置为 100%。 It will work.它会起作用。 But first, test your SVG by opening the file directly in chrome.但首先,通过直接在 chrome 中打开文件来测试您的 SVG。

For an 'inline' SVG element you have to apply the styles, inside the svg element itself.对于“内联”SVG 元素,您必须在 svg 元素本身内部应用样式。 inside the <defs></defs> tags. <defs></defs>标签内。 Since it has its Own DOM[Document Object Model], Css Apllied to it from Outside, will have no effect.由于它有自己的 DOM[文档对象模型],从外部应用到它的 Css 将不起作用。

You have Several different options for embeding an SVG, You can use it inline as in your example where you declare the <svg></svg> inside of your html/php document or you can use one of the many other methods listed below;您有几种不同的嵌入 SVG 的选项,您可以在您的示例中使用它inline ,在您的 html/php 文档中声明<svg></svg>或者您可以使用下面列出的许多其他方法之一;

embed an: .............................................. <img src="../pathtoyourSvg"></img>嵌入: ..................................... <img src="../pathtoyourSvg"></img>
embed as an img with a fallback option: <img src="logo.png" srcset="logo.svg" alt="My logo">嵌入带有后备选项的 img: <img src="logo.png" srcset="logo.svg" alt="My logo">

use an: .................................................... <object type="image/svg+xml" data="image.svg"> <!-- Your fall back here --> <img src="image.svg" /> </object>使用:...................................... ..... <object type="image/svg+xml" data="image.svg"> <!-- Your fall back here --> <img src="image.svg" /> </object>
use: ......................................................... <embed type="image/svg+xml" src="image.svg" />使用:…………………………………………………………………………………………………………………………………… ......... <embed type="image/svg+xml" src="image.svg" />
use an: .................................................... <iframe></iframe>使用:...................................... ..... <iframe></iframe>

embed the Svg inside of a canvas element using Javascript:使用 Javascript 将 Svg 嵌入画布元素中:

var img = new Image();
img.onload = function() {
    ctx.drawImage(img, 0, 0);
}
img.src = "path2your.svg";

You may Also Embed the Svg using Css Background image, ie:您也可以使用 Css 背景图像嵌入 Svg,即:

.mySvgContainer{
   background-image:url('PathToMySvg');
}

But from the comments & Questions I have read, this need to be either Url or Base64 encoded, which seems a bit Hacky and not very convenient.但是从我读过的评论和问题来看,这需要是 Url 或 Base64 编码的,这似乎有点 Hacky 并且不是很方便。

each of them have their own advantages and disadvantages.他们每个人都有自己的优点和缺点。

Because of security reasons, some SVG embedding methods will block access to external resources including CSS, fonts and javascript.出于安全原因,一些 SVG 嵌入方法会阻止访问外部资源,包括 CSS、字体和 javascript。 Especially when we have multiple images, ideally our embedding method should be able to refer to a single CSS, font or javascript file (to save resources) and be able to manipulate our embedded SVG.特别是当我们有多个图像时,理想情况下我们的嵌入方法应该能够引用单个 CSS、字体或 javascript 文件(以节省资源)并能够操作我们嵌入的 SVG。

Also worth mentioning that if you have display:flex;另外值得一提的是,如果你有 display:flex; attached to an elemnet or its parent then the width and height values will have no effect on the flex items.附加到 elemnet 或其父元素,则宽度和高度值将不会影响 flex 项目。 So It may appear as 0, 0, in the console.所以它可能在控制台中显示为 0, 0, 。

Some Useful Information & related questions:一些有用的信息和相关问题:
Svg Coords & Units w3.org Svg 坐标和单位 w3.org
Svg Width - Height Svg 宽度 - 高度
Applying Styles to an embedded Svg 将样式应用到嵌入的 Svg

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM