Saturday, December 27, 2008

Image Cropping with CSS

Part 1


Here is a quick, easy way to create a gallery by making all of your images squares. For simplicity's sake, in this example, all the images are 160x240px, both landscape and portrait. We'll make our squares 150px. In the HTML, wrap each image in a div, and give the div the following values:
div {
  width: 150px;
  height: 150px;
  overflow: hidden;
  float: left;
  margin: 0 10px 10px 0;
}

This sets up our square, and hides any part of the image that goes outside of that sqaure. Additionally, it adds a float and puts some space between each image.

Part 2


This gives us a good start to the gallery. My only compliant is that the images aren't centered within the div, we are only seeing the top left corner. So I wrote some jQuery that does this.
$(document).ready(function(){
  function centerImage(ele, w, h, pw, ph) {

      var heightdiff = -((h - ph) / 2);
      var widthdiff = -((w - pw) / 2);

      $(ele).css("margin-left", widthdiff);
      $(ele).css("margin-top", heightdiff);
  }

  $("div > img").load(function () {
      centerImage($(this), $(this).width(), $(this).height(), $(this).parent().width(), $(this).parent().height());
  });
});

See all this in action here.

From here, you can add more styles, link the images to a larger version, or anything else you can think of.

Tuesday, December 23, 2008

Inline List Items

When putting together a list with the li set to display: inline, the list will display some extra space in between each item. This space can be quite annoying. To fix it, put a float on the list items.

Saturday, December 20, 2008

Letting Things Go

This post is going to be more of a rant than anything...

For the last 2.5 years, my husband was a youth pastor for a church in Indiana. Being the pastor's wife, I took on a few responsibilities, one of which being the church's websites. I took the time to design, implement, and maintain both the church's website and youth group website for free.

My husband was let go last month, so I quit updating the websites. I'm the type that hates to leave things undone, so I found a couple people that were willing to take over the updates, and spent some time teaching them how to do it.

