Tuesday

Wrangling 3rd Party Images

Sometimes our images are coming from a 3rd party (sometimes further away) content source, and so can be a challenge to render on our page in a common way among differently sized images coming from elsewhere. Let's say we want to normalize all our images to a 100px square, regardless of the source size. Using CSS, we could just resize all images into a square like this:

img {
    height: 100px;
    width: 100px;
}
But this doesn't really work unless all our source images are also square, otherwise you'll be forcing your images into a non-native aspect ratio, and you'll be left with images that look like they are from an old spaghetti western movie (squished into being super tall and skinny). We don't want this, but we do want to keep our images as consistent as we can. So, instead of doing this, we want to just force our images to only ONE specific dimension, and leave the other to scale normally. Something like this:
.thumbnail {
    overflow: hidden;
    width: 100px;
    height: 100px;
}

.thumbnail img {
    width: 100px;
    height: auto;
    max-height: 100px;
    -ms-interpolation-mode: bicubic;
}
Notice that I've added a "thumbnail" container around our images for clipping purposes. Our thumbnail will be the actual dimensions we want to show to the user, in this case a 100px square, and set the overflow to hidden so any image that happens to be bigger than this will not be shown, resulting in what seems to the user to be perfect 100px square images.

Our img styling now is set to a static width of 100px, and I'm going to set height to auto, which tells the browser to scale the height to whatever is appropriate based on the static width we defined and the aspect of the original. In addition, I've added here extra instruction for old IE browsers to scale images in a very specific way - in fact, I add this to my reset stylesheet when I'm building any site. Here's some more info on this from CSS-Tricks.

Demo

Full demo with code

(Note I added a yellow outline, and a white background on each thumbnail to show where the 100px square clip is.)

You can see a few different cases here, all of which should show images that are sized down AND cropped to our defined box, while retaining their original aspect ratio; if the source is a rectangle, and if it's wider than it is tall, there's a chance it will not fill our full 100px of height. I think this is acceptable, and I've found it to be rare enough that it doesn't seem to be an issue.

The other thing you may want to consider is adding a default "no image found" background image onto .thumbnail for when a broken img occurs - this way, your users will still see something semi-nice and consistent with the other images on the page (some browsers show a broken image icon, others do not).

Placeholder images here are from lorempixel.com

Note: If you'd like to vertically position your images (e.g. in the 2nd case here, you could middle align your image instead of the default top alignment), you can do so by setting "vertical-align: middle" on your img, and "line-height: 100px" to .thumbnail.

Monday

CSS generated content issue on Chrome/Safari

Recently I needed to use CSS content to help with some display stuff. Skipping through the typical debate about separation of concerns, I had to use this method because in this case I don't have access to the actual markup of this part of the page. This really does happen to web developers on occasion, typically due to 3rd party generated content, which was the case here.

Basically, all I needed to do was add a text "header" before my content. So, I added something like the following;

p:before {
    content: "Header Text";
    display: block;
}
Note I wanted to add "display: block" here to introduce a line break between my new CSS generated content and the pre-existing text of my paragraph. No big deal, because I know that this is a supported feature with just about every browser sans the usual IE6/7, which I'm comfortable with leaving alone without the added content: http://www.caniuse.com/#feat=css-gencontent.

My particular case of 3rd party content was a bit more troublesome than usual, because it's made up of a bunch of unclassed paragraph tags, with bold tags representing what the 3rd party thought would be like a sub-header for each paragraph. The paragraph I need to edit was the 5th in this structure. This block of HTML looked something like this:

Sub header 1 A bunch of content

Sub header 2 A bunch of content

Sub header 3 A bunch of content

Sub header 4 A bunch of content

Sub header 5 A bunch of content

Sub header 6 A bunch of content

My thinking was that I wanted my generated content to be within my 5th paragraph, but before the bold tagged "sub-header"; as such, I change my selection to something a bit more complicated. So, I updated my CSS selector:

.some-content p:nth-of-type(5) b:before {
    content: "Header Text";
    display: block;
}
And it works as expected in recent versions of Firefox (though CSS3 selection isn't supported in IE8, so there's an issue there now), but yay! But this did not work as expected in either Chrome or Safari. What the heck?

Resolution

As it turns out (for reasons currently unknown to me), both Chrome and Safari didn't like adding the :before pseudo-class onto the b tag. Changing the selector to remove the bold, things worked:
.some-content p:nth-of-type(5):before {
    content: "Header Text";
    display: block;
}
I've always tried to avoid this type of combined selector with 2 pseudos, in this case "p:nth-of-type(5):before" would seem to elicit some buggy behavior. Only I've been shown I was a bit wrong, at least in this case. The above is working for Chrome 18, Safari 5, Firefox 11, IE 9 (sort of buggy re: positioning). IE8 and older are sort of out of the picture here now due to my fancy selector; I'm OK with this.

Thursday

The difference between the CSS selectors a:hover.className and a.className:hover

a.foo:hover and a:hover.foo are treated identically in modern browsers. I've tested back to IE6, and it works as expected. That being said, my personal preference is to define the selector first and then apply the psuedo-class; e.g. a.foo:hover. I just feel it reads more intuitively.

This was a question asked on Quora that I thought would be a nice additional reading here...One of the "huh" moments for me, so I figured I'd re-share. :-)

Wednesday

Nested List Styling

Oddly enough, I had a hard time finding a good set of default list styles that would work in a myriad of cases. For example, what if I had a nested ordered list, or an ordered list under an unordered list, or vice-versa? Well, here we go. I'm sure there's some weird cases I've missed, but this gets us a nice amount of flexibility and predictable behavior.

The entirety of my css for my lists is below. Pretty concise, and covers mostly every case I've needed.

ul {
    list-style: none;
}

ul.bullets,
ul.bullets ul {
    list-style: disc inside;
}

ul, ol, dl {
    margin-left: 2.5em;
}

ul ul {
    margin-bottom: 0;
}

ol {
    list-style: decimal outside;
}

ol ol {
    list-style: lower-alpha outside;
}

ol ol ol {
    list-style: lower-roman outside;
}
​

Results

See the Demo for the full code including HTML markup, but our end result will look like so: