 
    
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>learn Archives - Complete, Concrete, Concise</title>
	<atom:link href="https://complete-concrete-concise.com/tag/learn/feed/" rel="self" type="application/rss+xml" />
	<link>https://complete-concrete-concise.com/tag/learn/</link>
	<description>Practical Information Without The Bloat</description>
	<lastBuildDate>Sat, 18 Mar 2023 19:55:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>CSS &#8211; How and When to Use Float</title>
		<link>https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-how-and-when-to-use-float/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Mon, 25 Jun 2018 12:54:41 +0000</pubDate>
				<category><![CDATA[Front-End Basics]]></category>
		<category><![CDATA[box-sizing]]></category>
		<category><![CDATA[calc()]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">https://complete-concrete-concise.com/?p=3743</guid>

					<description><![CDATA[<p>Floats are easier to use when you know when and how you should be using them.</p>
<p>The post <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-how-and-when-to-use-float/">CSS &#8211; How and When to Use Float</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="preamble">While <span class="css-property">float</span> is not ideal for every column organizing idea, it is a tried and true method and works well with older web browsers.<br />
In this tutorial we are going to look at some best practices for the use of floating HTML elements.</div>
<h2>Contents</h2>
<ol type="1">
<li><a href="#divide-the-page-so-floats-are-in-their-own-block">Divide the Page so Floats are in their own Block</a></li>
<li><a href="#float-everything-in-the-same-direction">Float Everything in the Same Direction</a>
<ol type="1">
<li><a href="#everything-floating-left">Everything Floating Left</a></li>
<li><a href="#everything-floating-right">Everything Floating Right</a></li>
<li><a href="#mixed-left-and-right-floats">Mixed Left and Right Floats</a></li>
</ol>
</li>
<li><a href="#when-you-want-floats-to-overlap-other-blocks">When You Want Floats to Overlap other Blocks</a></li>
<li><a href="#float-and-box-sizing"><span class="css-property">float</span> and <span class="css-property">box-sizing</span></a></li>
<li><a href="#calc"><span class="css-function">calc()</span></a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#playing-around">Playing Around</a></li>
<li><a href="#references">References</a></li>
</ol>
<h2 id="divide-the-page-so-floats-are-in-their-own-block">Divide the Page so Floats are in their own Block</h2>
<p>When you <span class="html-tag">div</span>ide up a page, you want to ensure that floating elements are contained inside their own stacking block and <span class="css-class">.clearfix</span> is applied to that block.<br />
Notice how the 3 small floating images are in their own block, with other elements stacking above and below:</p>
<div style="width: 70%; background: yellow; text-align: center; height: 50px; margin: auto; padding-top: 16px;">Header section</div>
<div style="width: 70%; background: green; margin: auto;">
<div style="width: 80%; background: cyan; margin: auto; padding: 2em 0 2em 0; text-align: center;">Large image goes here.</div>
<div style="border: 0.5em solid black;">
<div style="width: 80%; margin: auto;">
<div style="width: 30%; height: 50px; margin: 1.66%; background: lime; text-align: center; float: left;">Smaller image</div>
<div style="width: 30%; height: 50px; margin: 1.66%; background: lime; text-align: center; float: left;">Smaller image</div>
<div style="width: 30%; height: 50px; margin: 1.66%; background: lime; text-align: center; float: left;">Smaller image</div>
<div style="clear: both;"></div>
</div>
</div>
</div>
<div style="width: 70%; background: fuchsia; text-align: center; margin: auto;">Footer section</div>
<p>In general, you want to use floats to make elements flow horizontally inside a vertically stacking block.<br />
As much as possible, avoid having floating elements actually floating on top of other elements &#8211; but there are exceptions.</p>
<h2 id="float-everything-in-the-same-direction">Float Everything in the Same Direction</h2>
<p>As a general rule, you should float all your elements in the same direction. For English language websites (or any other language that is written from left-to-right), you want to use <span class="css-property">float: left</span>.</p>
<h3 id="everything-floating-left">Everything Floating Left</h3>
<p>Consider the following <span class="css-value">left</span> floating elements which overflow and wrap:</p>
<div style="text-align: center; line-height: 4em;">
<div style="width: 40%; background: yellow; float: left;">float:left</div>
<div style="width: 40%; background: lime; float: left;">float:left</div>
<div style="width: 40%; background: orange; float: left;">float:left</div>
<div style="clear: both; margin: 1em;"></div>
</div>
<p>While there is too much white space on the right side, the overall flow and alignment look fine.</p>
<h3 id="everything-floating-right">Everything Floating Right</h3>
<p>Consider the following <span class="css-value">right</span> floating elements which overflow and wrap:</p>
<div style="text-align: center; line-height: 4em;">
<div style="width: 40%; background: yellow; float: right;">float:right</div>
<div style="width: 40%; background: lime; float: right;">float:right</div>
<div style="width: 40%; background: orange; float: right;">float:right</div>
<div style="clear: both; margin: 1em;"></div>
</div>
<p>There is too much white space on the left side and the right alignment looks somewhat “unnatural” or out of place.<br />
Floating to the right is a good choice for webpages written in languages written from right to left.<a id="fnref1" class="footnote-ref" href="#fn1"><sup>1</sup></a></p>
<h3 id="mixed-left-and-right-floats">Mixed Left and Right Floats</h3>
<p>Consider the following floating elements which overflow and wrap. The left-most elements are floated <span class="css-value">left</span> and the right-most element is floated to the <span class="css-value">right</span>:</p>
<div style="text-align: center; line-height: 4em;">
<div style="width: 40%; background: yellow; float: left;">float:left</div>
<div style="width: 40%; background: lime; float: left;">float:left</div>
<div style="width: 40%; background: orange; float: right;">float:right</div>
<div style="clear: both; margin: 1em;"></div>
</div>
<p>The way the content overflows doesn’t look quite right.</p>
<h2 id="when-you-want-floats-to-overlap-other-blocks">When You Want Floats to Overlap other Blocks</h2>
<p>There are times when you want a floating element to overlap other blocks. The most common is getting text to flow around an image:</p>
<div class="html-output"><img decoding="async" style="float: left; margin-right: 1em; width: 125px;" src="https://complete-concrete-concise.com/wp-content/uploads/2018/05/26-dog-compressor.jpg">Having text flow around an embedded image looks natural and is what most people would expect because this is a common layout style we find in books and magazines.<br />
So this is one case where it makes sense to allow a floating image block to overlap with paragraph blocks.</div>
<p>Another common case is when you want to insert a side note or comment as a visible part of your content.<br />
Consider the following (although you might want to justify the text to make it less ragged):</p>
<div class="html-output">Another common use for a float that overlaps HTML blocks in your content is when you want to insert some sort of side note for the reader.</p>
<div style="float: right; background: cornsilk; border: 1px solid black; margin-left: 1em; width: 35%; padding: 0.5em;">According to legend, Galileo Galilei dropped two canon balls of different mass from the top of the tower to show they fell at the same speed.</div>
<p>For example, you might be writing a document on the Tower of Pisa &#8211; which is more commonly known as the “Leaning Tower of Pisa”.<br />
In the course of the article you will likely mention a number of facts about the tower. You might also want to mention something that doesn’t really belong in the main text, but is still a fun fact.<br />
One way to do this would be to use a footnote or endnote. Another way, would be to insert it into the main text, but set it apart.</p>
</div>
<p>When you choose to deliberately overlap HTML blocks, you need to be careful about ensuring the floating element does not overlap blocks it shouldn’t.</p>
<h2 id="float-and-box-sizing"><span class="css-property">float</span> and <span class="css-property">box-sizing</span></h2>
<p>Earlier, we saw <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/nine-more-css-properties-to-play-with#box-sizing">the <span class="css-property">box-sizing</span> property</a> which instructs the User Agent (browser) to size HTML elements by either content area (default) or border. Recall the CSS box model:<br />
<img decoding="async" src="https://complete-concrete-concise.com/wp-content/uploads/2018/04/15-css-box-model.png"><br />
Sizing using the <span class="css-value">content-box</span> does not include the padding, border, or margin when setting the size of the HTML element.<br />
Sizing using the <span class="css-value">border-box</span> includes the padding and border when setting the size of an HTML element. It does not include the margin in the size of the element.<br />
For <strong>normal flow</strong> objects, this doesn’t make much difference to the layout since objects just flow downwards.<br />
When we apply <span class="css-property">float</span> to objects, they flow horizontally and will overflow and wrap around when they reach the edge of the browser &#8211; so we have to be more careful when specifying dimensions for floating objects. It is usually best to use <span class="css-property">box-sizing: border-box</span> for floating HTML elements. Consider the following styling and code:</p>
<div id="cb1" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb1-1" class="sourceLine" data-line-number="1"></a><span class="fu">.parent</span> {
<a id="cb1-2" class="sourceLine" data-line-number="2"></a>  <span class="kw">margin</span>: <span class="dv">auto</span>;
<a id="cb1-3" class="sourceLine" data-line-number="3"></a>  <span class="kw">width</span>: <span class="dv">300px</span>;
<a id="cb1-4" class="sourceLine" data-line-number="4"></a>  <span class="kw">background-color</span>: <span class="dv">yellow</span>;
<a id="cb1-5" class="sourceLine" data-line-number="5"></a>}
<a id="cb1-6" class="sourceLine" data-line-number="6"></a>
<a id="cb1-7" class="sourceLine" data-line-number="7"></a><span class="fu">.clearfix</span><span class="in">::after</span> {
<a id="cb1-8" class="sourceLine" data-line-number="8"></a>  <span class="kw">clear</span>: <span class="dv">both</span>;
<a id="cb1-9" class="sourceLine" data-line-number="9"></a>  <span class="kw">display</span>: <span class="dv">block</span>;
<a id="cb1-10" class="sourceLine" data-line-number="10"></a>  <span class="kw">content</span>: <span class="st">""</span>;
<a id="cb1-11" class="sourceLine" data-line-number="11"></a>}
<a id="cb1-12" class="sourceLine" data-line-number="12"></a>
<a id="cb1-13" class="sourceLine" data-line-number="13"></a><span class="fu">.block</span> {
<a id="cb1-14" class="sourceLine" data-line-number="14"></a>  <span class="kw">width</span>: <span class="dv">100px</span>;
<a id="cb1-15" class="sourceLine" data-line-number="15"></a>  <span class="kw">height</span>: <span class="dv">50px</span>;
<a id="cb1-16" class="sourceLine" data-line-number="16"></a>  <span class="kw">background-color</span>: <span class="dv">red</span>;
<a id="cb1-17" class="sourceLine" data-line-number="17"></a>  <span class="kw">float</span>:<span class="dv">left</span>;
<a id="cb1-18" class="sourceLine" data-line-number="18"></a>}</code></pre>
</div>
<div id="cb2" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb2-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"parent clearfix"</span><span class="kw">&gt;</span>
<a id="cb2-2" class="sourceLine" data-line-number="2"></a>  <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"block"</span><span class="kw">&gt;&lt;/div&gt;</span>
<a id="cb2-3" class="sourceLine" data-line-number="3"></a>  <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"block"</span><span class="kw">&gt;&lt;/div&gt;</span>
<a id="cb2-4" class="sourceLine" data-line-number="4"></a>  <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"block"</span><span class="kw">&gt;&lt;/div&gt;</span>
<a id="cb2-5" class="sourceLine" data-line-number="5"></a><span class="kw">&lt;/div&gt;</span></code></pre>
</div>
<p>Which results in:</p>
<div class="html-output">
<div style="margin: auto; width: 300px; background-color: yellow;">
<div style="width: 100px; height: 50px; background-color: red; float: left;"></div>
<div style="width: 100px; height: 50px; background-color: red; float: left;"></div>
<div style="width: 100px; height: 50px; background-color: red; float: left;"></div>
<div style="clear: both;"></div>
</div>
</div>
<p>There is no obvious separation between one child block and another, since they all run together. The obvious solution is to insert a bit of space between them. We can do this by giving each child block a small margin (let’s say 10px all around):</p>
<div class="html-output">
<div style="margin: auto; width: 300px; background-color: yellow;">
<div style="width: 100px; height: 50px; background-color: red; float: left; margin: 10px;"></div>
<div style="width: 100px; height: 50px; background-color: red; float: left; margin: 10px;"></div>
<div style="width: 100px; height: 50px; background-color: red; float: left; margin: 10px;"></div>
<div style="clear: both;"></div>
</div>
</div>
<p>Unfortunately, this causes the child blocks to overflow and wrap around. Remember:</p>
<ol type="1">
<li>the parent container is 300px wide,</li>
<li>each child is 100px wide,</li>
<li>each child has a 10px margin,</li>
<li>this makes each child block 100px + 10px + 10 px = 120px wide (there are 10px for the left margin and 10px for the right margin) and</li>
<li>50px + 10px + 10px = 70px high (there are 10px for the top margin and 10px for the bottom margin)</li>
</ol>
<p>The height of the child blocks is not the problem for us, it is the width, so we can adjust our CSS to account for the 10px margin on the left and right of the child block:</p>
<div id="cb3" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb3-1" class="sourceLine" data-line-number="1"></a><span class="fu">.block</span> {
<a id="cb3-2" class="sourceLine" data-line-number="2"></a>  <span class="kw">margin</span>: <span class="dv">10px</span>;
<a id="cb3-3" class="sourceLine" data-line-number="3"></a>  <span class="kw">width</span>: <span class="dv">80px</span>;
<a id="cb3-4" class="sourceLine" data-line-number="4"></a>  <span class="kw">height</span>: <span class="dv">50px</span>;
<a id="cb3-5" class="sourceLine" data-line-number="5"></a>  <span class="kw">background-color</span>: <span class="dv">red</span>;
<a id="cb3-6" class="sourceLine" data-line-number="6"></a>  <span class="kw">float</span>:<span class="dv">left</span>;
<a id="cb3-7" class="sourceLine" data-line-number="7"></a>}</code></pre>
</div>
<p>Which gives us the result we would expect:</p>
<div class="html-output">
<div style="margin: auto; width: 300px; background-color: yellow;">
<div style="width: 80px; height: 50px; background-color: red; float: left; margin: 10px;"></div>
<div style="width: 80px; height: 50px; background-color: red; float: left; margin: 10px;"></div>
<div style="width: 80px; height: 50px; background-color: red; float: left; margin: 10px;"></div>
<div style="clear: both;"></div>
</div>
</div>
<p>If we decide to add a border (even just a 1px border) or padding (even just 1px of padding), it will cause the elements to wrap (again).<br />
We can compensate by subtracting the added widths of our border and padding, or we can set the <span class="css-property">box-sizing</span> property to <span class="css-value">border-box</span>:</p>
<div id="cb4" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb4-1" class="sourceLine" data-line-number="1"></a>* {
<a id="cb4-2" class="sourceLine" data-line-number="2"></a>  <span class="kw">box-sizing</span>: <span class="dv">border-box</span>;
<a id="cb4-3" class="sourceLine" data-line-number="3"></a>}</code></pre>
</div>
<p>Using the <strong>universal</strong> selector will cause all HTML elements to be sized according to their borders and not their content area. Personally, I think it is easier and more intuitive to work this way. When you layout the content on a page you imagine each block of content as being completely self contained &#8211; i.e., it includes the content, the padding around the content, and the border around the content. However, you do have to compensate for any margins you might use.</p>
<h2 id="calc"><span class="css-function">calc()</span></h2>
<p>We’ve seen that <strong>%</strong> is a more convenient way of dimensioning floating elements than using pixels. If you want 3 equally sized columns you can make them each <span class="css-value">33.33%</span> of the width of the parent container. You could also make 2 of them <span class="css-value">25%</span> and one <span class="css-value">50%</span>. Unfortunately, this makes compensating for margins a bit trickier.<br />
CSS has a function called <span class="css-function">calc()</span> that makes this a lot easier. You simply enter the dimensions &#8211; even if they are different units &#8211; and <span class="css-function">calc()</span> will perform the necessary calculation for you.<br />
Taking the example above, we can write the CSS code for the blocks as follows:</p>
<div id="cb5" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb5-1" class="sourceLine" data-line-number="1"></a><span class="fu">.block</span> {
<a id="cb5-2" class="sourceLine" data-line-number="2"></a>  <span class="kw">margin</span>: <span class="dv">10px</span>;
<a id="cb5-3" class="sourceLine" data-line-number="3"></a>  <span class="kw">width</span>: calc(<span class="dv">33.33%</span> - <span class="dv">10px</span> - <span class="dv">10px</span>);
<a id="cb5-4" class="sourceLine" data-line-number="4"></a>  <span class="kw">height</span>: <span class="dv">50px</span>;
<a id="cb5-5" class="sourceLine" data-line-number="5"></a>  <span class="kw">background-color</span>: <span class="dv">red</span>;
<a id="cb5-6" class="sourceLine" data-line-number="6"></a>  <span class="kw">float</span>:<span class="dv">left</span>;
<a id="cb5-7" class="sourceLine" data-line-number="7"></a>}</code></pre>
</div>
<p>The <span class="css-property">width</span> is set to <span class="css-value">33.33%</span> less <span class="css-value">10px</span> for the left margin and <span class="css-value">10px</span> for the right margin. It could have also been written as <span class="css-function">calc(33.33% &#8211; 20px)</span>, but I chose to <strong>explicitly</strong> subtract each margin independently for clarity.</p>
<h2 id="summary">Summary</h2>
<ol type="1">
<li>Divide the vertically stacking blocks</li>
<li>Try to contain horizontally stacking blocks (floating elements) in their own vertically stacking block.</li>
<li>As a general rules, you want all your floats to go in the same direction. In most cases this will be <span class="cwss-property">float: left</span>. If you are developing webpages for a language that is written right-to-left, you will prefer [float: right]{css-property}</li>
<li>Mixing <span class="cwss-property">float: left</span> and <span class="cwss-property">float: right</span> in the same containing block is, usually, a bad idea.</li>
<li>Sometimes, you do want floating elements to overlap other blocks (like images, or side notes). In this case, you need to ensure appropriate overlapping.</li>
<li>Setting your HTML elements to have <span class="css-property">box-sizing: border-box</span> makes it easier to layout horizontally flowing elements, since it takes into account any border and padding values you set.</li>
<li>Regardless of which <span class="css-property">box-sizing</span> model you choose, you must always compensate for any margin values you apply.</li>
<li>The CSS function <span class="css-function">calc()</span> makes it easier to compute box sizes. It even allows mixing units (like pixels and % together).</li>
</ol>
<h2 id="playing-around">Playing Around</h2>
<h3 id="dividing-up-a-page">Dividing Up a Page</h3>
<p>Learning how to divide up a page layout into HTML block elements is crucial to being able to code that page.</p>
<ol type="1">
<li>Explore a number of websites (news websites tend to be very good for this<a id="fnref2" class="footnote-ref" href="#fn2"><sup>2</sup></a>) and pay attention to how the pages are laid out.</li>
<li>How would you arrange the various HTML elements to create a similar layout?
<ol type="1">
<li>Which are the vertically flowing blocks?</li>
<li>How are vertical elements nested inside of blocks?</li>
</ol>
</li>
<li>Can you code a page with a similar layout?</li>
</ol>
<p>Many of these sites have very long pages. It is not necessary to replicate the entire page from top to bottom &#8211; just focus on the portion of the page you see in your browser window.<br />
Don’t try to mimic all the functionality or behaviour. You haven’t learned to embed videos yet (just substitute a picture). There are effects (like shadows, animations, or dropdowns) that you haven’t learned either. Just focus on getting the general layout and look, more or less, right.</p>
<h3 id="box-sizing-and-calc"><span class="css-property">box-sizing</span> and <span class="css-function">calc()</span></h3>
<ol type="1">
<li>Experiment with using <span class="css-property">box-sizing: border-box</span>. You can do this by setting all HTML elements to have this property.</li>
<li>Experiment using the <span class="css-function">calc()</span> function.</li>
</ol>
<p>You can use these with the page layouts you try out above.</p>
<h2 id="references">References</h2>
<ol type="1">
<li><a href="https://www.w3.org/TR/CSS21/visuren.html#propdef-float"><span class="css-property">float</span> property</a></li>
<li><a href="https://www.w3.org/TR/CSS21/visuren.html#propdef-clear"><span class="css-property">clear</span> property</a></li>
<li><a href="https://drafts.csswg.org/css-ui-3/#box-sizing"><span class="css-property">box-sizing</span> property</a></li>
<li><a href="https://drafts.csswg.org/css-values-3/#calc-notation"><span class="css-function">calc()</span> function</a></li>
</ol>
<div class="prev"><a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-a-simple-page-using-float">Prev</a></div>
<div class="next"><a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-a-more-advanced-float-example-part-1/">Next</a></div>
<div class="clear"></div>
<section class="footnotes">
<hr>
<ol>
<li id="fn1">The most commonly used RTL languages are Arabic, Hebrew, Persian, and Urdu.<a class="footnote-back" href="#fnref1">↩</a></li>
<li id="fn2">Some sites that I find interesting for their layout include: <a href="https://www.cbc.ca/">CBC</a>, <a href="https://bbc.co.uk">BBC</a>, <a href="https://www.engadget.com/">Engadget</a>. Of course, you are free to try and copy the look and layout of other sites as well.<a class="footnote-back" href="#fnref2">↩</a></li>
</ol>
</section>
<p>The post <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-how-and-when-to-use-float/">CSS &#8211; How and When to Use Float</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
