Easy Display Switch with CSS and jQuery
By Soh Tanaka | Published March 9th, 2009 in TutorialsToday, I would like to go over a quick and simple way to allow your users to switch page layouts by using CSS and jQuery.
Today’s web users expect web pages to be increasingly more interactive. To this end, the ability to change page layouts provides your users with a more immersive experience and allows them to consume information more easily, either with a quick gallery view, or a detailed summary view.
Display Switch Tutorial:
Step1. Creating the Wireframe
We will first start out designing the vertical list layout with a simple unordered list which we will use as our columns/rows.
HTML
CSS
*Note I used different shades of grey to achieve the subtle bevel look.
color: #e7ff61;
text-decoration: none;
}
ul.display li .content_block {
padding: 0 10px;
}
ul.display li .content_block h2 {
margin: 0;
padding: 5px;
font-weight: normal;
font-size: 1.7em;
}
ul.display li .content_block p {
margin: 0;
padding: 5px 5px 5px 245px; /*--The left padding keeps the
content from flowing under the image--*/
font-size: 1.2em;
}
ul.display li .content_block a img{ /*--Double border technique--*/
padding: 5px;
border: 2px solid #ccc;
background: #fff;
margin: 0 15px 0 0;
float: left;
}
Step2. Styling the Content
Inside of each list item, we nest a div which will act as our container of the content inside. I feel it’s easier to calculate the columns when you don’t have to consider the margin and padding within. This comes in handy especially when you are working with a liquid layout (since we know % and fixed pixels don’t mix in too well).
HTML
CSS
color: #e7ff61;
text-decoration: none;
}
ul.display li .content_block {
padding: 0 10px;
}
ul.display li .content_block h2 {
margin: 0;
padding: 5px;
font-weight: normal;
font-size: 1.7em;
}
ul.display li .content_block p {
margin: 0;
padding: 5px 5px 5px 245px; <span style="color: #777;">/*--The left padding keeps the
content from flowing under the image--*/</span>
font-size: 1.2em;
}
ul.display li .content_block a img{ <span style="color: #777;">/*--Double border technique--*/</span>
padding: 5px;
border: 2px solid #ccc;
background: #fff;
margin: 0 15px 0 0;
float: left;
}
Step3. Creating the Secondary View Option
Now its time to style the second display option which are in columns flowing horizontally.

CSS
to accommodate for the three column layout--*/</span>
ul.thumb_view li h2 { display: inline; }
ul.thumb_view li p{ display: none; }
ul.thumb_view li .content_block a img { margin: 0 0 10px; }
Step4. Creating the Switch
Time to wrap up this tutorial by creating the switch.

HTML
CSS
Using the ‘CSS Sprites’ technique, I create the link button.
width: 122px;
height: 26px;
line-height: 26px;
padding: 0;
margin: 10px 0;
display: block;
background: url(switch.gif) no-repeat;
outline: none;
text-indent: -9999px;
}
a.swap { background-position: left bottom; }
I didn’t feel like creating a hover state on the image, so I’ll just use the opacity to dim it down a tad bit once hovered over.
filter:alpha(opacity=75);
opacity:.75;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=75)";
}
jQuery
At last, we’ll activate the switch with a little bit of jQuery.
$(document).ready(function(){
$("a.switch_thumb").toggle(function(){
$(this).addClass("swap");
$("ul.display").fadeOut("fast", function() {
$(this).fadeIn("fast").addClass("thumb_view");
});
}, function () {
$(this).removeClass("swap");
$("ul.display").fadeOut("fast", function() {
$(this).fadeIn("fast").removeClass("thumb_view");
});
});
});
</script>
Final View
Now Go and Experiment!
This method can be applied to many scenarios, so I would like to encourage you to experiment with this technique. If you have any other tricks to add on, please feel free to drop your comments!
About the Author:
Soh Tanaka is a Web Designer and blogger from Los Angeles. For more front-end web development tutorials, check out his blog or you can follow him on twitter @SohTanaka





