Not sleep… And not the band.
I was reading Breaking up responsive design on Yoast.com and, like pretty much everyone else, they’ve been been scaling down the browser base font to 62.5% for a while now. The default font-size for most browsers is, as it happens, 16px. The problem with that 62.5% call is that, at least on Chrome for Mac, I ended up getting a flash of 25.6px fonts before the page would load right. Turns out that wasn’t just me, and it’s the bane of 62.5%
So the fix would be to make my font 100%:
html { -webkit-text-size-adjust: 100%; font-size: 100%; }
And then I’d have to re-do all my font calls in my CSS because they were all based on 14px. This made me stare at my child theme on another site for a while, and finally knuckle down and script in better font handling, because the idea of manually cranking through everything was too daunting, even if I used REM Calculator to sort out my px/em ratio.
Still, doing this still meant I’d still have a lot of stuff like this:
font-size: 16px; font-size: 1rem;
Bah. Who want’s that? It was time to get sassier. I went back to Tracy and came up with her use of “Mixins.” And this is where Sass gets weird. It can do my math for me. This was, I admit, what originally made me interested. But in July it was a theoretical attraction. By December it was love. See, Tracy did the math with her sass-responsive library, which she calls a library of variables, mixins and formulas geared towards Responsive Web Design using Sass.
I called it _taupecat.scss
but we’ll get to that in a second.
The first step would be to split up my files. See, I’m using Genesis, and they have a handy table of contents for all the sections, so I just made each section an import:
/* HTML5 Reset --------- */ @import "normalize"; /* Defaults --------- */ @import "defaults"; /* Structure and Layout --------- */ @import "structure";
I admit, this gives me a million files, as you can see in that image on the right, but it’s actually pretty easy to figure out what’s where. It took me a while to get everything where I wanted it, and while I could have used Bones for Genesis, I know me, and like using Git is the fastest way to learn it, so is using Sass. So I started with the basics of splitting it up like imports. Also I didn’t want to add in Compass to my mix just yet.
I named my files _section.scss
and they all consisted of the normal CSS for each section at first. After all, the first step is segregation of code. The second step was to use Tracy’s code and change all instances like this:
margin-bottom: 24px; margin-bottom: 1.5rem;
to this:
@include rem(margin-bottom, 24px);
And then
padding: 0 12px 0 40px; padding: 0 0.75rem 0 2.5rem;
to this:
padding: 0 12px 0 40px; padding: 0 cr(12px) 0 cr(40px);
This lets me use Tracy’s cool files and her hard work to make mine way easier! Basically I no longer have to math out that if 16px is 1rem, then 32px is 2rem, and that means 40px is 2.5rem and so on and so forth. It’s faster. Of course, since I’ve done this in pixels, it takes me a teensy bit longer to use inspector to fiddle, since I have to uncheck the rem and edit the px, but I’m okay with that.
I also tossed in two special calls:
@import "settings"; @import "taupecat";
The taupecat has all of Tracy’s code, and settings… well that’s for another post about how I’m controlling my colors and the fonts. But for now, well, all my font sizes and paddings are dynamic. It does mean I have to rebuild my css every time I make a change, but if you ask me, that’s better than math sometimes.
That’s why we have calculators, right?
Comments
2 responses to “Gaining REM”
I love reading your blogs but this went over my head… apart from the REM song references π
I feel like I need to say “Que?!” like Manuel from Faulty Towers π
@Glenn: Font size calcuations π Read http://www.taupecat.com/2013/07/the-62-5-solution/ for more about why a lot of people use 62.5% as a ‘base’ for font-size (it lets you do all the math based on 10px which is easier to double or halve for most people vs 16px). So then 10px is the equivalent of 1rem and so on.
It just has a downside, which I’m defeating with scss so I don’t have to do all the calc of if 16px is 1rem, then what rem is 10px?