{"id":718,"date":"2017-03-18T12:48:15","date_gmt":"2017-03-18T07:18:15","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=718"},"modified":"2017-03-29T15:14:48","modified_gmt":"2017-03-29T09:44:48","slug":"what-is-a-clearfix","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/what-is-a-clearfix\/","title":{"rendered":"What is a clearfix"},"content":{"rendered":"<h4 id=\"defining-the-clearfix\"><strong><span style=\"color: #800000;\">Defining the Clearfix<\/span> <\/strong><\/h4>\n<p><strong>Clearfix:<\/strong><\/p>\n<ul>\n<li>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.<\/li>\n<li>It&#8217;s generally used in float layouts where elements are floated to be stacked horizontally.<\/li>\n<li>The\u00a0clearfix\u00a0is a way to combat the zero-height container problem for floated elements<\/li>\n<li>\u00a0All HTML elements (div, p, ul, li, a, etc&#8230;) are essentially a box, and can contain other boxes.<\/li>\n<li>A bigger box with more content (images, text, video, etc&#8230;) will make it&#8217;s parent box expand. Unless it&#8217;s taken out of the &#8220;flow of the document&#8221;<\/li>\n<\/ul>\n<h4 id=\"explanation-of-css-clearfix\"><span style=\"color: #800080;\"><strong>Explanation of CSS clearfix :<\/strong><\/span><\/h4>\n<ul>\n<li>Only two CSS declarations do that.\n<ul>\n<li>position:absolute\/fixed<\/li>\n<li>float:left\/right.<\/li>\n<\/ul>\n<\/li>\n<li>The parent essentially &#8220;forgets&#8221; that it contains children boxes and shrinks to only take up as much space as it needs.<\/li>\n<li>If you assign the parent box a background color and float or absolutely position all it&#8217;s children boxes, then you&#8217;ll see no color at all as your parent box has essentially collapsed completely<\/li>\n<li>Give the parent element a class of &#8220;clearfix&#8221; after having put the amazing clearfix CSS code in your stylesheet.<\/li>\n<li>Give the parent element a width of anything &amp; overflow:hidden.<\/li>\n<li>That strive to keep presentation in CSS &amp; content in HTML.<\/li>\n<li>However, this won&#8217;t work if it contains something like a drop down menu as the overflow:hidden would hide the drop down.<\/li>\n<li>Float the parent too<\/li>\n<li>You&#8217;ll have to deal with a clear:both at some point, but you can keep passing it on by keep floating a parent till you get to a point when you can clear it all.<\/li>\n<li>Clearfix is a &#8220;hack&#8221; used to contain\/enclose floats without structural markup.<\/li>\n<li>In short, it is a sophisticated\/elegant way to do the same thing as this:<\/li>\n<\/ul>\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 style=&quot;clear:both&quot;&gt;&lt;!-- this clears all previous floats --&gt;&lt;\/div&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>but without including that markup in the document.<\/li>\n<li>The magic relies in the use of <strong>::after<\/strong> (a pseudo-element)<\/li>\n<li>clearfix uses <strong>::after<\/strong> to generate content (via the content property), then style that content to clear previous floats.<\/li>\n<li>Because this method does not work in IE (&lt; 8) clearfix relies on haslayout .<\/li>\n<li>To make IE behave (giving a layout to a box assures that it will contain floats).<\/li>\n<li>Unfortunately, doing so creates very different constructs across browsers (see also new &#8220;<strong>block-formatting context<\/strong>\u201c).<\/li>\n<\/ul>\n<h4 id=\"clearfix-will-be-responsible-for\"><span style=\"color: #ff6600;\"><strong>Clearfix will be responsible for:<\/strong><\/span><\/h4>\n<ul>\n<ul>\n<li><strong>creating<\/strong> different margin boxes<\/li>\n<li><strong>collapsing<\/strong> margins differently across browsers<\/li>\n<li><strong>clearing<\/strong> floats differently across browsers (not children, but siblings)<\/li>\n<\/ul>\n<li>Clearfix is actually used to fix the issue when a parent div or block element contains floating children.<\/li>\n<li>Without clearfix the browser will not take into account the height of floated children when displaying the parent.<\/li>\n<li>So as an example, if you had a parent block with a background color that contained a floated child, the parent block would appear to stop behind the child or even above it.<\/li>\n<li>Adding the clearfix class to the parent ensures that the parent completely wraps the floated children.<\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><strong>How to use a Clearfix?<\/strong><\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">When applying classes to your HTML elements you should be mindful of which elements require a clearfix.<\/span><\/li>\n<li><span style=\"color: #000000;\">Any HTML element will have floated child elements will require a clearfix.<\/span><\/li>\n<li><span style=\"color: #000000;\">You can do this by adding the class clearfix to your existing classes ( assuming you have access to editing those classes).<\/span><\/li>\n<\/ul>\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<ul>\n<li>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<\/li>\n<\/ul>\n<p><strong>CSS :<\/strong><\/p>\n<p>This excellent CSS clear fix uses\u00a0<strong>:before\u00a0<\/strong>and\u00a0<strong>:after<\/strong>\u00a0pseudo-elements of the container to perform the clear:<\/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\">\/* Assuming this HTML structure:<br\/><br\/>\t&lt;div class=&quot;clear&quot;&gt;<br\/>\t\t&lt;div class=&quot;floated&quot;&gt;&lt;\/div&gt;<br\/>\t\t&lt;div class=&quot;floated&quot;&gt;&lt;\/div&gt;<br\/>\t\t&lt;div class=&quot;floated&quot;&gt;&lt;\/div&gt;<br\/>\t&lt;\/div&gt;*\/<br\/>.clear:before,<br\/>.clear:after <br\/>{<br\/>    content: &quot; &quot;;<br\/>    display: table;<br\/>}<br\/><br\/>.clear:after <br\/>{<br\/>    clear: both;<br\/>}<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Defining the Clearfix 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 \u00a0All HTML elements (div, [&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":[1258,1263,1264,1267,1269,1268,1259,1256,1261,1265,1270,1254,1266,1255,1262,1260,1257],"class_list":["post-718","post","type-post","status-publish","format-standard","hentry","category-css","category-html","tag-clearfix","tag-clearfix-bootstrap","tag-clearfix-css-w3schools","tag-clearfix-float","tag-clearfix-mixin","tag-clearfix-not-working","tag-clearing-floats-an-overview-of-different-clearfix-methods","tag-css-the-clearfix-hack","tag-css-clear-fix","tag-css-clearfix-2016","tag-how-to-use-clearfix","tag-html-what-is-a-clearfix","tag-micro-clearfix","tag-the-clearfix-force-an-element-to-self-clear-its-children","tag-the-very-latest-clearfix-reloaded","tag-understanding-the-humble-clearfix","tag-when-and-how-to-use-a-clearfix"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/718","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=718"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/718\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}