Skip to main content

Posts

Element.focus() in IE9 running Jasmine tests with Karma

I recently udpated our Jasmine unit tests to run in Karma ; expanding our browser coverage, adding code coverage reports , and using fixtures for testing DOM manipulation . One of my tests kept failing in IE9, but only when I ran from the console. If I attempted to debug in the browser, everything passed. It turns out that IE9 (at least) needed a few ms to catch it's breath before correctly focusing on the starting element. To do this, I just added a 100ms delay before each test ran (Using Jasmine 2.3). beforeEach(function(done){ loadFixtures('myfixture.html'); // Setting focus in IE requires a delay to work correctly! setTimeout(function(){ done(); }, 100); });

Be Careful Using SASS @extend, CSS3 Selectors, and IE8

I recently ran into a situation where some of my styles were broken in Internet Explorer 8. What makes this different from all the other times my styles broke in IE8 was that, as far as I could tell, it was correct. Consider the following code: %highlighted { background-color: #ff0; color: #333; } .keyword.is-active { @extend %highlighted; padding: 10px; text-transform: uppercase; } No problem, right? Then why isn't it working? I wasn't sure, until I looked at the compiled CSS and saw something like this: .some-list:nth-child(2n), .is-highlighted, .keyword.is-active { background-color: #ff0; color: #333; } .keyword.is-active { padding: 10px; text-transform: uppercase; } Since IE8 doesn't support the css selector nth-child , it ignores the entire rule . Without looking at the final output, there's no way of knowing if this will happen ahead of time. So what can you do? Well,...

CSS line-height Units Explained

In Chris Coyier's recent article, " Fun with line-height! ", he mentions that we often use unitless values for line-height so that it's proportional to the font size. This is only part of the story, and I was reminded of a Stack Overflow question that got me investigating more about how line-height worked with regards to the various CSS units. If you're declaring the line-height on each element, you won't notice any difference. But if you're not crazy and using the first C of CSS (that's 'Cascading', by the way), then the inherited line-height might not work the way you expect. Disclaimer: This is my understanding after doing some research and testing. I may not be completely correct. TL;DR When using a relative unit (em, %), the line-height is calculated based on the font size of the element where the line-height is defined. This line-height is then inherited, unchanged, by each of its descendants. So we end up with a line...

SASS Converts Zero Opacity 'rgba' To 'transparent'

I recently had a strange problem after updating SASS. I have a modal dialog with a semi-transparent overlay behind it. For modern browsers, I'm using a CSS transition from rgba(0,0,0,0) to rgba(0,0,0,.5) For older browsers (namely IE8), I'm using some JavaScript to apply the transparency and animate the transition. To make sure the background gets set correctly, I'm using the standard fallback strategy of defining the background as background: rgb(0,0,0); right before the rgba line. This worked fine, until SASS optimized my code by changing background: rgba(0,0,0,0); to background: transparent; The reason? It's 2 characters shorter. Yes, we've now saved 2 bytes of easily compressible text in exchange for breaking my code. Why did it break? Well, if you haven't already figured it out, normally a browser that doesn't support rgba will simply skip that property and move on. But transparent is supported. So now, instead of h...

Naming Booleans

Naming conventions are one of those little things that can become a big thing when you multiply the scale of the project and/or people involved. I was just looking through some code yesterday, and I saw a variable with a fairly typical boolean name that followed the format isObjectState This felt awkward to me, and I took a moment to consider why this is. In Logic, a boolean is a statement that is either true or false . However, this variable name is written as a yes or no question. It is an easy misconception to equate true with yes and false with no , but though they are similar, they are not exactly the same, and, especially as programmers, we should not treat them as such. When we expand our variables into full sentences, the awkwardness becomes more apparent. Especially when we insert them into control structures. Let’s rename our variable to: is this menu item active? So our control blocks will read: if is this menu item active?, then highlight it. while is this men...

Automatically Calculating Foreground Color using SASS

I ran into the problem, as I'm sure many others have, where I wanted to dynamically assign background colors to my components, but also needed to adjust the foreground color to be readable and visually appealing. It's easy for something to fall through the cracks when changing colors, and then we end up with unreadable text. So to try and avoid this, I put together a SASS function that selects a color for text based on the background color you give it. This also performs checks to make sure the contrast meets a given Accessibility spec (as per W3C rules). So far I've had pretty good results with it. Please feel free to tinker and use this in your own projects. It's far from perfect, but it has See the Pen Calculate Foreground Color by Thomas Pietrosanti ( @Rykus0 ) on CodePen .

SASS Mixin for Creating CSS Triangles

