Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical Alignment of SVG in CSS

I have a problem with inline SVG alignment. I have created a button with some text and an SVG. And while the alignment works correctly when the SVG is at least as big as the text it fails when the SVG height is smaller than the text.

I have created a test case with a two-colored background button to indicate where exactly the middle is. You can see if you look closer that the second case is not perfectly aligned because the height of the SVG is less than the one from the text.

Is there any way to fix this? Doing it some other way (no js please)?

Test case: https://goo.gl/KYDKGH

like image 669
MyWetSocks Avatar asked Mar 12 '16 19:03

MyWetSocks


People also ask

How do I align vertically in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do I center an svg image in CSS?

Just add: . container { display: flex; justify-content: center; } And add the . container class to the div which contains your svg.


1 Answers

jsfiddle 1 - You can use position:relative on the container and position:absolute on the objects like this:

  position: absolute;   top: 50%;   -webkit-transform: translateY(-50%);   -ms-transform: translateY(-50%);   transform: translateY(-50%);   left: 0;   right: 0;   margin: auto;   text-align: center; 

The top: 50% moves the object to the container's vertical center picking the top of the object as reference (not its center), so the transform: translateY moves it a distance of 50% of it's size upwards to let it exactly on the middle of the container (by the objects center).

ps: the text-align:center; left:0; right:0; and margin:auto are for horizontal align.

jsfiddle 2 - Or use display:flex on the container with align-items to vertical align the content like this:

  display: -webkit-flex; /* Safari */     display: flex;   -webkit-align-items: center; /* Safari 7.0+ */   align-items: center;   -webkit-justify-content: center;   justify-content: center; 

ps: the justify content is for horizontal align.

like image 71
Le____ Avatar answered Sep 28 '22 13:09

Le____