Front-enders won’t be the only ones to rejoice after reading this. I’m going to walk you through how to build a prettyfied cross-browser CSS-styled select lists.
I know for many years web designers have yelled at us front-enders about why we can’t style our site’s
select box like the one’s in the mockups they create. With the added rounded corners, customized drop down arrows, gradients, images and more; styling drop down
select boxes can become a nightmare for us. Not only is styling them a pain, but different browsers render them differently.
If you’re a pixel perfect coder like I am, I’m sure you share in my frustration trying to make those stupid drop downs look good and the same if the various browsers. Well, hopefully after reading this article you’ll be getting some better sleep at night because I’m going to make styling cross-browser CSS styled select lists using as easy as pie! Ready for some delicious code?
Enough with the small talk, let’s get into the code. First we’re going to start off by coding the structure and content of the page.
Step 1: Start with the basics
Take a look at the markup above. Most of it should be pretty straight forward for the majority of you. There’s one thing that may stick out a little and that’s the use of the
data-icon, the location of the icon for the specified list element
data-html-text, the content that will appear for the list element
Step 2: Build the functionality
This is where the magic happens. If you’re familiar with jQuery, you should be able to read and understand the markup above. It’s pretty simple and straightforward, nothing too complex.
Basically, we hid the
select box and looped through all of the items to generate the rendered HTML content. That content will turn into our styled
select box. When someone clicks on the rendered box, the list will slide down with all of the available list items from the hidden
select box. Once the user select an item, we update the visible text. Simple stuff!
Now that we’ve got the structure, content and functionality built, we need to make it prettyfied. This is where the power of CSS3 comes in.
Step 3: Make it prettyfied
That’s all the styling you’ll need to build this cross-browser CSS styled select list. Of course, you can always embellish it with your own little flair. Just remember to be vigilant about the capabilities of outdated browsers.
The Final Product
I made a point to ensure that this method of doing styled
select dropdowns would be cross-browser compatible and work with older versions. I’ve testing the code in the following browsers:
- Google Chrome
- Internet Explorer (version 7 and up!)
Let me know if you run into a different browser or a specific version of a browser that this doesn’t work on or comes out a little messed up.
I’ve demonstrated just one of the many ways to build a cross-browser CSS styled select list using jQuery. I prefer this method because of it’s simplicity and ability to style it however needed. A major bonus is that it’s cross-browser compatible, which many of the styled
select box scripts aren’t.
I’d love to see how you’ve incorporated it into your site and any feedback other developers might have.