Daniel T Ott » Home

Thoughts

“I could while away the hours, conferrin’
with the
flowers, consultin’ with the rain…”

Archive for the ‘Front End’ Category

Today, April 9th, is Naked Day. It’s an annual event in which sites remove all of their style information, to reveal the naked page structure underneath. This is my first year participating, but I think it’s a great idea.

Naked Day promotes the importance of valid, semantic html and a solid page structure in your designs. The bottom line is this: your site should still look good, even if it’s naked.

I know, I know. What this world really needs is a new rounded corners solution. With CSS3 support right around the corner (ha, right), why bother, right?

Well, this solution offers something new to the world of rounded corners. What it does is this: it crops the content below it, so that anything with a background color or image (headers, paragraphs, even images) automatically get the rounded corner treatment with no extra work. See below:

Screenshot of cropping corners

In this screenshot, the image is a normal, rectangular image. Nothing was done to it, except inserting it inside one of my rounded corners containers. It’s easy enough that you can even have your clients do it in their CMSs without the headache of trying to teach them to use the Rounded Rectangle Tool in Photoshop.

This technique solves a very specific issue. There are plenty of ways to render rounded corners that involve less markup, simpler CSS, and less time in photoshop, but none of them (that I am aware of) crop the content below it.

You can view the solution at work here, and I’ll try to take you through how it works.

First, The Images

So this part isn’t too hard, as long as you can navigate your way around photoshop. Just as a note, it’s definitely possible that there are better ways to go about this part in photoshop. If there are, don’t hesitate to drop a line in the comments.

First things first: go and download the Super PNG plug-in for photoshop. What this plug-in does is strips out the color information that photoshop usually saves with PNGs, which will help you avoid that little difference you can see between browsers. See here for a more detailed explanation, but this is important.

Once you’ve got Super PNG running, use the aforementioned Rounded Rectangle Tool to create a rounded rectangle with your desired radius and give it the background color of your container. Edit the layer style to give it a stroke of whatever width you’d like, then rastersize it. Then, decide how much padding you want to be cropped inside your container (I used 2 pixels in the screenshot above). Duplicate your rectangle, and shrink it to fit inside of your desired padding. CMD/CTRL+click on the layer thumbnail to load the selection on the smaller rectangle, then select the larger rectangle and hit delete. You can now throw out the smaller rectangle.

Once this is done, you should have the outline of a container. Now, zoom in to one of the corners, and, using the marque tool, select a square that is just big enough to contain the corner. Copy it, open a new document, and paste it in. Throw out the background, if there is one, and you should have a corner that looks something like this:

Once you’ve got this, simply save it, then rotate it and save it three more times so that you end up with each corner.

Second, The HTML

Now that that annoying photoshop work is done, I’m going to start referring you to an actual example, located here. Here’s the HTML for one box:

<div class="b_greyWhiteStroke">
	<div class="TL"></div>
	<div class="BL"></div>
	<div class="TR"></div>
	<div class="BR"></div>
	<div class="b_innerContainer">
		<h2>This Is A Block</h2>
		<p> Lorem ipsum dolor sit amet...</p>
	</div>
</div>

This is fairly simple. There’s a div for the outer container, a div for the inner container, and a div for each border. All of your content will go inside .b_innerContainer, and the corners of this content will automatically be cropped.

Third, The CSS

The CSS for this solution is a little more complicated, but nothing too bad. The basic idea is that you give the border divs position:absolute, place them in the corners, and let them sit on top of everything else, thus cropping it:

.b_greyWhiteStroke{
	position: relative;
	border-color: #dce3e7;
}

.TL, .BL, .TR, .BR{
	width: 6px;
	height: 6px;
	position: absolute;
	background-repeat: no-repeat;
	background-color: transparent;
	z-index: 9999 !important;
}

.TL{
	top:0;
	left:0;
}
.BL{
	bottom: 0;
	left: 0;
}
.TR{
	top:0;
	right:0;
}
.BR{
	bottom: 0;
	right: 0;
}

.b_greyWhiteStroke .TL{
	background-image: url(images/grey_whitestroke_tl.png);
}
.b_greyWhiteStroke .BL{
	background-image: url(images/grey_whitestroke_bl.png);
}
.b_greyWhiteStroke .TR{
	background-image: url(images/grey_whitestroke_tr.png);
}
.b_greyWhiteStroke .BR{
	background-image: url(images/grey_whitestroke_br.png);
}

.b_innerContainer{
	border-width: 1px;
	border-style: solid;
	padding: 2px 2px 8px;
}

.b_greyWhiteStroke .b_innerContainer{
	border-color: #dce3e7;
}

See, that’s not too bad, is it? A couple notes here. First, I’ve separated .TR from .b_greyWhiteStroke .TR. This is so that I can have multiple background-color schemes going with only minor additions to the CSS. So, if i wanted to create, say, and alert box with a yellow border, I wouldn’t have to rewrite all the existing CSS, I’d just have to make some additions.

