DEV Community


Positioning and Changing the location of an Element.

Stella Mwanahamuntu
Hey, glad you stumbled on my account, this place place is great right?! Anyways, I make apps, websites and everything else pretty and usable. Javascript is pretty cool so I am into that too.
・2 min read

Unless otherwise, HTML elements flow statically from the left to the right in the same order they are declared in your HTML code.
CSS code comes in to provide a mechanism to specify some advanced options in the position of an element. The position property specifies the type of positioning method used for an element.
There are five different position values: static, relative, fixed, absolute, sticky.

Static Positioning

This is the default for every single page element in HTML. Different elements don't have different default values for positioning, they all start out as static.
A valid reason for static positioning is to forcefully remove positioning that got applied to an element outside of your control, maybe when using a css framework.

<div class="staticdiv">
  This div element has position: static;
.staticdiv {
  position: static;
  border: 3px solid #000456;

Absolute Positioning

Absolute positioning allows an element to be placed in an exact location specified, relative to its parent or container's border.
You can apply properties individually; left, right, top, bottom. The absolute position takes a distance parameter that specifies the relative distance of the object from a reference point based on the positioning attribute specified.

  <div class="relativediv">This div element has position: relative;
  <div class="absolutediv">This div element has position: absolute;</div>

.relativediv {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid #73ff57;

.absolutediv {
  position: absolute;
  top: 80px;
  right: 0;
  width: 200px;
  height: 100px;
  border: 3px solid #69DA21;

Relative Positioning

With relative positioning, the element is positioned relative to it's immediate left sibling's coordinates.

Fixed Positioning

Fixed positioning is rarely used. A fixed position element is positioned relative to the viewport or browser window. An element with position:fixed will stay right where it is when the page is scrolled.

The top, right, bottom, and left properties are used to position the element.

<div class="fixeddiv">
This div element has position: fixed;
.fixeddiv {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 300px;
  border: 3px solid #73AD21;

Sticky Positioning

The position sticky allows an element to be positioned based on the user's scroll position.
Sticky element toggles between relative and fixed positioning, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in a specified place.

<div class="stickydiv">I am sticky!</div>
.stickydiv {
  position: -webkit-sticky; /*for safari browsers*/
  position: sticky;
  top: 0;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;

Yep, that is what I have got on the five positioning values.
Please feel free to drop your comments and anything additional on positioning and location of elements.

Discussion (0)

Forem Open with the Forem app