How to Use Absolute Positioning with Your Images
Your guide to using absolute bottom positioning with your images so that they are fixed and appear to peek over a designated break line.
You can create beautiful web pages by using small and simple CSS tricks such as absolute positioning with your images. This post walks you through how to use absolute positioning to enhance your web design.
What is absolute positioning?
Before we get to how you can do that, let's talk about what absolute positioning is. There are five ways to position elements on a page: static, relative, absolute, fixed, or sticky. By default, all the elements on the page have a static position.
Absolutely positioning an element will remove that element out of the page's workflow and will make it behave independently from all the other elements on the page. The absolutely positioned element can only be positioned in reference to its parent element. If it doesn't have a parent element, the HTML element will be its parent. The key to absolute positioning an element inside another element is to make the parent element's position relative and make the child element's position absolute:
In our example, the element that contains the image has absolute positioning and its direct parent which is the div element with the .container class has a position of relative. Also, a small note: Normally, we write "position: absolute;" or "position: relative" in our CSS file to make an element relatively or absolutely positioned but since we're using Bootstrap, we can take advantage of Bootstrap's shorthand utilities to configure positioning. We can simply give the tag elements quick positioning classes such as .position-static
, .position-relative
, .position-absolute
, .position-sticky
, and .position-fixed
.
CSS
Our image sits at the bottom of our container
div so if we position it as bottom: 0;
it will be positioned right at the bottom of the container. Over here we wanted to be a bit creative and make our astronaut sit on the ledge of our container so we gave it a bottom position value of -80px. That way half of the astronaut image will be below the container.
And there it is. With some simple tweaks to our HTML and CSS, we have an image that is fixed at the bottom of our section.