How to get rid of extra space below svg in div element

后端 未结 7 842
时光取名叫无心
时光取名叫无心 2020-11-29 02:00

Here is an illustration of the problem (tested in Firefox and Chrome):

相关标签:
7条回答
  • 2020-11-29 02:37

    Try adding height:100px to div and using a height="100%" on svg:

    <div style="background-color:red;height:100px">
        <svg height="100%" width="100" style="background-color:blue;"></svg>
    </div>
    
    0 讨论(0)
  • 2020-11-29 02:37

    Change your style to

    style="background-color: red; line-height: 0;"

    0 讨论(0)
  • 2020-11-29 02:38

    simply add height to main div element

    <div style="background-color: red;height:100px"><svg height="100px" width="100" style="background-color: blue;"></svg></div>
    
    0 讨论(0)
  • 2020-11-29 02:47

    You need display: block; on your svg.

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

    This is because inline-block elements (like <svg> and <img>) sit on the text baseline. The extra space you are seeing is the space left to accommodate character descenders (the tail on 'y', 'g' etc).

    You can also use vertical-align:top if you need to keep it inline or inline-block

    0 讨论(0)
  • 2020-11-29 02:47

    svg is an inline element. inline elements leave white-space.

    Solution:

    Add display:block to svg, or make height of parent div same as svg.

    DEMO here.

    0 讨论(0)
  • 2020-11-29 02:47

    Change the display property of the svg to be block.

    0 讨论(0)
提交回复
热议问题