CSS Layouts Float and Position
Float
The float property can be used to change the normal document flow by floating elements to the left or right side of its container. And when elements are floated, they are removed from the normal flow though they remain part of the flow, resulting in the change of the positioning of the surrounding elements.
-
This will place the image to the left side of its container.
-
The text in the following paragraphs will fill the available area and line up right to the edge of the image.
-
We can add some margin in to create a little space between the image and the text.
-
By putting the background color on the paragraphs, it’s now easier to see that while the text is flowing around the image, the boundaries of the element is actually behind the image.
- Floated elements are taken out of the normal flow, so the other elements move up to fill the space as if the floated element was no longer there.
- But the content of those elements will still make room for the floated element and fill up the remaining space around it.
Clearing the Float
- You can clear the float using the
clear
property. - The values for clear can be
left
orright
to specifically clear a left or right float, but I like to use the third option,both
, which clears left and right floats.
What if there’s no element to apply the clear to?
Option 1: overflow
Overflow is actually used to specify how to display content that doesn’t fit in its container.
Different overflow values can be used to determine how to display the overflow of content.
hidden
clips the overflow of content, this may be used for decorative content, like an image, but isn’t very useful for text since clipped content can’t be accessed.auto
adds a scroll bar, but only when there is overflowing content.scroll
will always show a scroll bar on the X and Y axis, even when the content does not flow outside of its container.
Option 2: “clearfix” Hack
CSS snippet added to the parent of floated elements.
- This group of CSS styles need to be added to the parent of the floated elements.
- When adding the class to the HTML, do not include the colon after.
- This is a pseudo-element and is used in conjunction with another selector.
- This fix has been created and maintained by the developer community, and if you’re curious, the history of this fix can be found in this CSS tricks article.
Option 3: display
Not currently supported in all browsers.
- A newer way is in the works to solve the problem, using the display property with a value of flow-root.
- While this provides a solution, it is not currently supported by all modern browsers, and is still in the draft phase.
box-sizing
box-sizing: content-box
- The initial box-sizing value is content-box.
- Width and height values change the size of the content-box.
- But when padding and border styles are added, they increase the size of the elements, adding to the width and height of the content box.
- Margin just adds the space around the element.
box-sizing: border-box
- Using the border-box value instead, we’ll include the padding and border space within the dimensions set with the width and height properties.
- This will make the content-box smaller, but the overall element will maintain the same width and height values without any adjustments.
The Box Model Fix
- This CSS Layouts Float and Position > box-sizing border-box is often referred to as the box model fix.
- To use this fix, add this CSS code snippet to all of your projects.
- More Resources: 1, 2.
Position
The position property can also be used to change the flow of the document. There are 5 different values. Each responsible for a different type of positioning.
static
: not positionedrelative
: relative to current positionabsolute
: relative to containing elementfixed
: relative to the viewportsticky
: relative to containing element and viewport
Position is also used with a combination of the top, right, bottom, and left properties.
Z-Index
- Every time an HTML element is added within the same container, the elements are stacked in layers on the Z axis.
- An element with a higher stack level is rendered in front of the element with the lower stack level.
- Most of the time, it’s hard to see how the elements stack on the Z index, unless they overlap each other.
Default stacking order
- HTML > floated > in-line > positioned.
- We can use the Z index property to change this, but it only works if the element has some type of position applied to it.