{"id":1067,"date":"2017-03-20T11:23:39","date_gmt":"2017-03-20T05:53:39","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1067"},"modified":"2017-03-29T12:14:36","modified_gmt":"2017-03-29T06:44:36","slug":"methods-clearfix-can-use","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/methods-clearfix-can-use\/","title":{"rendered":"CSS &#8211; What methods of \u2018clearfix\u2019 can be use"},"content":{"rendered":"<h4 id=\"clearfix\"><span style=\"color: #ff6600;\"><strong>Clearfix :<\/strong><\/span><\/h4>\n<p>A\u00a0clearfix\u00a0is a way for an element to automatically clear its child elements, so that you don&#8217;t need to add additional markup. It&#8217;s generally used in float layouts where elements are floated to be stacked horizontally. The\u00a0clearfix\u00a0is a way to combat the zero-height container problem for floated elements.<\/p>\n<h4 id=\"when-and-how-to-use-a-clearfix\"><span style=\"color: #808000;\"><strong>When and How to use a Clearfix\u00a0 :<\/strong><\/span><\/h4>\n<p>A CSS clearfix is used to fix issues related to floating child elements within a parent element. Without a clearfix your child elements wont behave as you expect them too. When a HTML element is floated it can appear to sit outside its parent element and that means it wont adjust the parents height accordingly.<\/p>\n<p>It is important to remember that a clearfix is a type of hack used to fix problems associated with floating child elements within a parent element. Float was intended to be used; as an example \u2013 to position elements like images within a page. When you float an image left or right the text will wrap around it.<\/p>\n<p><span style=\"color: #800080;\"><strong>What Happens when you Don\u2019t use a Clearfix :<\/strong><\/span><\/p>\n<p>Lets assume you have a\u00a0div\u00a0with a class\u00a0container. Inside this div you have two more divs with the classes\u00a0content\u00a0and\u00a0sidebar\u00a0respectively and you want to float them so your content is on the left and your sidebar on the right.<\/p>\n<p>Without a clearfix the following would happen.<\/p>\n<h4 id=\"figure-i\"><span style=\"color: #ff6600;\"><strong>Figure I :<\/strong><\/span><\/h4>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-1080 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Pe1.png\" alt=\"\" width=\"521\" height=\"450\" \/><\/p>\n<p>Notice how the two floated Divs (elements) are outside the parent container and are only influenced by the padding at the top. In this case there is 20px of padding on all sides of the parent container. So both the content and sidebar will be pushed down by 10px. However we also wanted 10px of clearance on the left, right and bottom but since floated elements get pushed outside the parent container this is not happening.<\/p>\n<p>On a real websites the results are a whacky looking website.<\/p>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"figure-ii\"><span style=\"color: #800080;\"><strong>Figure II :<\/strong><\/span><\/h4>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1081 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Pe2.png\" alt=\"\" width=\"569\" height=\"396\" \/><\/p>\n<p>What your seeing here is the same as figure I on blog. The brown colored background is coming from the optin form right at the bottom. Without a clearfix it fills the space that should be occupied by the parent container which houses both the sidebar and content. The white space you see at the top is the padding.<\/p>\n<p>What Happens when you Include a Clearfix :<\/p>\n<p>When you include a clearfix the issues presented in figures I &amp; II will be rectified. Here is the same illustration shown in figure I with a clearfix.<\/p>\n<h4 id=\"figure-iii\"><span style=\"color: #808000;\"><strong>Figure III :<\/strong><\/span><\/h4>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1082 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Pe3.png\" alt=\"\" width=\"517\" height=\"351\" \/><\/p>\n<p>As you can see the parent container now wraps around the two child elements ( content and sidebar) and this gives us the desired behavior that we would expect. This would also fix the issue seen in figure II as the footer and optin form would sit below the container. You will also notice the padding ( seen in purple) also behaves as we would expect.<\/p>\n<p><strong>How to use a Clearfix?<\/strong><\/p>\n<p>When applying classes to your HTML elements you should be mindful of which elements require a clearfix. Any HTML element will have floated child elements will require a clearfix. You can do this by adding the class clearfix to your existing classes ( assuming you have access to editing those classes).<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Html Code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">&lt;div class=&quot;container clearfix&quot;&gt;<br\/>    &lt;div class=&quot;content&quot;&gt;other content stuff...&lt;\/div&gt;<br\/>    &lt;div class=&quot;sidebar&quot;&gt;Sidebar stuff...&lt;\/div&gt;<br\/>&lt;\/div&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>HTML classes can accept more than one class by separating them with a space. That means the container will accept CSS rules from both\u00a0.container\u00a0and\u00a0.clearfix.<\/p>\n<p><span style=\"color: #ff6600;\"><strong>Methods of Clearfix :<\/strong><\/span><\/p>\n<p>&#8220;Reloaded&#8221; clearfix<\/p>\n<p>&#8220;Beat That ClearFix&#8221;<\/p>\n<p>Micro Clearfix<\/p>\n<p>&#8220;Reloaded&#8221; clearfix :<\/p>\n<p>Making a strong argument for dropping\u00a0display: table\u00a0in favor of\u00a0display: block\u00a0which has the advantage of not disabling margin-collapse between containers.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Css Code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">.container::after {<br\/>    content:&quot;&quot;;<br\/>    display:block;<br\/>    clear:both;<br\/>}<\/code><\/pre> <\/div>\n<p><span style=\"color: #800080;\"><strong>&#8220;Beat That ClearFix\u201c :<\/strong><\/span><\/p>\n<p>when targeting modern browsers, we can now drop the\u00a0zoom\u00a0and\u00a0::before\u00a0property\/values and simply use:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Css Code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">.container::after {<br\/>    content:&quot;&quot;;<br\/>    display:table;<br\/>    clear:both;<br\/>}<\/code><\/pre> <\/div>\n<p>If you start a new project from scratch, go for it, but don\u2019t swap this technique with the one you have now, because even though you do not support oldIE, your existing rules prevent collapsing margins.&#8221;<\/p>\n<h4 id=\"micro-clearfix\"><span style=\"color: #808000;\"><strong>Micro Clearfix :\u00a0<\/strong><\/span><\/h4>\n<p>The most recent and globally adopted clearfix solution, the Micro Clearfix<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Css Code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">.container::before, .container::after {<br\/>    content:&quot;&quot;;<br\/>    display:table;<br\/>}<br\/>.container::after {<br\/>    clear:both;<br\/>}<br\/>.container {<br\/>    zoom:1; \/* For IE 6\/7 (trigger hasLayout) *\/<br\/>}<\/code><\/pre> <\/div>\n<h4 id=\"example-program\"><span style=\"color: #ff6600;\"><strong>Example Program:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Css Code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">\/* &gt;&gt; The Magnificent CLEARFIX &lt;&lt; *\/<br\/>.clearfix:after { <br\/>  content: &quot;.&quot;; <br\/>  display: block; <br\/>  height: 0; <br\/>  clear: both; <br\/>  visibility: hidden; <br\/>}<br\/>.clearfix { <br\/>  display: inline-block;  <br\/>}<br\/>* html .clearfix {  <br\/>  height: 1%;  <br\/>} \/* Hides from IE-mac \\*\/<br\/>.clearfix {  <br\/>  display: block;  <br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Clearfix : A\u00a0clearfix\u00a0is a way for an element to automatically clear its child elements, so that you don&#8217;t need to add additional markup. It&#8217;s generally used in float layouts where elements are floated to be stacked horizontally. The\u00a0clearfix\u00a0is a way to combat the zero-height container problem for floated elements. When and How to use a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,24],"tags":[2205,2201,2208,2209,2206,1267,2203,1268,1259,2210,1265,2204,2207,1254,1255,2202,1262],"class_list":["post-1067","post","type-post","status-publish","format-standard","hentry","category-css","category-html","tag-a-new-micro-clearfix-hack","tag-all-about-floats","tag-clearfix-css","tag-clearfix-css-bootstrap","tag-clearfix-css-example","tag-clearfix-float","tag-clearfix-method","tag-clearfix-not-working","tag-clearing-floats-an-overview-of-different-clearfix-methods","tag-contain-floats","tag-css-clearfix-2016","tag-css-tip-a-better-clearfix-with-sass","tag-div-clearfix-bootstrap","tag-html-what-is-a-clearfix","tag-the-clearfix-force-an-element-to-self-clear-its-children","tag-the-new-clearfix-method","tag-the-very-latest-clearfix-reloaded"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1067"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1067\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}