What about ie6?

Ie6, as always, was the big headache here. However, by hacking up Angus Turnbull’s IE PNG Fix with a couple of my own modifications, I managed to get it to work. The problem, aside from ie6’s refusal to run transparent PNGs, was that the border block wouldn’t always line up with the edge of the container. I assume it’s some sort of rounding error, but I’m not positive about that. Anyway, the PNG Fix now fixes the transparency, and checks to make sure the borders are where they should be. The only problem here is that this solution doesn’t work in ie6 when you use a fluid width. Or, more correctly, it will correct itself when the page loads, but there is a possibility of the borders moving a pixel out of place if user starts manually changing the width of the window. Check out the example page in ie6 to see what i mean. Here is the code to include this fix:

<!--[if lte IE 6]>
<style type="text/css">
	.TL, .BL, .TR, .BR{ overflow: hidden; behavior: url("ie6fixborders.htc"); }
</style>
<![endif]-->

The End

So, there you have it. You can download the entire example here. This solution, for sure, is not the best for all situations. There are tons of smaller and easier solutions out there, and you should use them unless you have this specific need. However, if you do decide you need something like this, I think you’ll find it to your liking.

As always, I’m more than open for comments and suggestions for improvement. Hopefully you’ll find this solution useful sometime in the future.

Recently, I was writing a Javascript app that involved some animated, expanding boxes. While doing this, I realized I needed a way to dynamically convert pixels to computed ems. The function wasn’t too complicated, and so I decided to make a bookmarklet out of it that did the same thing. It was kind of fun. Hopefully you’ll find it useful, either in javascript, or to remove the headache when you’re trying to create pixel-perfect designs using ems

The Function

So, here are the functions:

//this function returns the computed pixel value
//of 1em relative to the given element
function getComputedEm(el){
	var tdiv = document.createElement("div");
	tdiv.style.height = "1em";
	tdiv.style.position = "absolute";
	tdiv.style.backgroundColor = "#f00";
	el.appendChild(tdiv);
	var emValue = tdiv.offsetHeight;
	el.removeChild(tdiv);
	return emValue;
}
//this function takes an integer, and a javascript element.
function convertPixelsToEms(pixels, el){
	var emval = getComputedEm(el);
	var value = pixels/emval;
	return Math.round(value*100)/100;
}

The Bookmarklet

And here’s the bookmarklet:

pixel2em

Just drag the link above to your menu bar, and click on it in any site. The bookmarklet will appear at the top of the page. It takes a pixel amount, and any css selector you can think of, so it will return the conversion relative to an element. For example, on this site, if you use “body”, 16 pixels = 1.14em, but if you use #content p, 16 pixels = 1.23em.

It’s probably not perfect, so please let me know if things aren’t working correctly. Although, I can tell you right now it’s not going to work on ie6, so don’t bother trying it there.

The bookmarklet makes use of Dean Edward’s cssQuery, a slick little script to retrieve elements based on CSS selectors.

All in all, this was a fun experiment, and I hope you find it useful. Have fun.

Aside from browser bugs, float clearing can be one of the most frustrating aspects of CSS development. It takes a little bit of patience and thought to set up your floats and clears in a way that facilitates both your design and the flexibility required in a modern website.

The Old Way

While judging the last contest of CSS Off, one of the things that I found myself repeating over and over again was this: “Don’t use empty divs to clear your floats!” What I meant by that was that in this day and age, there exist much better, more robust, and more semantically correct ways to properly clear floats that simply inserting <div class="clear"></div>.

The Not Really New Way

Enter the EasyClear method. I’m calling this the “Not Really New Way” because it appeared on positioniseverything.net in 2004, but is still being ignored for the old empty clear div solution.

I’m not going to spend time explaining the specifics of why this solution works; they have gone over the technical aspects of the solution in the article, especially different hacks for different browsers. However, before I explain the best way to use the EasyClear on your sites, there is one part of CSS that needs to be understood, since the entire EasyClear method hinges on it: the :after pseudo-element.

:after - Intrigue and Deception

For a long time, I was confused about the :after pseudo-element. After doing a little testing and research, I figured it out. It’s not all that difficult to understand, actually, but I was mislead in the beginning. The w3schools.com’s definition of :after is the first that pops on on a quick Google search, so it was the first place I went:

The :after pseudo-element inserts some content after an element.

It turns out that this is completely incorrect. The actual definition (via The W3) is this (emphasis added):

The ‘:before’ and ‘:after’ pseudo-elements can be used to insert generated content before or after an element’s content.

The difference between the two definitions is considerable. Content inserted using the :after pseudo-element is inserted not after the element, but instead within the element and after the content. See an example here.

You can use :after to insert just about any content you’d like, and give it most CSS attributes, including display and clear. This is how EasyClear works its magic.

Using EasyClear

