简体   繁体   English

在 svg 元素上显示内联和块之间的区别

[英]Difference between display inline and block on svg elements

I understand the difference when it comes to divs.我理解 div 的区别。

But in svg:但在 svg 中:

<svg>
    <rect display="block" id="svg_3" height="57" width="52" y="20" x="41" stroke-width="5" stroke="#000000" fill="#FF0000"/>

     <rect display="inline" id="svg_3" height="57" width="52" y="20" x="120" stroke-width="5" stroke="#000000" fill="#0000BB"/>
</svg>

Seems to produce the same result... ('none' hides the element tho) Here's the jsfiddle: https://jsfiddle.net/foreyez/3c7va377/似乎产生相同的结果......('none'隐藏元素tho)这是jsfiddle: https ://jsfiddle.net/foreyez/3c7va377/

what's the difference, and what's the default value, inline or block?有什么区别,默认值是什么,内联还是块?

Per the SVG specification根据SVG 规范

A value of display: none indicates that the given element and its children shall not be rendered directly (ie, those elements are not present in the rendering tree). display: none 值表示给定元素及其子元素不应直接呈现(即,这些元素不存在于呈现树中)。 Any value other than none or inherit indicates that the given element shall be rendered by the SVG user agent.除了 none 或inherit 之外的任何值都表示给定的元素应由SVG 用户代理呈现。

So everything except none is treated exactly the same.所以除了 none 之外的所有东西都被完全一样对待。

SVG is not HTML, it has no concept of reflow (ie changes in the position of one element do not affect other elements apart from tspan and tref in text). SVG 不是 HTML,它没有回流的概念(即一个元素位置的变化不会影响文本中除了 tspan 和 tref 之外的其他元素)。

I searched for this because I have an svg in a page where the height of the surrounding element is 5px more than the actual height of the svg.我搜索这个是因为我在一个页面中有一个 svg,其中周围元素的高度比 svg 的实际高度高 5px。

As far as I can see it does make a difference if you use display="none" , display="block" or display="inline" .据我所知,如果您使用display="none"display="block"display="inline"确实会有所不同。

Just as an image there is space below a svg.就像图像一样,svg 下方有空间。 Because they are, by default, inline-block elements (inline in some browsers).因为默认情况下它们是内联块元素(在某些浏览器中是内联的)。 As such, they sit alongside text: the space that's visible under an svg is there for descenders on letters like 'p' and 'q'.因此,它们与文本并排:在 svg 下可见的空间用于“p”和“q”等字母的下行。

This can be seen by placing a svg within a div.这可以通过在 div 中放置一个 svg 来看到。 If the svg is 24px.如果 svg 是 24px。 high, the div will have a height of (for instance) 29 px.高,div 的高度(例如)为 29 像素。

display="block" will prevent the svg to reserve that space, so the div wherein the svg is placed will have the same height. display="block"将阻止 svg 保留该空间,因此放置 svg 的 div 将具有相同的高度。

In my practice, when I use <svg> tag -- browser (Google Chrome Version 80.0.3987.100 (Official Build) (64-bit)) interpret it as inline element by default .在我的实践中,当我使用<svg>标签时 -- browser (Google Chrome Version 80.0.3987.100 (Official Build) (64-bit))默认将其解释为内联元素 And it behaves as a standart inline element .它表现为标准的内联元素 If complines display: block css-property for it -- it behave as standart block element如果 complines display: block css-property for it -- 它表现为标准块元素

If we make some research and fall into computed tab in the dev tools on any svg-child element then we'll see that it has the display inline property.如果我们进行一些研究并进入开发工具中任何 svg-child 元素的计算选项卡,那么我们将看到它具有display inline属性。 Even if we'll set svg display: block the svg-child elements stay inline elements by default即使我们将设置 svg display: block svg-child 元素默认保持内联元素

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

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