Tag: CSS

Scriptaculous Autocomplete Page Jump Using Arrow Keys

When you use overflow:auto in your css in conjunction with Scriptaculous’ Autocomplete, there is a bug that makes the entire page jump around when you use the arrow keys on your keyboard to navigate up and down through the suggestion list. This bug normally appears only when the page itself is long enough to require a scroll bar.

I managed to come up with a very clean working solution by hacking the controls.js file that comes with scriptaculous. The solutions requires replacing the markPrevious and markNext functions and adding a small line of code to the updateChoices function.

Currently, markPrevious and markNext are telling the page to jump around like that, and I’m not sure why! As far as I can tell (please let me know otherwise) this solution could be included in the scriptaculous without breaking a thing (hint, hint to the scriptaculous team).

To implement the solution, replace:

markPrevious: function() {
 if(this.index > 0) this.index--;
  else this.index = this.entryCount-1;
   this.getEntry(this.index).scrollIntoView(true);
},
 
markNext: function() {
 if(this.index < this.entryCount-1) this.index++;
  else this.index = 0;
  this.getEntry(this.index).scrollIntoView(false);
},

With:

markPrevious: function() {
 if(this.index > 0) {this.index--;}
 else {
  this.index = this.entryCount-1;
  this.update.scrollTop = this.update.scrollHeight;
 }
 selection = this.getEntry(this.index);
 selection_top = selection.offsetTop;
 if(selection_top < this.update.scrollTop){
  this.update.scrollTop = this.update.scrollTop-selection.offsetHeight;
 }
},
 
markNext: function() {
 if(this.index < this.entryCount-1) {this.index++;}
 else {
  this.index = 0;
  this.update.scrollTop = 0;
 }
 selection = this.getEntry(this.index);
 selection_bottom = selection.offsetTop+selection.offsetHeight;
 if(selection_bottom > this.update.scrollTop+this.update.offsetHeight){
  this.update.scrollTop = this.update.scrollTop+selection.offsetHeight;
 }
},

Now find the updateChoices function and just after the code this.stopIndicator(); add this.update.scrollTop = 0; so that it looks like this:

this.stopIndicator();
this.update.scrollTop = 0;
this.index = 0;

I’ve tested in FF 2.0.0.4, FF 3.0.5, Chrome 1.0.154.43, Safari 3.2.1, Opera 9.5.1, IE 7.0.5730.13 and IE 6.0.2600 without problems.

Bookmark and Share

31 Responses  |  add yours »

IE6 Empty Div spacing issue

It has become fairly common practice to use CSS to fill the contents of an empty DIV tag. For example, I’m working on a project now that uses the following DIV:

<div id="main_nav_top_shadow"></div>

And with CSS I’m giving that div a fixed height and a repeating background image that creates the appearance of a shadow being cast upward on top of my main nav.

#main_nav_top_shadow {
height: 6px;
background-image: url(../images/main_nav_top_shadow_x.gif);
background-repeat: repeat-x;
}

The problem is that IE6 thinks that the empty space consists of text. Empty text, but text nonetheless. So what happens is that whatever the default font size for that particular DIV becomes the height of the DIV, rather than my specified height: 6px; in the CSS. There are 2 fixes for this problem, one of which I’ve been using forever and another I just stumbled upon and which prompted me posting both solutions. Both are fairly easy.

Solution #1
This solution is the one I’ve been using forever. Basically, you just choose a font size that’s smaller than the height you are setting.

#main_nav_top_shadow {
height: 6px;
font-size: 1px;
background-image: url(../images/main_nav_top_shadow_x.gif);
background-repeat: repeat-x;
}

That usually does the trick. But this has always seemed wrong. I can’t say why, it just feels weird. Maybe it’s the fact that I’m setting a font size for no text at all.

Solution #2
This solution I found this week and actually makes more sense to me. It involves using simple HTML comments. And not the [if IE] stuff either. Just a plain simple comment:

<div id="main_nav_top_shadow"><!--Leave this empty--></div>

It does not matter what text you put in there. It’s not any command to the browser or anything. But it works, by darn! I guess IE6 feels more comfortable not having anything in that div, as long as you put a comment there.

Not only does that work, but it is the right thing to do. After all, the next guy to come along and read your code might see an empty DIV and be tempted to remove it because it contains no content. This way you can leave some nice little message pleading the proverbial programmer to leave well enough alone!

Bookmark and Share

9 Responses  |  add yours »

Safari CSS Hack

I just came across an article about how to write CSS only visible to Safari 3.0 and Opera 9. It saved my life so I thought I’d share the love! It was especially useful to me while trying to use ‘display: inline;’ to create a horizonal nav out of an unordered list <ul>. It turns out Safari and Opera would prefer to see ‘display: inline-block;’ instead, which the other browsers don’t like at all. Now before you go test this, I will say that the only reason ‘display: inline;’ wasn’t working for me in Safari/Opera is because I’m trying to throw a background-color behind each list item <li> and pad it so that the color flows around the outside. Safari/Opera wouldn’t let me pad the list items unless they were set to inline-block.

Bookmark and Share

2 Responses  |  add yours »

piped navigation with css

You’ve seen piped navigation before. Typically found in the footer of websites, appearing as a list of links separated by a | (pipe).

There’s a nice way to fake piping using really clean, standards friendly code along with our friend, CSS. I’ve been using this method for years now and am quite happy with it. Basically, you start by creating a typical unordered list containing the list of links.

<ul id="piped">
  <li><a href="first">One</a></li>
  <li><a href="second">Two</a></li>
  <li class="last"><a href="third">Three</a></li>
</ul>

I’ll explain the class=”last” in a minute.

Now in your stylesheet you tell the list to be displayed without any bullets and displayed inline rather than stacked as is the default. You also give each link a nice pad on both the left and right. To fake the pipe, you simply add a border to one side. This is where the class=”last” comes in. You don’t want there to be a border after the last link, so for that class, you can simply set the border to none.

ul#piped li{
  display: inline;
  border-right: 1px solid #000;
  padding-left: 10px;
  padding-right: 7px;
}
ul#piped li.last{
  border-right: none;
}

(Realistically, you could also do border-left and class=”first.”)

The end result:

Bookmark and Share

1 Response  |  add yours »

Using css to center an image with position:absolute

I ran into an interesting problem yesterday while building a simple web page. I needed to cause the main image at the top of the page to do two things: 1) Be centered on a liquid layout page, so that no matter what size the window the image was centered. 2) "Float" above the design on it’s own z-index.

Of course to get the image to be on it’s own layer, it must be set to position:absolute; but once you give it an absolute position it loses all references to being centered.

I thought I had the solution at one point, until I tested it in IE and it was all wrong. After a few hours of struggling, I finally came up with what now seems like a simple solution:

Adding styles to the image itself was my mistake. In this case I had to wrap the image with a div and give it an id. I then applied the following styles to that div in my stylesheet:

#image {
  position:absolute;
  width: 100%;
  text-align: center;
}

The image itself doesn’t need any other styles.

Bookmark and Share

13 Responses  |  add yours »

Search