2082 8

CSC329 ยท 2082 ยท 5 marks

Create an HTML page containing three paragraphs p1, p2 and p3 with some contents. Write internal CSS to set the position of p1 to relative, p2 to float and p3 to absolute.

Create an HTML page containing three paragraphs p1, p2 and p3 with some contents. Write internal CSS to set the position of p1 to relative, p2 to float and p3 to absolute. [5]

Web TechnologyChapter 3: Cascading Style SheetsPositioning

HTML Page with CSS Positioning (p1: relative, p2: float, p3: absolute)

Concept (from notes)

  • position: relative - element is positioned relative to its normal position in the document flow.
  • float - used to float an element to the left or right, allowing other content to wrap around it.
  • position: absolute - element is positioned relative to the nearest positioned ancestor; removed from normal document flow and can overlap other elements.

HTML Code

<!DOCTYPE html>
<html>
  <head>
    <title>CSS Positioning Example</title>

    <style>
      /* p1: position relative */
      #p1 {
        position: relative;
        top: 20px;
        left: 30px;
        background-color: lightblue;
        width: 300px;
        padding: 10px;
      }

      /* p2: float */
      #p2 {
        float: left;
        background-color: lightgreen;
        width: 250px;
        padding: 10px;
        margin: 10px;
      }

      /* p3: position absolute */
      #p3 {
        position: absolute;
        top: 200px;
        left: 400px;
        background-color: lightyellow;
        width: 300px;
        padding: 10px;
      }
    </style>

  </head>

  <body>

    <h2>CSS Positioning Demo</h2>

    <p id="p1">
      This is paragraph P1. Its position is set to <strong>relative</strong>.
      It is shifted 20px from top and 30px from left relative to its
      normal position in the document flow.
    </p>

    <p id="p2">
      This is paragraph P2. It uses <strong>float: left</strong>.
      This element floats to the left and other content
      wraps around it.
    </p>

    <p id="p3">
      This is paragraph P3. Its position is set to <strong>absolute</strong>.
      It is positioned relative to the nearest positioned ancestor
      or the document body. It is removed from the normal document flow.
    </p>

  </body>
</html>

Output Description

ParagraphCSS PropertyEffect
p1position: relativeShifted 20px down and 30px right from its normal position
p2float: leftFloats to the left; other content wraps around it
p3position: absolutePlaced at exact coordinates (top: 200px, left: 400px) from the document body; removed from normal flow

Key Notes

  • Internal CSS is written inside the <style> tag placed within the <head> section.
  • The top, left, right, and bottom properties only work when the position property is explicitly set.
  • Absolute positioned elements are removed from normal flow and can overlap other elements.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.