Introduction: Enhancing Web Designs

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!

Understanding Positioning in CSS

In CSS, the position property controls an element's location on a web page. Here’s what different position values do:

  • Static: By default, an element is static, occupying its natural place in the flow of the document.
  • Relative: A relative element can move from its natural place based on the top, right, bottom, left properties. Other elements still behave as if it's in its original position.
  • Absolute: The position is set relative to the nearest positioned ancestor, not from the top of the page.
  • Fixed: The element's position is "fixed" to the viewport, so it stays in the same place even when you scroll the page.

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:

<style>
  /* These styles illustrate four positioning types in CSS */

  .static {
    position: static;  /* No positioning adjustments */
    border: 3px solid #4AFF82;
  }

  .relative {
    position: relative;  /* Move element from normal position */
    top: 30px;  /* Pushes the box 30px down from its original position */
    left: 20px;  /* Pushes the box 20px to the right of its original place */
    border: 3px solid #F98404;
  }

  .absolute {
    position: absolute;  /* Position relative to nearest positioned ancestor */
    top: 60px;  /* Positions the box 60px from the top edge of its nearest positioned ancestor */
    right: 0;  /* Positions the box at the right edge of the nearest positioned ancestor */
    border: 3px solid #0275D8;
  }

  .fixed {
    position: fixed;  /* Element fixed in the viewport */
    bottom: 0;  /* Positions the box at the very bottom */
    border: 3px solid #7034A3;
  }
</style>

<!-- Our HTML elements with different positioning types -->
<div class="static">Static</div>
<div class="relative">Relative</div>
<div class="absolute">Absolute</div>
<div class="fixed">Fixed</div>

Note that the 7-symbol words (#4AFF82) in the border properties are hexadecimal notations of some colors.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal