I have the following HTML:
<div class="outer">
<div class="inner rotate">Centered?</div>
</div>
div.outer
is a narrow vertical strip. div.inner
is rotated 90 degrees. I would like the text "Centered?" to appear centered in its container div. I do not know the size of either div in advance.
This comes close: http://jsfiddle.net/CCMyf/2/. You can see from the jsfiddle that the text is vertically centered before the transform: rotate(-90deg)
style is applied, but is somewhat offset after. This is particularly noticeable when div.outer
is short.
Is it possible to center this text vertically without knowing any of the sizes in advance? I haven't found any values of transform-origin
that solve this problem.
display: table
on the elements? Because the 'standard' way to vertically align dynamic divs should work with the transform without issue: https://mcmap.net/q/45137/-how-can-i-vertically-center-a-div-element-for-all-browsers-using-css – Wingeddisplay: table
. This fiddle shows that the table solution has the same issues as the one above: jsfiddle.net/4d384 – Costaterotate
class on the middle element. That should do it, but it looks like it messes up the width of the item. Might have to play around with the padding or width or height props, but it should be do-able: jsfiddle.net/digthedoug/jWYuq – Wingedmin-width: 16px; width: 16px
on.outer
results in the exact same non-centering as in the original question. – Costate