Home > Chapter Review and Exercises > Chapter 15 - Floating and Positioning > The Position Property
w3schools.com resources (including Going Beyond resources)
A Position Property example
w3schools.com: CSS Position Layout examples
w3schools: using absolute positioning to place text in an image
Additional resources
The CSS Position Property -- static, relative, fixed, absolute, and sticky
CSS z-index Property -- overlapping elements
CSS Animatable -- Animatable properties can change gradually from one value to another, like size, numbers, percentage and color.
CSS clip Property -- Lets you specify a rectangle to clip (i.e. to remove part of it) an absolutely positioned element. The rectangle is specified as four coordinates, all from the top-left corner of the element to be clipped.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample Table</title>
<style>
table, td, th {
border: 4px solid green;
}
table {
border-collapse: collapse;
width: 100%;
}
div.absolute {
position: absolute;
top: 10px;
right: 10px;
width: 200px;
height: 100px;
border: 3px solid red;
}
</style>
</head>
<body>
<!-- <table style="width: 100%;" border="2"> -->
<table style="width: 100%;">
<tbody>
<tr>
<td style="width: 100%; height: 120px; text-align:center;" colspan="3">
This is a very interesting newsletter<br>
This is a very interesting newsletter<br>
<div class="absolute"><img src="eagle.jpg" height="100px" width="200px"></div></td>
</tr>
<tr>
<td style="width: 25%; height: 200px; text-align:justify;">
This is the first column<br>
This is the first column<br>
This is the first column<br></td>
<td style="width: 50%; text-align:center; vertical-align: top;">
This is the middle column<br>
This is the middle column<br>
This is the middle column<br></td>
<td style="width: 25%; text-align:right;">
This is the last column<br>
This is the last column<br>
This is the last column<br></td>
</tr>
</tbody>
</table>
</body>
</html>
This is the table example from Chapter 8 modified to include an image of an eagle.
The div.absolute selector in the internal CSS specifies that the position property is to be applied only to <div> elements with the descriptor (i.e. property and value) of class="absolute".
Here is the div.absolute descriptor:
div.absolute {
position: absolute;
top: 10px;
right: 10px;
width: 200px;
height: 100px;
border: 3px solid red;
}
The properties (and their values) top: 10px; and right: 10px; are positioning the image relative to the nearest positioned ancestor, in this case the top row of the table.
The img element,
<img src="eagle.jpg" height="100px" width="200px">,
has the properties (and their values) of width: 200px; and height: 100px; which determine the size of the image.
The row that the eagle image is in has a height of 120 pixels:
<td style="width: 100%; height: 120px; text-align:center;" colspan="3">
The property
border: 3px solid red;
has values for the border width (3px), that it be a solid border, and that it be the color red.
In the second row, in the middle cell of the table, notice that the text is both centered and aligned to the top of the cell. The CSS for this is:
<td style="width: 50%; text-align:center; vertical-align: top;">
This is what the website looks like:
Copy this HTML and CSS into your text editor, use your own picture, or download the Eagle image, and see how changing the values of the properties properties top: 10px; and right: 10px; will re-position the picture. And instead of top: you can use bottom:, and instead of right: you can use left:
1. Position: static
HTML elements are position: static; by default. Static positioned elements are not affected by the top, bottom, left, and right properties. An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page.
2. Position: relative
An element with position: relative; is positioned relative to its normal position. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
3. Position: fixed
An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a gap in the page where it would normally have been located. Notice the fixed element in the lower-right corner of the page.
4. Position: absolute
An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body as its "positioned ancestor", and moves along with normal page scrolling. Note: A "positioned" element is one whose position is anything except static.
5. Position: sticky
An element with position: sticky; is positioned based on the user's scroll position. A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed; ).
6. Position: Overlapping Elements
When elements are positioned, they can overlap other elements. The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others). An element can have a positive or negative stack order.
>>>>>> Try changing the -1 to 1 and see what happens. <<<<<<
Mozilla position property page - there are some great examples here for you to try out
CSS Position Tutorial | Learn CSS For Beginners - this is the video we watched in class
CSS position properties (relative, absolute, fixed, position sticky, and floats) — Webflow video tutorial
CSS Position Explained In 6 Minutes - video tutorial
CSS Positioning: Position absolute and relative explained - video tutorial
CSS Positioning Tutorial - Relative vs. Absolute vs. Fixed vs. Sticky - video tutorial
CSS Positioning Tutorial for Beginners -video tutorial (but his accent is a bit hard to understand)
Learn CSS Position In 9 Minutes - video tutorial
w3schools CSS Positioning Tutorial - this is from w3schools.com