Close and Go BackBack to Viget

Styling the Button Element in IE

Rob Soule
Rob Soule, ON THE TOPIC OF CSS and Tips and Tricks
Mar26 29

If you’ve ever attempted to style a button inside IE, you know that it can be a huge pain. On most of our projects, we’ve now started using the button element in place of traditional input buttons due to the ease of styling and consistency across browsers. One of the first things you might notice when you bring your nicely styled buttons up in IE is that, for reasons unknown to me, IE decides to stack the padding depending on the width of the content inside the element.

image

After spending some time adding a lot of extraneous CSS to the IE specific style sheets, I realized that two simple lines of code fixes the problem.

button {
  width: auto;
  overflow: visible;
}

Those two lines of code will save you countless hours. Go give your button elements some extra love.

UPDATE: July 16, 2008 - Please note that this fix DOES NOT work for buttons contained inside table elements.

josh said on 03/26 at 07:24 AM

Thanks for the tip, Rob. I’ve been having some IE issues on my personal blog.

I wonder if there is a CSS snippet that might automatically force IE users to download Firefox or Flock, close and delete IE from their computer, and reopen the page in the new browser? I could see some value in that…

Rob Soule said on 03/26 at 07:27 AM

Seriously, I would love that. In the meantime checkout http://savethedevelopers.org.

Doug Avery said on 03/26 at 10:10 AM

Rob, bless you for propagating this tip to out into the internets. It’s a quick fix for a maddening problem, and I’m sorry I erased it from its rightful place on the whiteboard.

shay said on 03/26 at 10:31 AM

LOL i love your assertion that IE land and the real world are two different “places.”

Neal G said on 03/31 at 05:24 AM

It appears that IE still applied top and bottom padding differently than other browsers. Can you find a fix for that? I’ve tried padding, and height: auto;

Rob Soule said on 03/31 at 05:37 AM

@Neal - Yeah, IE can still be a bit quirky about the top and bottom padding. It’s usually a couple of pixels off. What I’ll typically do to get around that is declare different top and bottom padding values in the IE specific stylesheet. If you need to have things pixel perfect you can always use image replacement and use an image as your background. Hope that helps!

Colin said on 03/31 at 07:38 PM

I had previously thought this was unsolvable. Thanks for sharing. This makes for a good addition to the “reset” part of any stylesheet.

Miro said on 04/01 at 10:51 PM

This is an awesome trick. Thanks so much!

Steve Love said on 04/06 at 02:24 PM

A couple weeks ago I found out this also works to fix IE’s padding issues with traditional input buttons.

Just wondering, what cross-browser issues do you guys see with input buttons that are solved by using a button element instead? Or will it be readily apparent once I try it myself?

Rob Soule said on 04/07 at 08:39 AM

@Steve - Thanks for the tip on the input buttons! I never thought of trying that but it makes perfect sense.

The main benefit in my opinion to the button element is it’s ability to wrap content and it’s ease of styling. Typically I’ll define a style for all input fields and then end up re/un-styling my buttons, which is a pain. The W3C explains it much better than me.

Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to “image”, but the BUTTON element type allows content.

Steve Love said on 04/10 at 04:03 PM

I’m sold. Thanks for cluing me in on what I’d missed!

Ryan Miglavs said on 04/10 at 05:52 PM

Quick question: do you know what versions of IE exhibit this bug, and if this fixes all of them?

Thanks for the tip!
{ryan}

Rob Soule said on 04/11 at 08:16 AM

@Ryan - I know this problems occurs in both IE6 & 7. I haven’t had a chance to check in the IE8 beta or 5.5. And as far as I know this fix fixes the problem for both IE6 &7;.

Thanks for your interest and comment.

Bernat Lleonart said on 04/11 at 11:26 AM

@Ryan: It fixes the bug in IE 5.5, IE 6 and IE 7. IE 8 beta 1 renders the button element properly.

#Rob: Thanks a lot!

Cezary Tomczyk said on 04/11 at 04:25 PM

Nice trick. I remember that IE don’t send value param for button. Ugly fix is to add onclick="if (check_is_IE) this[removed]=’change’” to button.

Ryan Miglavs said on 04/12 at 10:54 PM

@Rob & Bernat: Thanks! I appreciate the info.

{ryan}

jive said on 04/14 at 11:51 PM

That makes perfect sense to use buttons so you dont have to restyle for submit buttons. I’ll definately be using this in the future. :)

Haiming said on 04/15 at 08:42 AM

Is that ture?I’ll try!and thank you for the good tip!

Andrew said on 04/25 at 12:52 PM

This is going straight into my CSS reset

Zach said on 05/03 at 11:58 PM

Nice trick, thanks for sharing. You guys ever notice that in FF it’ll push the text so it’s towards the bottom? I see it on this site for example. Haven’t found a fix for it. Has anyone else?

Mike said on 07/14 at 07:24 PM

I am having an issue in IE with some odd padding appearing on the outside of a button element when displayed inside of a table cell.  Thoughts?  It appears to be something directly related to the button element.

Rob Soule said on 07/15 at 07:51 AM

That certainly does sound weird. Do you have an example?

Mike said on 07/15 at 11:37 AM

http://test.baileyfam.mine.nu/button_test.html

Notice the odd margin occuring on the right side of the buttons.

Jez said on 07/15 at 05:44 PM

Thank you for this fix; always wanted to get rid of that extra space around the text on submit buttons. Do you know of a fix for IE 5.0 though?

Rob Soule said on 07/16 at 10:36 AM

@Mike - Thanks for bringing up this issue. It _looks_ like the width button bug can’t be fixed inside a table. I tried most everything. Very weird indeed. I’ll note that in the post. What did work, however, was setting a static width. That might be your best bet if you’re forced to work inside a table. Just be sure to give your text room to grow in the case of text-resizing. Good luck!

@jez - IE 5… wow… I can’t say I have a good solution. My suggestion would to lovingly nudge your users/clients to upgrade. 5.0 is ancient. :)

Jez said on 07/17 at 07:19 AM

Im my tests I’ve found that overflow-visible fixes the problem on its own and wondered why you’ve added width: auto? Also in some versions of IE (can’t remember which) the actual SUBMIT text itself, is not vertically centered inside the styled button—how would I go about fixing this?

Rob Soule said on 07/17 at 01:56 PM

@Jez - I would double check your line height. Sometimes it can be inherited which can cause vertical alignment issues.

Jez said on 07/17 at 06:07 PM

Fantastic! Thanks Rob. It seems that the incorrect line-height for the submit button text was indeed being inherited from someplace else. Everything’s cool now. By the way love the watercolor look to your site!

Himanshu Ranavat said on 08/18 at 05:11 PM

As you mentioned the fix does not work in tables, that has been fixed. For IE set the width of button to 1 and the overflow:visible will help the text in the button expand. See: http://jehiah.cz/archive/button-width-in-ie-revised

We're The Designers

at Viget Labs. We write about design news, trends, techniques, buildout, inspiration, CSS, and our projects.

What's a-twitter?

Follow us @VigetInspire for updates of the goings-on here or @Viget for more from all of the Viget crew. #thatisall

Recent Comments

@brad

It makes little difference when using native EE functionality or Structure to add pages, it’s just that if you want to add a sub page, you need to remember to assign a...

Subscribe to Comments RSS RSS

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


How many days in a non-leap year?

Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.