Sass / SCSS Mixin for Clearfix - best approach?

16,712

Solution 1

I should probably have added - this is the solution I came up with. I am still pretty new to all this so I don't know if this will actually do the same job as setting an element's class to clearfix and using the HTML5 boilerplate code above.

@mixin clearfix {
    zoom:1;
    &:before, &:after {
        content: "\0020"; 
        display: block; 
        height: 0; 
        overflow: hidden; 
    }
    &:after {
        clear: both;
    }
}

Edit: It's best to use @extend instead of a mixin as it will produce much less CSS code. Also, it's useful to use a silent class (denoted by %) when using @extend. This prevents unexpected CSS rules and eliminates the rule you're extending if it's not being used directly.

%clearfix {
    zoom:1;
    &:before, &:after {
        content: "\0020";
        display: block;
        height: 0;
        overflow: hidden;
    }
    &:after {
        clear: both;
    }
}

#head {
    @extend %clearfix;
    padding: 45px 0 14px; margin: 0 35px 0;
    border-bottom: 1px solid $border;
}

Solution 2

To achieve less code output by using @extend, define clearfix as placeholder (here just for modern browsers w/o IE 6+7):

Sass code:

%clearfix {
    &:after {
        content: " ";
        display: block;
        clear: both;
    }
}

/* Use above defined placeholder in the selector(s) of your needs via @extend: */
#container {
    position: relative;
    min-width: 42.5em;
    @extend %clearfix;
}

CSS output will be:

#container:after {
    content: " ";
    display: block;
    clear: both;
}
#container {
    position: relative;
    min-width: 42.5em;
}

Solution 3

// source http://www.alistapart.com/articles/getting-started-with-sass/
// http://nicolasgallagher.com/micro-clearfix-hack/

    @mixin clearfix {
     // For modern browsers
      &:before,
      &:after {
        content:" ";
        display:table;
      }

      &:after {
        clear:both;
      }

      // For IE 6/7 (trigger hasLayout)
      & {
        *zoom:1;
      }
    }

Solution 4

Why not use the Compass framework? It already provides mixins for clearfix among a host of other useful mixins and utilities. It's always better to look for existing tools rather than to have to maintain additional code yourself.

Share:
16,712
Richard Jordan
Author by

Richard Jordan

Introduction Experienced people manager, of technical teams of up to 40 people; highly productive senior software engineer; 20 year Silicon Valley veteran co-founder. Great mentor. Summary Richard has been working with internet technologies since prior the launch of the web. He learned to program in BASIC as a child on the original Apple II (which still sits on the desk in his home office). At university co-founded a company which built & sold computers for students and local businesses. On graduation, he spent three years in London in Enterprise IT roles, with significant success. Moved to Silicon Valley in 1999. Spent the next ten years in go-to-market roles with startups in the Enterprise Mobilization space. In 2009 he returned to development and fell in love with coding all over again. He has been developing full-time in Ruby since then, first in contract roles, then as a full stack developer at Teespring, followed by three years as a senior software engineer at Enjoy Technology. In 2018 took the helm at Voy, where he was hired by a regional airline to build product & engineering team for a spin-out startup that was shuttered when the parent company ran into financial difficulties. Led the engineering team at Snackation, before taking over as Director of Engineering at FinTech startup XCLAIM. Experience 2009 - Present: Development Roles 09/20 - PRESENT: XCLAIM Director of Software Engineering Lead engineering team of 8 engineers and one QA person. Ruby, Rails, React. 03/19 - 09/20: Snacknation Software Engineering Manager Managed engineering team. Architect. Code Design. Ruby, Rails, React. 01/18 - 03/19: Voy President & CTO/Architect Architect of product, built and led engineering team. Ruby, React, Blockchain 01/15 - 01/18: Enjoy Technology (3 years) Full Stack Software Engineer, Senior Used Ruby, Rails, JS, React to build commerce platform for smart last mile delivery. Designed, built and managed APIs for both internal use and external third parties. 04/14 - 12/14: Teespring (1 year) Full Stack Software Engineer Used Ruby, Rails, for e-commerce platform during period of hypergrowth. Built both back end and front end features. 06/11 - 03/14: Misc. Contract Roles (3 years) Developer, Lead Developer Contract roles with a startups & established businesses, primarily in Ruby (and Rails). 07/09 - 06/11: AppWhirl/App.Co (2 years) Co-Founder & CEO, Lead Developer (from 2010) Co-founder of AppWhirl (App.Co) - platform to 'make your own app free, in about five minutes'. 1999 - 2009: Go-to-market Roles, Silicon Valley Misc. roles with early-stage startups focused on go-to-market activities. Ask me about them - always happy to share.

