CSS Sliding Door using only 1 image
May 27th, 2008 in CSS, Tutorials by kailoon
Before I know about this technique, I was using different images for each of the button I needed in a navigation bar. I found that it is not user friendly and also need more CSS coding. Besides, it is increasing the processing time and bandwidth in loading a site. In this tutorial, I will show you how to code the navigation bar using only 1 image. I will not cover the image creation part because I think you should know how to do that after follow few of my tutorials in the past. So, start here…
The Image

I will not show how to create this but I will let you guys download the psd file as a reference. I provided 4 types of colors as default, you can edit them according to your preferences. Download the psd here and I even prepared the slices for you. What you need in building the navigation menu is only a 493px X 24px image like below.

Before we start, head over and see what we are going to achieve from this tutorial.
Concept

The concept of the sliding door is to use a background image for the buttons in a navigation menu. I am using a span within a link in the list to hold a part of the image. the link itself will hold another part of it. Which means, the important part will be the background-image position.
HTML
<ul class="blue">
<li><a href="#" title="home">home</a></li>
<li><a href="#" title="products">products</a></li>
<li><a href="#" title="blog">blog</a></li>
<li><a href="#" title="contact">contact</a></li>
</ul>
This is a normal code to create a list. I assign a class for the <ul> (unorder-list) so that I can easily reuse the whole thing with different style but same structure.
<ul class="blue">
<li><a href="#" title="home" class="current"><span></span>home</a></li>
<li><a href="#" title="products"><span></span>products</a></li>
<li><a href="#" title="blog"><span></span>blog</a></li>
<li><a href="#" title="contact"><span></span>contact</a></li>
</ul>
Now, I add in a <span> for each of the link to hold the left hand side of the background image.
CSS
1. <ul> – Unorder-List
ul.blue {
padding: 5px;
margin: 10px 0;
list-style: none;
float: left;
}
We need to make list-style as none because no image for any list within it. I use a float left here because I am going to use float left for the <span> and also <li>. I am not going to define a width for it because this is just a sample.
For your information, you need to define a width for the container which you had float it.
2. <li> – List
ul.blue li {
float: left;
}
ul.blue li a {
float: left;
text-decoration: none;
color: #ccc;
padding: 4px 15px 0 0;
margin-right: 8px;
font: 900 14px "Arial", Helvetica, sans-serif;
}
Like what mentioned just now, there is a float for the list. padding right for 15px is for the space between the edge of the image and the link text. The margin here is for the space between the list.
3. <span> – span within the link
ul.blue li a span {
float: left;
padding-right: 15px;
display: block;
margin-top: -4px;
height: 24px;
}
For the span, we use padding right for the same purpose. However, I add a -4px margin-top because I I have a 4px padding top for the link.
4. <Hover> – mouse over action
ul.blue li a:hover, ul.blue li a.current {
color: #0d5f83;
background: url("images/blue.png") no-repeat top right;
}
ul.blue li a:hover span, ul.blue li a.current span {
background: url("images/blue.png") no-repeat top left;
}
Since we are going to have the same effect for the mouse over and active link. I combine the codes.
Here is another example with 4 colors.
Conclusion and Download
This is a very useful technique which you can re-use in page with only 1 image. It is easy to use and I hope that you guys can understand my poor English explanation. Any question, just send me an email or drop me a comment here.
















