Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an svg element not respect CSS 'bottom', 'top', 'left', 'right' properties?

Tags:

html

css

svg

size

I sometimes find it convenient to give the size of an element in terms of the bottom, top, left and right properties, rather than using width and height. This is, for example, the accepted answer here:

CSS 100% height with padding/margin

However, for some reason this doesn't work with an svg element. I've tried the following example with the latest stable Firefox and Chrome. The svg element inexplicably wants to take a size of 300x150:

Fiddle

Why?

like image 989
mhelvens Avatar asked Oct 03 '14 18:10

mhelvens


People also ask

Does CSS affect SVG?

There are many Scalable Vector Graphics (SVG), but only certain attributes can be applied as CSS to SVG. Presentation attributes are used to style SVG elements and can be used as CSS properties. Some of these attributes are SVG-only while others are already shared in CSS, such as font-size or opacity .

How do I move an image from bottom to top in CSS?

Absolute PositioningMove Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top. Move Down - Use a positive value for top.

Can you add padding to SVG?

Padding on an svg element actually widens the canvas upon which svg shapes can be visible. For instance, if you have an element that is on the edge of an SVG canvas, then you can add padding that will make that shape not cut off. Whereas margin will just affect the right/left positioning of the svg element.


1 Answers

While it's not mentioned directly in the spec (at least in my knowledge)<svg> is considered as a replaced element (unlike <div> which is a non-replaced block level element).

For absolutely positioned replaced elements, if the values of top/bottom are over-constrained, once you set a value for top, bottom would be ignored. This is true for left/right properties as well.

10.3 Calculating widths and margins / 10.3.8 Absolutely positioned, replaced elements

  1. If at this point the values are over-constrained, ignore the value for either 'left' (in case the 'direction' property of the containing block is 'rtl') or 'right' (in case 'direction' is 'ltr') and solve for that value.

10.6 Calculating heights and margins / 10.6.5 Absolutely positioned, replaced elements

  1. If at this point the values are over-constrained, ignore the value for 'bottom' and solve for that value.

Hence the absolutely positioned <svg> element would be position with the respect to top and left offsets.

like image 163
Hashem Qolami Avatar answered Oct 19 '22 13:10

Hashem Qolami