Updated on June 23, 2022

Comments

  • Richard Jordan
    Richard Jordan almost 2 years

    I want to remove the clearfix class from my HTML and include a clearfix mixin in my SCSS (Rails 3.1 application). What is the best approach to this?

    I am thinking of just taking the HTML 5 Boilerplate clearfix and turning it into a mixin, then @including it within the CSS for elements that need clearfixing.

    Copied from HTML5 Boilerplate:

    /* The Magnificent Clearfix: Updated to prevent margin-collapsing on child elements. http://j.mp/bestclearfix */
    .clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; }
    .clearfix:after { clear: both; }
    
    /* Fix clearfix: blueprintcss.lighthouseapp.com/projects/15318/tickets/5-extra-margin padding-bottom-of-page */
    .clearfix { zoom: 1; }
    

    Is there a drawback to this? Is there a better way? Can I safely remove clearfix from my HTML markup this way?

  • Richard Jordan
    Richard Jordan over 12 years
    Ah it is working... There was an error elsewhere in the scss... still not sure if this is the best solution though... any other ideas out there?
  • Richard Jordan
    Richard Jordan over 12 years
    Yeah - I agree with using what's out there... the challenge I have is that compass seems quite dauntingly complicated... I looked at it and got intimidated so decided to roll my own...
  • Rhysyngsun
    Rhysyngsun over 12 years
    Compass has a lot of mixins and utilities, yes it can be overwhelming, but their documentation is nicely organized and more likely than not you're only going to use a small slice of what they provide initially. you can even view source within their docs in case you have any doubts as to how they implemented it.
  • Richard Jordan
    Richard Jordan over 12 years
    That's awesome advice Nathan - thanks - really appreciate it :-) ...you should have put this as a standalone answer so i could vote it up and you can get some more points :-) i will give something like this a try - what i am trying to avoid now (don't remember if it mattered when i first wrote this post) is the need to have non-semantic classes or anything else in my html
  • Richard Jordan
    Richard Jordan over 12 years
    having worked with both - i still prefer the solution that doesn't use extend
  • Rhysyngsun
    Rhysyngsun over 11 years
    @davidg I can't disagree with your point, but I felt pointing him in the direction of a framework that is going to solve a lot of other CSS boilerplate problems was more beneficial than giving him a mixin that, as I pointed out, he now has responsibility to maintain.
  • tbleckert
    tbleckert almost 11 years
    How can extend produce less css? If something, it should produce more code since the .clearfix itself is included as well.
  • Rob Erskine
    Rob Erskine almost 11 years
    This is the best answer, since it uses silent classes instead of a mixin or placeholder.
  • robertlyall
    robertlyall almost 10 years
    I'd recommend against using @extend for clearfix. Ideally, you should break clearfix into it's own utility class and use in your markup when needed to prevent repeated, unnecessary styling repetition. If you're going to extend, or use a placeholder/pseudo class; make sure you are strict, and only use it inside selectors that are nested one level or below.
  • Snymax
    Snymax almost 10 years
    you should use a placeholder class %clearfix... if your going to use extends rather then mixin