Very nice. Simple technique with excellent results.
Good job.
Very impressive; I’m not exactly sure how I will implement this yet, but I’ll let you know!
This is an awesome tutorial. Thank you and I will definitely be using this.
Nice effect, Soh. I just wonder how it looks to a site visitor who has javascript turned off?
Where possible I think it’s important to use javascript functions which degrade gracefully, and if this does so then that would be doubly useful.
[...] Easy Display Switch with CSS and jQuery [...]
MMMMMMMMMMMM
Nice!!
Excellent tutorial! I definitely should implement it on one of my blogs!
Nice…
nice work, thanks for the helpful tut..
Great tut!
It´ll help a lot my recent project!
Great tutorial Soh!
popurls.com // popular today…
story has entered the popular today section on popurls.com…
awesome work
Just a little bug if you double click the switch view button. The views get mixed up.
That’s cooooool.
[...] Easy Display Switch with CSS and jQuery [...]
Tracey Grady’s right. Graceful degradation is important, and this does not accommodate that.
Looks cool otherwise though.
wow..it’s so cool bro..
thank u for your post..
i got the same thing as theBagg.
How hard would it be to add pagination into this? I love the effect and would love to have 3 different display types, along with pagination to reduce the load times of a million images.
never thought it actually that simple.
i think it gonna use some crazy java
code, but nope. it just that..
thanks.
Fantastic! Such a simple idea and simple code too for a fabulous result.
great tutorial cheers
[...] Easy Display Switch with CSS and jQuery (tags: css jquery javascript) [...]
This is a simple but awesome thing! Congratulations!
Very nice, and the code is well documented and easy to follow. Thanks.
Thanks a lot! Wonderful piece of code!
It will be useful for my future projects!!!
Thank you all for the comments, Im glad that you guys liked it. Looking forward to seeing some nice stuff with this technique
@Tracey Grady, @Mike Mella, when javascript is turned off, there is nothing hindering the user from the content since all we are doing is showing them less info when the button is clicked. To make the button not appear when js is turned off, we can simply add a “display: none;” on the button, and .show() once jquery is done loading
@theBagg, @WireframingTools, can you please let me know which browser had the buggy issue? I was trying to make it skip but I wasn’t able to pull it off.
Thank you again!~
Incredible
Very nice
thank you
Pretty slick
[...] Easy Display Switch with CSS and jQuery [...]
It’s great to see how easy such an elegant effect can be coded. Thanks gor the tutorial.
Well written and easy enough for a CSS layman to follow!
[...] Easy Display Switch with CSS and jQuery A really nice way to dynamically load alternating CSS files to switch styles! (tags: css jquery javascript webdesign ajax) [...]
[...] Easy Display Switch with CSS and jQuery (tags: jquery webdesign css javascript styleswitcher) « links for 2009-03-09 [...]
[...] Easy Display Switch with CSS and jQuery Great tutorial of jQuery and CSS Display Switch (tags: webdev tutorials jquery javascript tutorial css ajax) [...]
This would be great for a gallery, thanks for the tut Sol.
I was looking for a nice way to promote some category thumbnails with different variations.
I just found it! Nice work Tanaka.
[...] Easy Display Switch with CSS and jQuery A very slick result (tags: css jquery switch) [...]
Who have javascript turned off these days
?
I whould sure use it!
[...] a quick and simple way to allow your users to switch page layouts by using CSS and jQuery on “Easy Display Switch with CSS and jQuery“. The technique is quite simple but the result is very nice and [...]
[...] Fuente: Designm.ag [...]
nice but you miss one point
another switch to the real size of the image would be great
as i got my own portfolio with thumbnail, normal size and big size
and no i dont want another lightbox for big size
only jquery rocks !!
nice!!!
No cookie to remember witch layout was last used?
Quite useless…
[...] Switch Page Layouts – easily switch page layouts by using CSS and jQuery. [...]
Soh,
Good stuff – helpful to many!
Great! simple but excellent result
Very nice.
I’m going to have to learn me some jquery.
Layout works fine with javascript disabled, but none of the switching works. That doesn’t seem to be a problem for anyplace that I would use this.
Very nice!
Would be good to add a cookie to remember the last layout.
How would you invert the process from thumb view to list?
thanks!
[...] Easy Display Switch with CSS and jQuery (tags: jquery webdesign css styleswitcher) [...]
Simply beautiful!
useful tutorial. Thanks a lot
[...] Easy Display Switch with CSS and jQuery Easy Display Switch with CSS and jQuery on Design M. [...]
Hey guys, I’m looking into the cookie issue, I had this in mind when making this tutorial so it was actually intended for a smaller site w/out paging. But to make this useful for sites of all sizes, its a must~
@Frank, If you want to reverse the order, your going to have to set the default list style to have the thumbs first, and in your jquery, your going to addClass your list style to the thumb layout. Also don’t forget to change the css sprites on the button as well~
OMG, I want to use this! It’s so snazzy! Thanks.
[...] Easy Display Switch with CSS and jQuery – Offer multiple views of your web content! [...]
Very nice and simple! thank you!
[...] via Easy Display Switch with CSS and jQuery. [...]
mmm.. good idea!!
Great Tutorial! I’m gonna surely use this this technique! Thanks for sharing!
Can you try to use cookie on this? Like on the bestwebgallery style. Because everytime I open the demo it will back to the default style.
[...] Easy Display Switch with CSS and jQuery Today, I would like to go over a quick and simple way to allow your users to switch page layouts by using CSS and jQuery. [...]
[...] more easily, either with a quick gallery view, or a detailed summary view. With this awesome jQuery tutorial, you can allow your users to switch page layouts in a very easy manner. Also it looks so [...]
nice
Well, it’s a 94% solution for me.
It works great in Firefox and Explorer. But it looks like the switching part is broken in Chrome and Safari. The content_block DIVs have the correct width, but they end up being a narrow list on the left side of the page.
I’ll still use it on a personal site, but even with only 6% Safari users, I can’t use this on one of my ecommerce sites.
Nice!
Will work perfectly on my webshop =)
[...] Nah ini yang ditunggu-tunggu, image gallery dapat berubah (switch) dengan simple konsep dari thumbnail kecil hingga ada spesifik dengan keterangan seperti contoh gambar diatas. Jika ingin mengetahui cara-cara membuat switch view gallery, kunjungi disini. [...]
[...] Continue reading here. Share and Enjoy: [...]
[...] Easy Display Switch with CSS and jQuery a quick and simple way to allow your users to switch page layouts by using CSS and jQuery. (tags: webdesign webdev jquery css tutorial gallery style styleswitcher) No TweetBacks yet. (Be the first to Tweet this post) Sure, go on and share this [...]
[...] Easy Display Switch with CSS and jQuery [...]
[...] Read Article… [...]
good job!!
[...] us a quick and simple way to allow your users to switch page layouts by using CSS and jQuery on “Easy Display Switch with CSS and jQuery“. The technique is quite simple but the result is very nice and [...]
[...] Easy Display Switch with CSS & jQuery [...]
[...] How To » Easy Display Switch with CSS and jQuery [...]
Great tutorial.:)
Awesome tutorial, I’ve been looking for something like this
Hi there,
First off all, very nice trick with css and jquery.
I do have a question though. Would it be possible (easy) to add a “third” display option?
I am very new to jquery and wouldnt exactly know if toggle could accomplish what i am looking for.
Hope to hear from you.
Thanks!
Great idea, thanks for the tip.
[...] Easy Display Switch with CSS and jQuery | change, css, display, jquery, layout, swap, switch, view [...]
this very cool, thx for share
Man you know how to make a good tutorial. Great
Thanks for share ! I like this
Wow, never though that Tabs can use like this.
Nice idea
Good idea, great.
Very nice, i would think about adding a cookie, so it remembers your switch view for the next time you visit.
thanks for sharing.
Very nice mate keep Up…
its possible to start with the thumb_view (2)
10X mate
[...] 3. Easy Display Switch with CSS and jQuery [...]
[...] Easy Display Switch with CSS and jQuery(Via: DesignM.ag), Demo Category: Tutorial| Tags: jQuery, Switch |Permalink [...]
Thank you so much, this tutorial give me some idea..!!
Thank you..
[...] Easy Display Switch with CSS and jQuery | change, css, display, jquery, layout, swap, switch, view [...]
[...] Switch Page Layouts – easily switch page layouts by using CSS and jQuery. [...]
Awesome Tuts Soh, thanks for sharing this.
[...] Easy Display Switch with CSS and jQuery This tutorial outlines a method for using jQuery to smoothly-transition into different viewing modes, which can be helpful in image galleries. [...]
[...] Easy Display Switch with CSS and jQuery This tutorial outlines a method for using jQuery to smoothly-transition into different viewing modes, which can be helpful in image galleries. [...]
[...] Easy Display Switch with CSS and jQuery This tutorial outlines a method for using jQuery to smoothly-transition into different viewing modes, which can be helpful in image galleries. [...]
[...] Easy Display Switch with CSS and jQuery [...]
[...] Easy Display Switch with CSS and jQuery This tutorial outlines a method for using jQuery to smoothly-transition into different viewing modes, which can be helpful in image galleries. [...]
[...] Tutorial auf designm.ag zeigt wie man ganz einfach mit CSS und jQuery ein Gallery-Ansicht zu einer detailierten [...]
[...] Easy Display Switch with CSS and jQuery (tags: jquery ui css) [...]
Great script; can think of many uses.
But: is there a way to stop it from jumping to the top of the page when one clicks the switch?
Soh, Were you ever going to get it working with a cookie? That would be really appreciated.
Awesome script. Thanks for sharing.
[...] Easy Display Switch with CSS and jQuery This tutorial outlines a method for using jQuery to smoothly-transition into different viewing modes, which can be helpful in image galleries. [...]
Pretty freakin’ sweet! I put this tutorial to good use at 83themes.com. Thanks for making it so easy!
Thank you so much, that’s an awesome gallery! So fresh and so clean, clean.
been searching all over Google for a non-complicated way to add reviews and ratings and it looks like this is it.
Thank you a million times over.
[...] http://designm.ag/ Format: CSS, jQuery Tutorial Link: Tutorial Bilderanzeige mit css und jquery switchen Thread [...]
Did any one ever figure out how to keep the page from jumping back to the top of the site? – Rihanna
Very Nice,It is a good job!!!
Very very cool!
I have been trying and trying to get the cookie to work because I have multiple pages but just can’t get it. Did ANYONE get a working version with a saved cookie?
That is a great display switch plugin! I might have to use this with a project I am working on (:
I’m also running into a page jump problem. I tried to replace the “#” hashes with a…
onclick=”return false;”
… but that doesn’t fix the issue. Any idea?
A URL example is available by clicking my name link.
Can someone tell me how i can use that gallery seperately twice or more often on one site?
So that i can have different categories and when i change the display method of one category only this one changes and not all on the site.
[...] Easy Display Switch with CSS and jQuery [...]
[...] Easy Display Switch with CSS and jQuery This tutorial outlines a method for using jQuery to smoothly-transition into different viewing modes, which can be helpful in image galleries. [...]
Nice. The good thing is that its simple and easy to read for anyone!
[...] web: http://designm.ag/tutorials/jquery-display-switch/ Demo: [...]
[...] Easy Display Switch with CSS and jQuery A tutorial by Soh Tanaka on DesignM.ag that shows how you can achieve a similar effect as Best Web Gallery to give visitors layout options. [...]
Very Nice,It is a good job!!!
thanks so much
Great tutorial. Thanks for taking the time to write this!
I’ll use this on one of my projects. this is very helpful, thanks for sharing your knowledge
Cool, this is excellent. I will definately be using it on my site…..
[...] #8 – Easy Display Switch with CSS and jQuery [...]
thanks for this great tutorial.
we can use this for wallpaper websites… or photo sharing websites…But we should store this in database
[...] Vertically Scrolling Menu Easy Display Switch with CSS and jQuery [...]
nice one, even tho a cookie is missing
Hi this is very nice. I have everything working except the image for the switch doesn’t show. The spot is a hot spot and toggles but for some reason I can’t get the switch.gif to show up any suggestions?
thanks
[...] Some people prefers to read a list of blog exerpts in lines, so other prefers a column display. With this tutorial you’ll learn how to create a jQuery display switch so your readers will be able to choose their favorite display. » Ver Tutorial [...]
Hello,
great tut!
Can someone tell me how i can show the thumb gallery first?
thx
When you’re thinking about graceful degradation, you have to think, “Is this functional and usable to everyone?” So with consideration to javascript and the Switching Views, I’d say this degrades gracefully… it’s layout is still semantic XHTML with CSS. There’s only one catch. If javascript is not available to the user viewing this, they still see the list of items/images, they just don’t have the added feature available to switch views. So a tweak to this solution would be to write the switch_view div using javascript, so that if js isn’t available, then the user won’t even see the option to switch views. Otherwise… if you absolutely must have this necessary feature that degrades gracefully and all that marketing hype you scanned over on alistapart, you would have to work it in using some server side language to control the styles (which would require a page reload at least once until the option is set).
If you want to prevent it from causing the page to jump to the top, remove the hyperlink and use span instead.
[...] us a quick and simple way to allow your users to switch page layouts by using CSS and jQuery on “Easy Display Switch with CSS and jQuery“. The technique is quite simple but the result is very nice and [...]
Nice One. Very Appreciative.
It helped me save a lot of time and effort with the readymade coding posted here.
You can simplify this script using the ‘toggleClass’ function in jQuery:
$(“span.switch_thumb”).click(function () {
$(“span.switch_thumb”).toggleClass(“swap”);
$(“ul.display”).fadeOut(“fast”, function() {
$(this).fadeIn(“fast”).toggleClass(“thumb_view”);
});
});
Also, if you want to use a cookie to remember the view state, use the jQuery cookie plugin (http://www.stilbuero.de/index.php?PHPSESSID=09n8bbbakpkaamhk6hirk168f6&s=cookie&x=0&y=0), and then add to your code like this:
$(“span.switch_thumb”).click(function () {
$(“span.switch_thumb”).toggleClass(“swap”);
$(“ul.display”).fadeOut(“fast”, function() {
$(this).fadeIn(“fast”).toggleClass(“thumb_view”);
});
$.cookie(‘view_State’, $(‘ul.display’).is(‘.thumb_view’) ? ‘list’ : ‘thumbs’ );
});
// COOKIES
// view state
var view_State = $.cookie(‘view_State’);
// Set the user’s selection for the viewState
if (view_State == ‘thumbs’) {
$(“ul.display”).addClass(“thumb_view”);
$(“span.switch_thumb”).addClass(“swap”);
};
[...] grid view or as a detailed list view. All this was done with some jQuery magic. NetTuts+ and DesignMag have some great tutorials on how to do it [...]
Any restrictions on usage? Can I put this into my templates that I distribute and or sell? I build WordPress themes.
Jared,
Feel free to use it in your templates or themes.
[...] information more easily, either with a quick gallery view, or a detailed summary view.” Demo | Project [...]
[...] Hay gente que prefiere ver una lista de imágenes con título en vez de mostrar más texto. así de un simple vistazo se puede mostrar mucho más contenido, puedes ver un tutorial para tratar imágenes con Css y JQuery y dar a tus usuarios la opción de cambiar la vista al vuelo. Ver tutorial >> [...]
[...] How to Create an Image Gallery with easily Switchable Views [...]
[...] How to Create an Image Gallery with easily Switchable Views [...]
This is a great tutorial, I’m a big fan of your work mr. Tanaka, thank you for putting time back into the community. You can see my working version of this scrip on my blog: domeprojects.com Thanks again.
[...] ease of design, and he is kind enough to give back to the community in a major way. His tutorial Easy Display Switch with CSS and JQuery made setting up this feature a [...]
[...] Homepage:http://designm.ag/tutorials/jquery-display-switch/ [...]
Thanks for share. I was looking for it.
Its simple and easy to read for anyone, great!
Awesome work friend….
[...] how to manually change a layout, and it shows two great examples and “how they did it.”Easy Display Switch with CSS and jQueryA quick and simple way to enable users to switch page layouts using CSS and jQuery.Quick Tip – [...]
I use it in my website http://www.danilab.net , very nice, Soh Tanaka!!
[...] Easy Display Switch with CSS and jQueryA quick and simple way to enable users to switch page layouts using CSS and jQuery. [...]
Thanks for sharing! Nice and easy… Will be using it on a new project!
[...] Easy Display Switch with CSS and jQueryA quick and simple way to enable users to switch page layouts using CSS and jQuery. [...]
[...] Easy Display Switch with CSS and jQuery A quick and simple way to enable users to switch page layouts using CSS and jQuery. [...]
I also wanted to use this with cookies and I tired the Sam’s suggestion with no luck:
“You can simplify this script using the ‘toggleClass’ function in jQuery:
$(“span.switch_thumb”).click(function () {
$(“span.switch_thumb”).toggleClass(“swap”);
$(“ul.display”).fadeOut(“fast”, function() {
$(this).fadeIn(“fast”).toggleClass(“thumb_view”);
});
});
Also, if you want to use a cookie to remember the view state, use the jQuery cookie plugin (http://www.stilbuero.de/index.php?PHPSESSID=09n8bbbakpkaamhk6hirk168f6&s=cookie&x=0&y=0), and then add to your code like this:
$(“span.switch_thumb”).click(function () {
$(“span.switch_thumb”).toggleClass(“swap”);
$(“ul.display”).fadeOut(“fast”, function() {
$(this).fadeIn(“fast”).toggleClass(“thumb_view”);
});
$.cookie(‘view_State’, $(‘ul.display’).is(‘.thumb_view’) ? ‘list’ : ‘thumbs’ );
});
// COOKIES
// view state
var view_State = $.cookie(‘view_State’);
// Set the user’s selection for the viewState
if (view_State == ‘thumbs’) {
$(“ul.display”).addClass(“thumb_view”);
$(“span.switch_thumb”).addClass(“swap”);
};
It won’t change views anymore if I use this. Any suggestions?
Cool effect! Thanks man!
[...] Easy Display Switch with CSS and jQuery A quick and simple way to enable users to switch page layouts using CSS and jQuery. [...]
[...] Easy Display Switch with CSS and jQuery A quick and simple way to enable users to switch page layouts using CSS and jQuery. [...]
[...] Easy Display Switch with CSS and jQueryA quick and simple way to enable users to switch page layouts using CSS and jQuery. [...]
Wow! I’ve been looking for this feature. Now my other site just using different theme to change the display type. But this, is like amazing! Might compatible with wordpress? I’ll try this..
[...] Easy Display Switch with CSS and jQueryA quick and simple way to enable users to switch page layouts using CSS and jQuery. [...]
This is a nice fine for me. Cool effect.
[...] Easy Display Switch with CSS and jQuery A quick and simple way to enable users to switch page layouts using CSS and jQuery. [...]
+1 for a cookie ! thanks
[...] Easy Display Switch with CSS and jQuery [...]
[...] Homepage:http://designm.ag/tutorials/jquery-display-switch/ [...]
I defiantly liked the tutorial and this made me to think why would a user would like to see such features and functionality if at the very next refresh of the page his/her preference will get flushed….
How do other people think about this and how can we take this really awesome tutorial to the next level.
Some people were asking about the ‘jump’ that happend on the anchor (a href=”") element. This will always happen on an anchor/href element. Change the switch trigger to something else. I think just deleting the href=”" altogether will work. A better solution would be to change it to a (button) element.
It doesn’t work :S
Great tutorial, the switch is so useful… just need to find a use now!
[...] http://designm.ag/tutorials/jquery-display-switch/ [...]
[...] Easy Display Switch with CSS and jQuery – DesignM.ag Today’s web users expect web pages to be increasingly more interactive. To this end, the ability to change page layouts provides your users with a more immersive experience and allows them to consume information more easily, either with a quick gallery view, or a detailed summary view. (tags: tutorial css webdesign styleswitcher switch) [...]
[...] explains how to manually change a layout, and it shows two great examples and “how they did it.”Easy Display Switch with CSS and jQueryA quick and simple way to enable users to switch page layouts using CSS and jQuery.Quick Tip – [...]
Great tut Soh,
for those of you that cannot get the cookie to set,
from the kind offer of the code by ‘Sam’ you need to replace the curly quotes that the comment system has added and replace them with normal double quotes, if that makes sense…
Hi
I tried that one out but i doesnt work. The css is incomplete.
[...] Easy Display Switch with CSS and jQuery Go To Source [...]
[...] Tutorial Tutorial Page [...]
For all still having problems with the cookies enabled version. Here is the complete working script code:
http://padexx.pa.ohost.de/togglecookies.txt
And you also need the above mentioned jquery cookies plugin and call it right after your jquery call.
http://plugins.jquery.com/project/cookie