[!] [=title "Week 13: Transitions"] [=content-]

We can make simple static pages, and we can add color and style to them. With `:hover` (see Week 13: Hover if you haven't already!) we can make webpages that react instantly to changes.

What if we want to react gradually? For instance, we might want a sidebar to expand slowly, or we might want a link to change color slowly instead of immediately. For this we use `transition`! Transition is a weird property in that it doesn't set anything itself - it only describes how to transition between other properties.

This means `transition` is completely useless if we don't have any properties changing on our page. Fortunately, `:hover` can change properties, so we can animate `:hover` with a transition!

The syntax looks like `transition: property duration;`, so for instance, `transition: background-color 1s;` literally means "take exactly 1 second to go from the old background color to the new one whenever it changes". Applying that to the example from the Hover article:

    #my_button {
            padding: 10px;
            background-color: purple;
            color: white;
    	transition: background-color 1s;
    }
    #my_button:hover {
            background-color: white;
            color: purple;
    }

The result looks like this: My Button

Try hovering it - it will take exactly 1 second for it to go from purple to white, and 1 second to go back!

Note that the color still changes instantly - we never told CSS to transition the color, so it didn't. Transition supports comma-separated values like this: `transition: background-color 1s, color: 1s;`, so let's try that!

    #my_button {
            padding: 10px;
            background-color: purple;
            color: white;
    	transition: background-color 1s, color 1s;
    }
    #my_button:hover {
            background-color: white;
            color: purple;
    }

The result looks like this: My Button

Much smoother.

It bears note that not every property can `transition`! For instance, `font-family` can't be transitioned - what would that even do? We call this animatable - any property that is not animatable will not do anything in a transition (or animation - we'll get to animations later). A list of every property that can be transitioned is found on W3Schools.

You can transition things that change the layout! CSS will recompute the layout for you. For instance, if I have,

    #my_text_block {
    	margin-left: 200px;
    	margin-right: 200px;
    	background-color: green;
    	transition: margin 1s; /* note that this transitions margin-left and margin-right automatically -
    				  I could specify them separately, but that's a waste of time! */
    }
    #my_text_block:hover {
    	margin-left: 0px;
    	margin-right: 0px;
    }

Then the result is something like,

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et volutpat nisi. Pellentesque id accumsan arcu. Nunc tempus, lorem ut venenatis blandit, quam orci pretium neque, ac bibendum nulla ex vel lectus. Curabitur viverra justo orci, eget porta erat ultrices a. In dictum ante nec quam semper ultrices. Pellentesque ac interdum tortor. Nam ultrices risus risus, vel vulputate turpis faucibus sed. Aenean eget urna vulputate, vulputate tortor molestie, varius dui. Quisque id ipsum auctor, dapibus lacus eget, laoreet eros. Morbi dignissim velit sed ipsum viverra, egestas efficitur nisl pretium. Cras eu porta turpis.

Try hovering it - it'll expand out to the edges!

Transitions can also be used with transforms and positions! This is very fun; I'm not going to go through any examples, but I highly recommend playing with it. If you need to brush up on `transform`, check out MDN's bit on the topic (an example transition for a simple 2-second animation would be `transition: transform 2s;`).

[/] [#template.html]