Hi Kailoon, i really impressed with your work that how you smartly used one single image without slicing it a part( though sliding door means slicing an image into two to use) but you buttons works well and they will scale to top notch. the only problem is it won’t pass validation of strict xhtml because no element in xhtml should be empty. so place the nav-text inside the span to make them work perfect and valid.
Hi – good stuff, looking at the implementation would it not be better though to wrap the buttons label in the span? This would make more sense rather than having an empty tag.
The image applies to the background on the a-tag left aligned while the span tag has same the background but right aligned.
The a-tag will need left padding and the span right padding so that the right curve doesn’t appear over the left curve.
I guess as another feature you could also put an icon image within the SPAN tag to give each element a unique identifier, set the image to vertical-align:middle; should keep it all balanced – good tutorial, it’s made me think!
Hi there
When I’m trying this, ‘Home’ is permanently highlighted regardless of what page I’m on. How do I get it so that the page I’m on is highlighted in the menu?
Great tutorial otherwise!
It is because there is a “current” class for home link. You need to use dynamic language such as php or javascript to add class to highlighted link.
Thank you. I had to change it into a div so that it coild work with the CMS (joomla). It still cuts out either the left or the right edge of the image in the navigation generated by the cms. How do i solve this?
If possible, can you paste your code somewhere? So that I can check it out?
Excellent tutorial Bro
How do you center the menu on the page?
I can’t get what you mean…
I think he means that when the links are listed left to right… the list as a whole isn’t centered in the webpage.
For instance, I am trying to put 5 buttons side-by-side under my banner on the top of my webpage… the banner is centered so the buttons must be centered. The button on the left of the list is all the way on the left, and the button on the right doesn’t reach all the way to the right… the list isn’t centered on the screen.
How can we adjust the values in the CSS sheet to make the list center itself on the webpage (meaning distributed evenly horizontally but in the middle of the page)
thx…ur site was very useful..^^
nice
what if the corner is transparent?
Very nice!
I´m trying a typo3-version.
Instead of:
1.
2. home
3. products
4. blog
5. contact
6.
I´m using
1 = TMENU
1 {
wrap = |
NO = 1
NO.linkWrap = |
}
The css is identic. Unfortunately this doesn´t work. Does anyone have an idea to solve the problem?
With best regards!
1 = TMENU
1 {
wrap = (ul class=”blue”)|(/ul)
expAll = 1
NO.wrapItemAndSub = (li)|(/li)
NO.stdWrap.wrap = (span)(/span)|
}
Use “” instead of “(” and “)”.
This is too perfect!!!
Thanks for tutorial!
It’s a great menu to learn CSS.
Thanks, very easy to understand and implement
Really cool tutorial. It help me on my proojects.
very neat….. thanks
Waw Cascading Style Sheets tuty, thank for sharing to us…
I lke the site…
Thanks ….
Thank you.
this is good!
amazing… love it buddy… helps me a lot… can you post more about CSS tricks…
can it work for blogspot??
Very nice…. Thats Really Great!
Thanx ………………
Thank for the awesome tips!
kailoon in one of the best site for css
mamjed :
Hmm.. okie, I will try
hey can you write a tutorial on how to implement this into a drop down menu? thank!
found FF button padding fix:
button {padding:0; border:0}
button::-moz-focus-inner {padding:0; border:0}
button:focus {outline: 1px dotted}
http://yoyurec.in.ua/button_with_1_image/
i think there are problems with using block elements (floating span) in button
i just change your link style (vertical align) from padding (and span negative margin) to height=line-height.
theoretically code is right, but:
Opera 9.5 – good, FF3 – strange padding near span, IE7 – doesn’t understand floating and button text shifted to 2nd line (((
looks like the best way is to wrap whole button text in span, like here -
http://filamentgroup.com/lab/styling_the_button_element_with_sliding_doors/ and then add little changes for using your idea of 1-image slidind doors…
PS: sorry for my english
YoYurec :
Mind to show me the sample?
hi! good article!!!
but what about styling forms in this way? i mean making instead links?
i change html & style in your example (a -> button)… works fine, but i have strange left padding in FF3 (((( have you any ideas? 10x!
Wow ~
Thanks
Here is another version for TABS
http://www.vision.to/single-image-tabs-with-three-state-rollover.php
feha :
Cool
Hi
here is my version
http://www.vision.to/single-image-sliding-doors-and-rollover.php
HI i need your help i really want to create my own website/web page but i dont know how to go about doing it so can you please help me out
css Font examples , Properties , Attribute – - //
http://www.css-lessons.ucoz.com/font-css-examples.htm
Cool Tutorial, How about to get the page validate in the span use
Ahh, those spans do wonders – thanks for the article!
Ross Johnson:
Ya, the good thing to have css based website is cutting up unnecessary codes and images. Thanks!
John:
luckily readers still can understand and they are so kind
Thanks for informing me on this.
Opps, typo
Shreemani:
Thank you!
existdissolve:
Hi there, wow. cool ~
Fazal:
Huh? Really? I don’t have such problem. Anyway, thank you for your suggestion:)
Buzu:
Thank you.
Phunky:
Thank you. yeah~ but I prefer to put it as empty
smt:
thanks for your explanation
Pretty cool technique. Thanks mate.
Your concept example shows “<span”… this is not correct.
Pretty smart, saves a bunch of time from cutting up every sliding door image that you plan on creating.
If nothing else it saves the amount of code and server calls to download the two separate images, etc…
Excellent tutorial Bro. It’s a creative solution to that issue.
nice technique, however you cant align PNG’s to the right as background images in IE6 and below…using a GIF in that situation would suffice.
Good article–I am using a similar technique on the submission form field “hints” at http://css-imagine.com.
@jeffjose @Palantir
try to do the same with n imgs and js, with dreamweaver you can do it in minutes.
you will see by yourself that this tecnique is a lot more fast, because it neads only 1 http request and because css sliding is very fast
Palantir, its not a image swap as its using the same single image – but just moving the background-position of the image instead.
There is no need to leave the SPAN’s as empty tags they should really hold the text of the ANCHOR and can still give the same effect.
thnx for share
Thanks
Bro, I love your Blog’s Theme, its so cute.. I wish I had it
Nice work on the skin buddy..
Yeah, I dont see anything special here. Its just a swap image or a roll over effect which is pretty easily implementable.
great tutorial kailoon. now i know how to make sliding door with ur clear tutorial. thx dude for sharing
thanks Bel..
Good tutorial!
I don’t get it…what’s so special about it ? it is just a swap image…
Haha…the only comment we have here
thanks dude ….
WOW excellent tutorial! thnxs i will have it in ming this one! I think that’s the best and simplest tutorial for a blog menu!