A major question all CSS-ers have again and again is "how can I get this stupid element exactly where I want it to go on the page!?!?!". Traditionally, the answer lied in using floats and/or position attributes in combination with margins. You may still see this in use and may find it useful in some situations so we’ll start by learning about float based layouts. You’ll need to develop a mental model for what’s happening on the page when you float elements and when you use the different positioning types. Pay particular attention to which element acts as the parent of the element you’re messing with — there are some rules about that which get a bit confusing and can cause hair-pulling frustration. If you absolutely position an element inside an absolutely positioned element, where does the first element go?
position
attribute?top
left
right
and bottom
attributes?float
and position
?1.1 Watch the next short video to get a first look at how ‘float’ works in CSS, and make sure to practice using the code below (uncomment each line to see the effect shown in the video)
A visual demonstration of how CSS float and clear actually works. If you’ve ever been confused about the float and clear property to arrange block elements – this is the video for you.
Duration: 3 minutes
Here’s the code that reflects the structure presented in the video. Play around with the lines with the float
and clear
commands.
<head>
<style>
#red {
width: 100px;
height: 100px;
background-color: red;
/* float: left; */
}
#green {
width: 100px;
height: 100px;
background-color: green;
/* float: left; */
}
#blue {
width: 100px;
height: 100px;
background-color: blue;
/* float: left; */
/* clear: left; */
}
#yellow {
width: 100px;
height: 100px;
background-color: yellow;
/* float: left; */
/* clear: left; */
}
</style>
</head>
<body>
<div id="red"></div>
<div id="green"></div>
<div id="blue"></div>
<div id="yellow"></div>
</body>
1.2 Read CSS Floats 101 from A List Apart
Play around with the Positioning Tutorial / Widget from BarelyFitz Designs to see the differences between different positioning schemes.
Carefully go through the following articles on Centering Elements in HTML, and learn some of the fundamental CSS concepts and properties along with mastering the art of centering elements. It might not sound much, but you will be challenged to do this under various conditions as a Frontend developer.
4.1 Centering in CSS: A Complete Guide
4.3 How to Center Both Vertically and Horizontally
position: fixed
, stays on bottom while scrolling the page)This section contains helpful links to other content. It isn’t required, so consider it supplemental for if you need to dive deeper into something.
Video: Positioning Elements – amazing video giving a general overview of the different CSS position values and how they work.
How browsers position floats: a really nice explanation by Monica Dinculescu along with an interactive demo. This is a must read!
(To learn more about the line box
mentioned in the article, check out this StackOverflow question.)
Absolute Horizontal and Vertical Positioning in CSS from Smashing Magazine is a great resource for when you begin worrying about the finer details of positioning elements.
UPDATED: 19.01.2021
How browsers position floats
article by Monica Dinculescu