Welcome, eager learners! In today's exciting web design lesson, we're diving into CSS positioning, transitions, and animations. These dynamic enhancements breathe life into sterile web pages, creating vibrant and interactive digital experiences. Ready to jump in? Let's go!
In CSS, the position
property controls an element's location on a web page. Here’s what different position
values do:
top
, right
, bottom
, left
properties. Other elements still behave as if it's in its original position.The top
, right
, bottom
, and left
properties are used in conjunction with all positioning types except static. For relative positioning, these properties will "push" the element from its normal position to down
, left
, up
, and right
respectively. But for absolute and fixed, they position the element at a specific distance from the respectively top
, right
, bottom
, and left
edge of its containing element.
Here’s an illustration in code:
HTML, XML1<style> 2 /* These styles illustrate four positioning types in CSS */ 3 4 .static { 5 position: static; /* No positioning adjustments */ 6 border: 3px solid #4AFF82; 7 } 8 9 .relative { 10 position: relative; /* Move element from normal position */ 11 top: 30px; /* Pushes the box 30px down from its original position */ 12 left: 20px; /* Pushes the box 20px to the right of its original place */ 13 border: 3px solid #F98404; 14 } 15 16 .absolute { 17 position: absolute; /* Position relative to nearest positioned ancestor */ 18 top: 60px; /* Positions the box 60px from the top edge of its nearest positioned ancestor */ 19 right: 0; /* Positions the box at the right edge of the nearest positioned ancestor */ 20 border: 3px solid #0275D8; 21 } 22 23 .fixed { 24 position: fixed; /* Element fixed in the viewport */ 25 bottom: 0; /* Positions the box at the very bottom */ 26 border: 3px solid #7034A3; 27 } 28</style> 29 30<!-- Our HTML elements with different positioning types --> 31<div class="static">Static</div> 32<div class="relative">Relative</div> 33<div class="absolute">Absolute</div> 34<div class="fixed">Fixed</div>
Note that the 7-symbol words in the border
properties are hexadecimal notations of some colors.
Transitions in CSS let you gradually change property values, creating a smooth effect. By specifying the transition parameters like so:
CSS1transition: <transition-property> <transition-duration> <transition-timing-function>
we require to change the property <transition-property>
within <transition-duration>
amount of time (typically accepts values in seconds (s) or milliseconds (ms), e.g., 2s
or 2000ms
) using the function <transition-timing-function>
.
The timing function can have the following values:
The difference between 'ease' and 'ease-in-out' is that 'ease' speeds up sooner. For example, consider a button that slowly changes from blue to green when hovered over.
HTML, XML1<style> 2 /* This class produces a smooth color transition on hover */ 3 4 .button { 5 background-color: #4CAF50; /* Start color */ 6 transition: background-color 0.5s ease; /* Transition rule */ 7 padding: 15px; 8 border: none; /* removes the border from the element */ 9 color: white; 10 } 11 12 .button:hover { 13 background-color: #45a049; /* End color on mouse hover */ 14 } 15</style> 16 17<button class="button">Hover over me</button> <!-- Hover over this button to see transition -->
:hover
is called a pseudoclass, that applies a specific style to an element when hovered over. Do not worry if you don’t understand it now. We’ll go into more detail later on.
While transitions offer basic effects, animations
allow you to define multiple style changes at various points, creating complex visual effects. Like transitions, they go from one CSS style to another but offer much more control.
Animations are defined using keyframes
. Here’s an animation example:
HTML, XML1<!-- An example of using keyframes to create an animation --> 2<style> 3 @keyframes moving-box { 4 /* At the start, the box is at the left */ 5 0% {left: 0px;} 6 /* At the halfway point, the box moves to the right */ 7 50% {left: 200px;} 8 /* In the end, the box returns to the left */ 9 100% {left: 0px;} 10 } 11 12 .box { 13 position: relative; /* So we can use 'left' property */ 14 height: 50px; /* Box height */ 15 width: 50px; /* Box width */ 16 background-color: #4CAF50; /* Box color */ 17 animation: moving-box 2s linear infinite; /* Adding the animation */ 18 } 19</style> 20 21<div class="box"></div> <!-- Here's our animated box -->
Let's take a closer look at the following line:
CSS10% {left: 0px;}
This is a keyframe. 0%
represents the starting point of one cycle of the animation (100%
represents the ending point of the cycle, and any percentage in between represents the corresponding point in time within that cycle). The accompanying rule set specifies the CSS property (or properties) that are to be applied at this particular moment of the animation. In this case, the CSS left
property is set to 0px
, positioning the box at the left edge of its containing element when the animation starts. It's important to note that multiple properties could be defined within these braces, allowing multiple style changes to be animated simultaneously.
When adding an already defined animation to the element, we use the following property:
CSS1animation: <animation-name> <animation-duration> <animation-timing-function> <animation-iteration-count>
<animation-name>
is the name of the animation. <animation-duration>
and <animation-timing-function>
are similar to the transition parameters. <animation-iteration-count>
specifies how many times animation repeats, type 'infinite' for endless repetition.
In some scenarios, you might want your animation to rotate its direction every time it completes a cycle. In that case you add a parameter called <animation-direction>
to your animation
property with the value alternate
in the end. Specifically, the above code can be rewritten in the following way:
HTML, XML1<style> 2 @keyframes moving-box { 3 0% {left: 0px;} 4 100% {left: 200px;} 5 } 6 7 .box { 8 position: relative; 9 height: 50px; 10 width: 50px; 11 background-color: #4CAF50; 12 animation: moving-box 1s linear infinite alternate; 13 } 14</style> 15 16<div class="box"></div> <!-- Here's our animated box -->
Great job! By mastering CSS positioning, transitions, and animations, you have taken a significant step forward. Now, get ready for practice exercises to reinforce your knowledge. Keep practicing, and soon, you'll intuitively create visually stunning web designs. Let's proceed!