To answer your question why top: 50% is not working, when you use property top on an element, the parent of that element needs to have a static height set in place. This should be in pxpx or a unit other than percent. If you really need to use percent in your parent as well, then you can move on to the next parent (which is the parent of that parent) and have that one a fixed static height.
To vertically centering anything. I prefer to use this method here
The use of cssCSS transform since you don't have to know what the width of height your element has.
- Using javascriptJavaScript take the natural height of the parent element and then set the height property of that parent element to the value you just took. You can use this method when the height of the parent element is caused by another child element that is sibling to the element you are centering.
Tip: If you are taking responsive into concern, just set height again in javscriptJavaScript on browser resize. You can get the new natural height by setting height to auto in javascriptJavaScript and do the process again.
- You can scratch the ideas above and use centering with
display: tableinstead. CssCSS-tricksTricks has a very good example here.
Hope this helps someone. :)