A position property is what allows you to manipulate the location of an element. It has multiple values and properties so that you can change the location based to your own liking. These assist when dealing with multiple elements or a parent and child element.

One of the first code examples we’re going to show is a positioned element in a relative position that will be taken down 10px. Relative is one of the many different values for the position property.

Here is a look at the definitions of each individual value:

  • Relative: The element will have settings at the top, bottom, left, and right properties of the relatively positioned element causing it to move from its regular position. Any content along with the positioned element will not adjust to make up for the fixed gap left by a moved element.
  • Static: All elements by default have a static position in order for the element to stick to the page flow. Any top/bottom or left/right index that is set will not have an effect on an element.
  • Absolute: Any element positioned relative to the nearest positioned parent element will be removed from the overall flow of the document
  • Fixed: This element is removed from the flow of the document, similar to an element that is absolutely positioned, the only difference between a fixed element and an absolute is that fixed positions are relative to the document, not any parent element, meaning their unaffected as well by scrolling.

Playing with the Properties

Using a child element with an absolute value is a good way to see how it interacts with the parent element. The parent element will act as if the child is not there.

While trying to set other values like left/right or bottom/top, you’ll find that the child element will respond to commands and dimensions from the document instead of its parent element. A form of absolute protest you could say.

The first thing you’ll want to do is to create two elements. We’re going to make them boxes so we can test them out. The parent box is going to be a relative box, nestled inside of the parent element will be child element that has an absolute position. Create it with these following dimensions, the CSS code will look something like this.

In order to display your text in the middle of the code example screen, add a little something at the bottom to do so.

Here is the code to play with and test out the position properties in action with relation to the parent and child elements with each respective absolute and relative value. As you’ll be able to tell, the position of a relative element is unaffected by scrolling, watch out for this then by using the proper values. Play around with this and learn how to develop a element based on its position and how it can react with others.

See the Pen CSS Position Example by Mike Colagrossi (@SantinoMPC) on CodePen.