I hopped on one of the sites today to grab some information, and found the site had been completely redone, but not by either person I had spent time with. Apparently someone else had been brought in to redo the whole website. My biggest complaint - it is now all in Flash (I have several other complaints as well, but I'm not going into those).

This is a static, informational site. There is no reason for it to use Flash. Flash should only be used for splash pages, videos, and games. They don't even have any special animation that would require Flash. Plus, a lot of nifty "animations" can be now be easily done with jQuery.

So this now brings me to my point. At times in your web designing life, you are going to quit working on projects and hand them off to the next person. At that point, it is no longer in your control, so whatever happens from then on is not your responsibility. But at the point of the hand-off: make sure to keep a copy of the code for your portfolio. I did this, and am glad I did since I can no longer refer to these websites. I'm thinking about rebuilding a couple pages to put up as samples on my personal website.

</rant>

Tuesday, December 9, 2008

How CSS Works

I have encountered a couple situations recently in working with clients that are writing their own CSS. By looking at their code, I can see that they don't completely understand how CSS works, because they are writing too much code. So I decided a blog post on this topic was necessary.

The beauty of Cascading Style Sheets is their ability to cascade. Basically, when you have several style sheets, the last one to be read into the page is going to have the most importance, but all files will still be read. Certain parts of the less important files are just overridden.

So let's say in one CSS file you have the following attributes set for paragraph text:
p {
color: #000000;
font-size: 12px;
line-height: 16px;
}

This file is called into your document by putting this code in the <head>:
<style type="text/css" media="all">@import url("first.css");</style>

Now you have a second file you want to import, and it has these attributes for your paragraphs:
p {
color: #4c4c4c
margin: 0;
padding: 0 0 15px 0;
font-weight: normal;
}

And this second file is now added to your list of imported stylesheets:
<style type="text/css" media="all">@import url("first.css");</style>
<style type="text/css" media="all">@import url("second.css");</style>

All attributes will be read in from first.css, then second.css. Color is listed in both, so since second.css is listed last, it's going to take the #4c4c4c value, overriding #000000. All other values listed in first.css will still apply to the paragraphs. There is no need to duplicate the attributes listed in first.css into second.css.

More


To go into this a little bit further -- let's add a class called "bodyText" to that paragraph. Since it's still a paragraph, all the CSS listed above will apply. But now you can add more CSS that is specific to your bodyText:
.bodyText {
font-weight: bold;
border: 1px solid #123456;
}

Once again, the styles that have already been declared for paragraphs do not need to be duplicated.

Why?


Now why would you ever use multiple stylesheets? I'll give a simple example. Let's say you have a fairly large website, with three or four separate sections. You have one CSS file that will apply to the entire site. This will set things like page width and font. But now you want each section to have its own set of colors. This is where you can write a specific CSS file for each section. Each section will import the site-wide CSS, then that section's css.

A Matter of !Importance


Final note, I promise. If you were to go back to first.css and add !important next to the color (color: #000000 !important;), it is going to take precedence (unless, of course, second.css also had !important next to its color). I try to use this sparingly, especially since IE6 tends to ignore it.

Wednesday, November 5, 2008

E-mail Obfuscation with jQuery

I recently was given the task to obfuscate e-mail addresses. I looked at a few options, and decided I wanted a method that followed some certain criteria:
   1. easy to implement
   2. effective
   3. linked
   4. selectable text

I found a blog where this guy put nine different obfuscation methods on one web page, and let it sit for a year and a half. This graph shows the effectiveness of each method. Using all of this data, I decided Javascript was the way I wanted to go. For extra simplicity, I went with jQuery

<a class="obfuscate">example<span class="replaceAt">-AT-</span>gmail.com</a>

<script type="text/javascript" charset="utf-8">
  $(".replaceAt").replaceWith("@");
  $(".obfuscate").attr("href", "mailto:"+$('.obfuscate').text());
</script>

The first line replaces the whole .replaceAt span (including tags) with an @, then the second line takes the constructed e-mail and sticks it into the href of the <a>. Those without Javascript installed will see example-AT-gmail.com.

Wednesday, October 29, 2008

Dynamic Text in a Limited Amount of Space

A common problem I've seen when working with dynamic text is you sometimes don't know how much text the end user is going to enter, and this can be a problem when you have a limited amount of space. Generally, you set the container to overflow:hidden and be done with it. The problem with this is that sometimes your text gets cut off in the middle of the line so only the tops of the letters can be seen. It's especially a pain when you are never 100% sure that your users are going to have the correct font.

One solution is to limit the amount of characters, but this isn't always very consistent with the varying sizes of words - you can end up with more or less space available than desired. I decided I wanted to limit the amount of lines, and was able to achieve this with just CSS:

.box {
width: 50px;
height: 50px;
}

.box span {
display: block;
font-size: 10px;
line-height: 14px;
height: 42px;
overflow: hidden;
}

<div class="box">
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.</span>
</div>

The line-height here is important, and then setting the height to 42px limits our text to 3 lines (14 x 3 = 42). A "read more" link can then be added after the span, if desired.

Thursday, October 16, 2008

Quick Border Fix

Ever have a problem with borders mysteriously disappearing/reappearing from some of your items in IE? Give the item "position: relative;"

Monday, September 29, 2008

Cellspacing and Cellpadding With CSS

In HTML, a <table> can have a couple attributes added to it: cellspacing and cellpadding, and these attributes are valid code. Unfortunately, these have default values so that writing
<table>
is basically the same as
<table cellpadding="1" cellspacing="2">

This becomes a problem when you do not want this additional space and cannot change the HTML, as cellpadding and cellspacing are not CSS attributes. There is still a way to get around this in CSS with the following code:
table {
border-collapse: collapse;
}

table td {
padding: 0
}

Thursday, September 11, 2008

Always Learning

It seems funny to me, that because of the way I learned CSS there are still lots of things I don't know.

For instance, a couple weeks ago I was in a Barnes & Noble and picked up a CSS book. After flipping to random page, I found a CSS property I didn't know existed: outline. When used in this way:
a { outline: none; }
the outline you see after clicking on a link no longer displays.

This is particularly helpful in Firefox, because for absolutely positioned links, the outline often stretches to the far left side of the page, which drove our QA person nuts. Previously, I was fixing this behavior by using overflow:hidden, but this doesn't always work depending on how your links are set up. I personally don't like the outline, so I plan on setting it to none on all of my sites now.

Friday, September 5, 2008

IE6 Disappearing Images Solution

After a bit of testing on my issue with the sprites disappearing on hover, I came up with a temporary solution. I still wasn't satisfied with it, and did some more searching on Google. That lead me to a nice script that fixes the problem. Here are the two solutions:

Temporary Fix:
Take the images being used for each link, and set them as the background of the ul or table you are using for the navigation. I made a quick attempt to set the image as the background of the li that contains the anchor, but that still flashed. It seemed to work fine putting it in the background of the ul. This solution does not fix the disappearing images, just makes it less noticeable.

Script Fix:
Doing a Google search led me to this script:
<script type="text/javascript">
document.execCommand("BackgroundImageCache",false,true);
</script>
and that seemed to fix the problem just fine. I have more testing to do, but hopefully this will be my final solution.

Wednesday, September 3, 2008

IE6 and Dropdown Navs

So I have run into this certain issue lately involving dropdown navigation menus and IE6. It doesn't happen all the time, but with certain sites I have noticed the background on menu items disappearing when you hover over them to display the dropdown. It doesn't always only cause the hovered item's background to disappear, but sometimes also the background of the items continuing to the right of the hovered item.

I am pretty sure this is being caused by the Javascript that was written for the dropdowns to work, specifically for IE6. The dropdowns do appear in other browsers without the use of the Javascript. The JS in question simply changes the name of the class applied to your hovered item. I tried to use JQuery instead, and got the same result.

Upon further inspection today, I found that this is only happening when your main nav items are using background images, instead of pure CSS colors. The images I am using are even sprites, so there is no apparent reason for their disappearance. One site I am seeing this behavior on is our own, so that can easily be changed to not use images. The other, unfortunately, is a client site. The designer has designed the site in such a way that images are necessary, and as we recently discovered, the client is using IE6.

So much for ignoring the obsolete.