CSS3 Transition

Posted on Wednesday April 11, 2012 / by Eric Potvin

Have you ever visited a website on your mobile device (assuming it's yours or a friend's one) on a portrait and then switch to landscape? Have you noticed a nice smooth transition effect? or it was more a from a to point b type of thing?

Well if you wonder how to visually transition from a landscape view to a portrait view, here's how.

The setup is pretty easy. Simply choose an element you want to transition, for example H1 HTML tag and then apply the CSS for it.

HTML

<h1>Transition Text</h1>

CSS

* {
  margin: 0;
  padding: 0;
}
html,body {
  height: 100%;
}

body {
  background: #422A20;
  margin: 0;
  padding: 20% 0 0 0;
  width: 100%;
}

h1 {
  -moz-transition: all 1s linear 0s;
  -o-transition: all 1s linear 0s;
  -webkit-transition: all 1s linear 0s;
  color: #fff;
  display: block;
  font-size: 225%;
  margin: 0 auto;
  text-align: center;
  width: 95%;
}

@media screen and (min-width: 1025px) {
  h1 {
    font-size: 400%;
  }
}

For this example, if your resolution is greater than 1025 pixels, you will see the text taking almost all the width of the page. Once you resize the window to less than 1025 pixels you will see the text shrinking and at the same time see a nice smooth transition.

How that works?

First here's the syntax:

transition: property duration timing-function delay;

In this demo, the transition is set to use all the properties (All properties that can have an animated transition will do so). Then, the duration is set 1 second (The transition will last for 1 second). After this, the timing-function is set to linear. Here's the specifications:

Then finally, the delay is set to 0 seconds, well as you know it starts right away.

You can find all the information about transition on the mozilla website.