Using EasyClear is simple. The only trick is to apply it to the correct element. The element that you are going to apply it to most is the element containing the floated elements. For example, a page with a container that has a background you’d like to extend to include the entire content, and two floated elements:

Example page

In this example, you want to apply the EasyClear to #container, since that is the containing element. It will insert a block-level element with the clear attribute set to both, thus clearing the floats inside the element without crufting up your HTML. You can see an example at work here, and here’s the EasyClear CSS itself:


/* EasyClearing http://www.positioniseverything.net/easyclearing.html */
#container:after
{
	content: ".";
	display: block;
	height: 0;
	clear: both;
	visibility: hidden;
}

#container
{display: inline-block;}

/* Hides from IE-mac \*/
* html #container
{height: 1%;}

#container
{display: block;}
/* End hide from IE-mac */

As you can see, it’s pretty simple to employ, and the results are perfect. The best way that I’ve found to implement this on larger projects is to keep the EasyClear in one place, at the bottom of your main stylesheet, and simply add elements that you need to clear to the declaration itself, as illustrated here.

Clears For All

So now you never have to insert an empty div into your otherwise perfectly crafted HTML. Go read the article, learn the background, spread the word, and, please, just start using it!

For years, I’ve always formatted my CSS in what I like to think of as the “traditional” manner. Each selector on one line, and each property indented one tab underneath:


#content{
	width:49em;
	padding: 0 1.5em;
	margin: 0 auto;
	clear: both;
}
#content h2{
	font-size: 2em;
	height: 1.5em;
	margin-left: .75em;
	color: #000;
}
#content h2 a{
	font-weight: normal;
	float: left;
}
#content h3{
	font-size: 1.25em;
	line-height: 1.2em;
	color:#4d9fdd;
}

Pretty standard stuff. I’d break up sections with big comments, and that made it fairly easy to scan. While this format never caused and problems per se, and it was easy enough to implement, it never seemed completely ideal.

When I started working with Sprokets, the other front-end developer there was using one-line css declarations. This threw me for a loop for a while, but I slowly started liking it better and better.

The method I’ve finally settled on is based on the one-line approach, but with an extra twist: I indent the declarations based on it’s place in the HTML document. For example, take the HTML for this simple page:

<body>
	<div id="one">
		<p>text</p>
		<p>text</p>
	</div>
	<div id="two">
		<p>text</p>
		<ul>
			<li>list item</li>
		</ul>
	</div>
</body>

The CSS to access this code would be like this:


body{ properties; }

	#one{ properties; }
		#one p{ properties; }

	#two{ properties; }
		#two p{ properties; }

		#two ul{ properties; }
			#two ul li{ properties; } 

This has worked very well for me so far, and I’m looking forward to trying out on some bigger projects. It allows me to scan through my CSS much more quickly, view comments more easily, and has the added benefit of helping me think through things like descendants and inheritance. Take a look at the CSS for this site to see it in action on a bigger scale.

This method, while new for me, is clearly not revolutionary. However, since I stumbled on it on my own, I thought some of you guys might benefit from it as I have. How do you format your CSS?

Well, I made it almost a month since the last design change. However, this redesign is a little more extensive.

The Little Stuff

First, I went to an all-Helvetica font scheme. My typography was all over the place on the last view versions, and I feel much more comfortable with choosing one font and sticking to it. Second, but related, I moved the title and navigation to their own bar under the opening graphic, and moved the quotes under the section heads to the right of the section heads.

The Grid

The next thing I needed to change was the vertical spacing. I chose 1.5 ems as my baseline height, and went with it. Everything now lies on a 1.5 em baseline grid. You can imagine (or maybe you know from experience) that this seems like it could be a pain. It is. What was my secret weapon? Rob Goodlotte’s Syncotype. What it does is place a horizontal grid over the page you’re viewing, at any size you’d like. 1em converts to 16px, so I was able to line everything up to a 24px horizontal grid. It’s awesome.

The Em

The biggest change, which might not be all that visible at first, is that the entire site (except for a couple borders) is now in ems. The entire structure, the images, and of course the fonts, now resize with the text. The effect is that when you increase font size in your browser, the entire web site zooms. Try it out (cmd/apple + to increase size, cmd/apple - to lower size).

Seems like an easy concept; just divide all of your pixel values by 16, like I mentioned before, and you’re set, right? Well, at first, yes. For the overall structure, that’s all I had to do. The problems arise when you change the font size of an element. Whenever you change the font size of an element, like a paragraph or a heading, the rest of the attributes of that element are measured against the new font size, instead of the body font size. To get the margin or padding that you want, you have to divide the margin you’d like by the new font size, and that number is your new margin. Ideal margin / new font size = New margin.

I had my calculator program running the entire time.

Inspiration and guidance for this redesign came from Wilson Miner’s Setting Type On The Web (on A List Apart) and Richard Rutter’s Compose to a Vertical Rhythm (on 24Ways).

It’s been a fun (and trying) experiment. Hopefully this version will last longer than a month before I’m fed up with it!