Understanding CSS: Relative and Absolute Positioning Explained



This is a brilliantly delivered CSS video tutorial! It breaks down the difference between absolute and relative positioning in a way that really makes sense for anyone getting started with CSS.

Truly comprehending how to use the position: relative; and position: absolute; positioning declarations is necessary to have complete control over styling your documents through CSS. This is something that can be applied to just about any part of a document, which provides a high level of customization options.

When working with position: relative; the way the object in question is positioned is indeed relative to page margins of your document or relevant container. So for example, of you had a header that you wanted to be positioned in the upper left hand corner of the screen, then the code may look something like this.

h1 {
    position: relative;
    top: 5px;
    right: 5px;
}

Now compare that to position: absolute; where elements are positioned in relation to the browser window or parent element. So while relative positioning does not affect any other elements other than what is specifically targeted, absolute positioning can result in overlapping elements because objects are being placed in specific locations. If you wanted to position an image in the lower right corner of the screen, for instance, your code may look something like this.

img {
    position: absolute;
    bottom: 0px;
    right: 0px;
}

There are more ways to position things using CSS, but with these two basics fully understood you can accomplish a lot. If this simple CSS tutorial was helpful to you, you will also enjoy this post on the ‘cascading’ behavior of Cascading Style Sheets. Thanks for stopping by!