以下是问题的说明(在Firefox和Chrome中测试):
<div style="background-color: red;"><svg height="100px" width="100" style="background-color: blue;"></svg></div>
请注意蓝色red
下方div
内的额外svg
空格。
已经尝试将这两个元素的padding
和margin
设置为0
,但没有运气。
答案 0 :(得分:116)
display: block;
上需要svg
。
<svg style="display: block;"></svg>
这是因为内联块元素(如<svg>
和<img>
)位于文本基线上。你看到的额外空间是留下来容纳角色下降器的空间(尾部在&#39; y&#39;,&#39; g&#39;等)。
如果您需要保留vertical-align:top
或inline
inline-block
答案 1 :(得分:8)
答案 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;”