Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

svg

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

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

View on JSFiddle

Notice the extra red space inside the div below the blue svg.

Already tried setting padding and margin of both elements to 0, but with no luck.

like image 284
Daniel Avatar asked Jul 08 '14 08:07

Daniel


People also ask

How do I get rid of SVG padding?

This is done using the attribute "preserveAspectRatio" and setting it to "none". Hopefully this helps some of your understanding of SVGs, as they're a really useful tool! Show activity on this post. If you want the SVG to stretch to the entire box, put preserveAspectRatio="none" on the root <svg> element.

Why is there extra space at the bottom of my div?

If you try to put an image inside a <div> element that has borders, you will see an extra white space (around 3px) at the bottom of image. It happens because image is an inline-level element so browser adds some whitespace under the baseline to adjust other inline elements.

Why is there extra space in SVG?

The explanation is that 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).

Why does my SVG have extra space at the top?

You need display: block; on your 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 Amazing.

Why is there a white space between a div and image?

Cause of this issue: It happens because image is an inline element, In order to keep other elements inline, browser add a white space between div and image. Yuou can run code using codepen editor or click on right corner of the window.

Why does my image have extra space below the image?

Seems to be related to a similar problem with images, e.g. stackoverflow.com/questions/7774814/…. Does this answer your question? Image inside div has extra space below the image You need display: block; on your svg. This is because inline-block elements (like <svg> and <img>) sit on the text baseline.

How do I change the height of an SVG file?

Change the display property of the svg to be block. Try adding height:100px to div and using a height="100%" on svg: Thanks for your comment. This works fine, but becomes tricky when one does not know the height of the svg in advance. If you're setting the height on your SVG, and you want the <a> tag to wrap around your SVG:


1 Answers

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

like image 177
Andy Avatar answered Sep 22 '22 12:09

Andy