如何摆脱div元素中svg下面的额外空间

时间:2014-07-08 08:19:34

标签: html css svg

以下是问题的说明(在Firefox和Chrome中测试):

<div style="background-color: red;"><svg height="100px" width="100" style="background-color: blue;"></svg></div>

http://jsfiddle.net/EXLSF/

请注意蓝色red下方div内的额外svg空格。

已经尝试将这两个元素的paddingmargin设置为0,但没有运气。

7 个答案:

答案 0 :(得分:116)

display: block;上需要svg

<svg style="display: block;"></svg>

这是因为内联块元素(如<svg><img>)位于文本基线上。你看到的额外空间是留下来容纳角色下降器的空间(尾部在&#39; y&#39;,&#39; g&#39;等)。

如果您需要保留vertical-align:topinline

,也可以使用inline-block

答案 1 :(得分:8)

svginline元素。 inline个元素会留下空格。

解决方案:

display:block添加到svg,或将父div的高度设为svg

DEMO here.

答案 2 :(得分:3)

将svg的display属性更改为block

答案 3 :(得分:2)

另一种选择是将font-size: 0添加到svg父级。

答案 4 :(得分:1)

尝试将height:100px添加到div并在height="100%"上使用svg

<div style="background-color:red;height:100px">
    <svg height="100%" width="100" style="background-color:blue;"></svg>
</div>

答案 5 :(得分:1)

只需将高度添加到主div元素

即可
<div style="background-color: red;height:100px"><svg height="100px" width="100" style="background-color: blue;"></svg></div>

答案 6 :(得分:0)

将样式更改为

style =“ background-color:red; line-height:0;”