I feel like one of the most common shapes that we can reliably create with CSS vs images is the triangle. One of the reasons for this is that it works as far back as IE7 (IE6 works, but doesn't support background-color: transparent ). Another is how frequently a little triangle icon can be used (e.g., ascending/descending buttons, expanding menus, open/close state, etc.) I'm not going to cover the techniques of creating triangles with CSS . That's been done already , and probably better than I would do it. What I'm going to share with you, is a little SASS mixin that I've been using to generate triangles. See the Pen SASS Triangles by Thomas Pietrosanti ( @Rykus0 ) on CodePen . I'm using a SASS placeholder ( %triangle-base ) as a base to contain the common styles. This makes it easy to extend without generating tons of extra code each time I want to make a triangle. When this compiles, it creates a rule that applies to all of th...

When Table Cells are Smaller than the Table

I ran into an issue at work the other day that was a bit strange. A portion of one of our forms is using tables for layout (Yeah, I know... it's older code), in all other browsers the fields lined up with the others on the page, but in IE9 there was some unexpected extra space throwing the layout all off. I took a look, and noticed a few things: The table had a fixed width of 690px There were only two columns in the entire table, but the second cell had colspan='2' Both table cells had a fixed width The sum of the table cells widths (including padding and all) was less than the width of the entire table So what does all of this mean? Well, the main issue is that, since the total width of the cells is less than that of the table, the browser has to figure out what to do with the leftover space. Let's see what happens (I'm leaving off the colspan and adding some extra elements for demonstration purposes). cell 1 ...

Dotted/Dashed borders fail on rounded corners in Firefox

I just learned today that a dotted or dashed border will render as solid for the rounded portions (specified by border-radius ) in Firefox. There is an open bug for this, but I haven't run across it until just now. So if you're wondering why you can't put a dotted border on that circle in firefox - that's why. Which looks like this in Firefox (screenshot from v21)

Flags vs Vendor Prefixes for Experimental CSS

Google recently announced that it will be switching Chrome's rendering engine to a fork of Webkit, called “Blink”. If you're interested, you can read their blog post about Blink . One of the changes we will be seeing in Blink, is the use of browser flags in place of vendor prefixes . What this means, is that rather than prefixing experimental CSS with -webkit-, -moz-, -ms-, -o-, -khtml-, etc.; We will be required to enable these attributes using a flag in the browser (like the ones currently at chrome://flags). If you ask me, both ways have their advantages and disadvantages. Though, in the end, I lean towards the flags over the vendor prefixes, and here's why: #1 Code Bloat On average, using CSS with vendor prefixes requires writing it 5 times . Every time you use it . #2 Future Friendly Eventually, prefixed code will be replaced by the unprefixed code. #3 Browser Dev Friendly Browsers tend to continue to support the prefixed code, even if it doesn...

Responsive Images On-The-Fly with PHP and ImageMagick

I've seen some responsive image solutions proposed, and they seem great. But there's still some debate and it will be a while before support is widespread. In the meantime, I've expanded on a method I've been using for re-sizing images on the fly, which I believe can be further expanded on to make it even better. Basically, I use a PHP script and ImageMagick to generate a re-sized and cropped image, which I also cache for later use. Here's the essentials of the script I'm using: Using this script, I can request re-sized images via JavaScript, or even CSS (presuming the image is in a background) based on the current screen resolution and pixel density. This may not be the most elegant solution, but it's worked fairly well for me. For the purpose of this example, assume this script resides at domain.com/tools/ Note: This is an untested adaptation of a working script. It may not work as-is. $file = urldecode($_GET['f']); $widt...

Get width and height of a remote image with VB .NET

The Problem I wanted to grab the width and height of an image that was on a remote server. I've done this with PHP, but never in .NET. The Solution I did a little digging and came across this code (slightly modified for my own purposes). Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest) request.Method = "GET" request.Accept = "image/*" Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) Dim s As Stream = response.GetResponseStream() Dim bmp As New Bitmap(s) Width = bmp.Width Height = bmp.Height

Standalone Web Apps on iOS

As more and more mobile OSes are released, that means apps have to developed for more and more platforms; multiplying the workload and the cost to both create and maintain any mobile app that you want to be available to anyone. Luckily, the web offers us a more standardized platform. (And before you cite differences in browsers: browsers are free and much easier to change than hardware, and there may be some minor discrepancies but the base technology used for development is still the same across the board). While there are obvious drawbacks (available data connection, bandwidth, integration with device-level features), many of those do not have that great of an impact for a lot of applications or can be circumvented with HTML5 APIs. One hurdle that HTML5 doesn't solve (at least, not that I've seen) is to run a web app in a way that the user experience is the same as if they were running a native app; meaning the removal of the browser chrome (address bar, browser menu, ...

JavaScript Self-Executing Anonymous Functions Simplified

I've seen code like this everywhere, and maybe you have too. A function (hopefully wrapped in parenthesis) followed by some variables in parenthesis. (function(a, $){ ... code ... })(window, jQuery); I eventually started trying to research what they were, how they worked, and why they should be used. Most of the explanations got thick quick, so I thought I'd simplify things. In essence, this is declaring and calling a function all at the same time. The easiest way to think of it (for me, anyway) is that this: (function(a, $){ ... code ... })(window, jQuery); ...is roughly equivalent to this: var myFunction = function(window, $){ ... code ... } myFunction(window, jQuery); There are some (pretty big) differences (such as when and how it runs), but this is enough to get you started. The parenthesis wrapping the function aren't required, but are considered best practice and common courtesy because they help to identify t...

Fanning Cards with CSS

I was thinking of fanning out some cards to demonstrate options for one of our sites, and I wanted to do it in CSS, so I came up with this (Plan for fall-back in non-supporting browsers is just a button that shows the options). The plan is to show all options in full when the stack is clicked on, but right now it just fans out on hover. This isn't anything terribly ground-breaking or difficult, but it is a nice, fun effect so I thought I'd share. Edit: I updated the link and code here to point to an updated version of the code that uses the css3 :target selector to spread out the cards when a link is clicked, and collapse them when it's closed. (See the full page example for better effect) See the Pen Fanning Cards by Thomas Pietrosanti ( @Rykus0 ) on CodePen . I used SASS to do handle some of the incremental and procedural parts, but did not leverage Compass, since I am not using it. Really, all I'm doing is absolutely positioning div s on top of ea...

Imitate :nth-child(x) in IE 8

I ran into the situation today where I wanted to use :nth-child(x), but this won't work in IE8. Since the majority of our user base is still using IE8, I had to address this issue. If you don't want to use a separate class for each one, there is a solution. first-child nth-child(2) nth-child(3) ​ li a{color:#fff;} li:first-child a{ background:#333; } /* :first-child is supported */ li:first-child + li a{background:#666; } /* :nth-child(2) */ li:first-child + li + li a{background:#999; } /* :nth-child(3) */​ See for yourself: This obviously is not a solution for cases like :nth-child(odd) but it's a nice way to handle cases where you want to control an element based on a specific sibling distance from the first child, such as centering the text of the second column in a table.

Animating Pseudo Elements in CSS

I just ran into a case where I wanted to animate a:after using CSS transitions, and found out that it is only supported in Firefox 4+. So I turned to modernizr for a test case, but didn't see one. Just as I'm debating whether or not I'm good enough to give it a go, I find that a bug has already been logged for it, and a pretty good solution is already in the works . Even though it's not official, it's better than what I had, so I figured I'd throw it in. Kudos to the Modernizr team and all who have contributed!

The importance of Kerning

I'm not that great of a graphic designer, and I'm even less skilled when it comes to typography. But I know the basics, and this is a great (and hilarious) example of what kerning is (in short: the distance between letters) and why it's important (in this case, "click" looks like "dick"). I can't take credit for this image. It was a retweet of a retweet originally posted by @jbrownridge .

Use parseInt Correctly

Something else I just learned today. I've been happily using parseInt all this time, and I've been doing it wrong . I never really thought much about it. "Ok, let's change this string to an integer. parseInt(string) Bang. Done." But then I got a warning when compiling my code with Google's Closure Compiler saying that I needed two parameters. Wait, what? Turns out, there is a second parameter for the Radix that you should always fill in . This makes sense, of course, but apparently I assumed everyone used base ten. Always. That was pretty dumb, and here's why: If the input string begins with "0x" or "0X", radix is 16 (hexadecimal). If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt. If the input string begins with any ot...

Copying an Object in JavaScript

The Setup var default = { param1: "one", param2: "two", param3: [] }, working = default; // Fill form fields from working data // User modifies form fields // Save form fields in working data // Perform calculation based on working data // User clicks a button to reset the form to default state function reset(){ working = default; // Fill form fields from working data // Perform calculation based on working data } The Problem The form was not being reset to the default data, but instead did not change at all. When I debugged, I noticed that the data in default was identical to the data in working , even though I never assign any values to default after initialization. The Cause Little did I know, that assigning an object in JavaScript actually only assigns the reference, and does not create a new copy of the object. The Solution The object needs to be copied, rather than simply assigned. Based on a quick Google search, there is no bui...