I have a list of elements and upon a specific action, i'd like to apply the css-transform translate to move it into the center of the window. I know css-transform is used to relatively move an element, but I was wondering whether it is possible to anchor it absolutely somewhere, i.e. in the middle of the window (lightbox-style). I'm looking at an animation similar to the iPad Music app, where the album cover flips and centers.
Note: I want to use css-transforms because it avoids reflow of surrounding elements.
Nam
Edit: I have created a JSfiddle for better illustration: http://jsfiddle.net/bdtZc/, relevant line:
.card:focus {
-webkit-transform: rotateY(180deg);
}
If you want to center something horizontally in CSS you can do it just by, using the text-align: center; (when working with inline elements) or margin: 0 auto; (when working with block element).
Because translateX(-50%) moves something back to the left 50% (because of the - negative value), which means it pairs with left: 50%; to center something. If you want to use right: 50%; then use that with translateX(50%) to center.
The -50% transform basically means, in simple words, "move this element left and upwards by 50% of the computed dimension along the axis". -50% along the x-axis means "move me leftwards by half my computed width", likewise for that in the y-axis.
Since I just received another upvote for this answer I thought I'd revisit it.
With current browsers you should have good luck with the transform(-50%, -50%) technique from other answers but depending on how your content containers are set up that may not result in the center of the window; it may be the center of your container which could be smaller or larger than the window.
The latest browsers support viewport units (vh, vw) which would give you exactly what you're looking for as far as viewport centering. Animation from current location to viewport center would be a different issue due to scrolling.
http://caniuse.com/#feat=viewport-units https://developer.mozilla.org/en-US/docs/Web/CSS/length (see vh, vw)
You can accomplish this without using css-transform by using absolute positioning:
(full code here : http://jsfiddle.net/wkgWg/ )
.posDiv
{
position:absolute;
top:0;
left:0;
margin:0;
border:1px solid red;
-moz-transition:all 2s;
-webkit-transition:all 2s;
-o-transition:all 2s;
transition:all 2s;
}
.triggerElement:hover .posDiv
{
top:50%;
left:50%;
margin-left:-50px;
margin-top:-50px;
-moz-transition:all 2s;
-webkit-transition:all 2s;
-o-transition:all 2s;
transition:all 2s;
}
If you'd like to continue working with CSS-Transform, you'll need to calculate the "center" or the end location of your transform using JavaScript and then generate and attach a transform statement at runtime. Your origin transform vector would need to be subtracted from the "center to screen" vector.
Here's a javascript version using css-transform (where supported) via the jQuery.transit plugin by Rico Sta. Cruz.
(Full fiddle here: http://jsfiddle.net/ZqpGL/263/ )
$(function() {
var $cards = $('.card');
var cardInFocus = null;
$cards.each(function(index, elem) {
var $elem = $(elem);
var pos = $elem.position();
$(elem).data('orig-x', pos.left);
$(elem).data('orig-y', pos.top);
});
var reset = function() {
if (cardInFocus) {
$(cardInFocus).transition({
x: 0,
y: 0
});
}
};
$cards.on('focus', function(e) {
reset();
cardInFocus = this;
var $doc = $(document);
var centerX = $doc.width() / 2;
var centerY = $doc.height() / 2;
var $card = $(this);
var origX = $card.data('orig-x');
var origY = $card.data('orig-y');
$(this).transition({
x: centerX - origX,
y: centerY - origY
});
});
$cards.on('blur', function(e) {
reset();
});
});
An article at CSS-Tricks (http://css-tricks.com/centering-percentage-widthheight-elements/) pointed out that one could use a mixture of absolute positioning and translation, without resorting to the use of JS.
This helps to limit reflow, and keeps your element centered even during size changes to the element or the screen.
.center {
position: absolute;
left: 50%;
top: 50%;
/*
Nope =(
margin-left: -25%;
margin-top: -25%;
*/
/*
Yep!
*/
transform: translate(-50%, -50%);
/*
Not even necessary really.
e.g. Height could be left out!
*/
width: 40%;
height: 50%;
}
You can do it in pure CSS now with the vh
(view height) unit. It's always relative to the screen size, not the element itself:
/* position element in middle of screen */
translateY(calc(50vh))
So if you know the height of the element (say height:320px
), you can move it exactly to the center of the screen.
/* moves the element down exactly off the bottom of the screen */
translateY(calc(50vh - 160px))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With