CSS - Float and Clear
Tags: css, float, clear, layout
Published: 2020-08-25 22:41:26

<!-- wp:heading -->
<h2>Float Property</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The original purpose of the float was to take a block type element out of the natural flow but still related to the other elements and float it to the right or to the left of the parent container so that other inline elements or text can wrap around it.<br><br>In time people started using float as a way to build layouts, but as today there are technologies like flexbox, grid, and etc', so the use of floats is almost gone, apart from small tweaks here and there inside other elements.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The float property takes the element out of the natural flow of the page and floats it to the left or right based on the value, but it still considers the flow of other elements. Unlike the position absolute which takes the element completely out of the flow.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are a couple of values that we can use in the float property, the standard values are none, left and, right. In the Firefox browser (as of the time of writing of this article) there are two more additional values: the inline-end, and the inline-start values.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":5} -->
<h5>float: none</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is the default value for every element for the float property. Just as the value itself implies, the element is not under the influence of the float property and behaves as it "naturally" does.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":282,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image.png?w=908" alt="" class="wp-image-282"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>float: left</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will take the element and "float" it to the leftmost side of the parent container until it reaches the padding or border, if there are any other elements that also float right next to our element, they will be "floating" next to each other.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":293,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-8.png?w=907" alt="" class="wp-image-293"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>float: right</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will take the element and "float" it to the rightmost side of the parent container, and the same as the float: left, if any other element is also floated to the right it will be aligned next to it.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":284,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-2.png?w=908" alt="" class="wp-image-284"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>float: inline-start</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will push the element to the start of the typing direction if the text in the container is written Left-to-Right, so the element will float to the left. If the text is written Right-to-Left the element will float to the right.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":287,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-4.png?w=901" alt="" class="wp-image-287"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":288,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-5.png?w=905" alt="" class="wp-image-288"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>float: inline-end</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will push the element to the end of the typing direction if the text in the container is written Left-to-Right, so the element will float to the right. If the text is written Right-to-Left the element will float to the left.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":290,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-6.png?w=906" alt="" class="wp-image-290"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":292,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-7.png?w=898" alt="" class="wp-image-292"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Clear property</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As we learned the float property takes the element out of the natural flow of the document, but still gives the inline elements and text wrap around it, but what happens to the other block elements around it?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The block elements around a floated elements take the original place of the floated element and most likely to get "behind" the floated element, in order to deal with this we can use the clear CSS property.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The clear property dictates where float elements can be around our element. The standard values that can be used are none, left, right, and both, in addition to them, in the firefox browser (as of the time of writing of this article) there are two more additional values: the inline-start, and the inline-end values.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":5} -->
<h5>clear: none</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is the default value, it allows float elements to be from both sides of our element.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":296,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-10.png?w=905" alt="" class="wp-image-296"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>clear: left</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will clear all the floats that are on the left side of our element, or in other words, this will push our element's top edge under the bottom edge of the lowest floated element from the left.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":294,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-9.png?w=917" alt="" class="wp-image-294"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>clear: right</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will clear all the floats that are on the right side of our element, or in other words, this will push our element's top edge under the bottom edge of a lowest floated element from the right.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":300,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-12.png?w=910" alt="" class="wp-image-300"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>clear: both</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will not allow any float elements to be on both sides of our element, so it will push our element under the last and lowermost float in the same container.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":301,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-13.png?w=910" alt="" class="wp-image-301"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>clear: inline-start</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will not allow any float elements to the side of our element based on the text direction in the parent. If the text direction is LtR the clear will get the value "left" if the text direction is RtL the clear will get the value "right".</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":304,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-15.png?w=908" alt="" class="wp-image-304"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":305,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-16.png?w=907" alt="" class="wp-image-305"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>clear: inline-end</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This will not allow any float elements to the side of our element based on the text direction in the parent. If the text direction is LtR the clear will get the value "right" if the text direction is RtL the clear will get the value "left".</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":307,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-17.png?w=903" alt="" class="wp-image-307"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":308,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-18.png?w=907" alt="" class="wp-image-308"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":4} -->
<h4>This concludes my article that explains the float and clear properties and their different values.</h4>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Hope you like it, and stay tuned for my next article soon!</h4>
<!-- /wp:heading -->