written on:14 Feb, 2008 by Alen Grakalic
Create Resizing Thumbnails Using Overflow Property
Delicious | Digg | Stumble | Reddit | Float | Subscribe to RSS Feed
![]()
This tutorial is aimed at controlling the size of the thumbnails appearing on your page. Sometimes we don't have enough space to spare to fit in large thumbnails and yet we would like to avoid small and barely recognizable images. Using this trick we limit the default dimensions of the thumb, and show it in full size when user mouse-overs it.
Take a look at the demo | Download zip file
Overview
What we have here is not actual image resizing. It is a resizing of the thumb's visible area on mouse over. How do we do that? Using overflow property!
The overflow property defines the appearance of the content when it overflows the container area. If the container has limited size, for one reason or another, then we use overflow to define what happens. Possible values of overflow property are visible, hidden, scroll and auto. It's the combination of these values that we will use here and make this trick work. Basically, we will hide a part of the thumbnail when in default state, and show it entirely on mouse over.
The Concept
The idea behind this is, to place an image into a certain container. Since we're talking about thumbnails that container would be an <a> tag. We set the size (width and height) of the container to desired values and we set the position property of the container to relative. Image inside has an absolute position. We use negative top and left values to offset the image. Container has overflow set to hidden so only a part of the image that is placed inside the container's actual area will be visible. The rest of it will be hidden. On a:hover we set the container's overflow to visible, and reveal entire image.
![]()
The Code
This trick can be used for thumbnail lists or single thumbnails, as shown on the demo page. For markup we use standard tags
<a href="#"><img src="image.jpg" alt="my image" /></a>
Definition of the default state for thumbnails would be something like this:
ul#thumbs a{
display:block;
float:left;
width:100px;
height:100px;
line-height:100px;
overflow:hidden;
position:relative;
z-index:1;
}
ul#thumbs a img{
float:left;
position:absolute;
top:-20px;
left:-50px;
}
<a> tag has defined width and height to whatever fits into our site's design. Also, overflow is set to hidden. We then play with negative top and left values to "crop" the image to a perfect fit. If you want to take this further, you can define cropping area for every single image you have in thumb list and target the area you would like to show.
ul#thumbs a img{
float:left;
position:absolute;
top:-20px;
left:-50px;
}
ul#thumbs li#image1 a img{
top:-28px;
left:-55px;
}
ul#thumbs li#image2 a img{
top:-18px;
left:-48px;
}
ul#thumbs li#image3 a img{
top:-21px;
left:-30px;
}
.
.
.
Now, when user mouse-overs it we set the overflow to visible:
ul#thumbs a:hover{
overflow:visible;
z-index:1000;
border:none;
}
Note the z-index for both default and hovered container. This is very important because we want to place the hovered above it's siblings. Otherwise it would be placed below and the trick wouldn't be complete.
About the author:
Designer, developer and a passionate standardista with large experience in all types of front-end work. Started to get involved with web in 1999. and turned freelance in 2005., the same year he started Css Globe. Alen's work has been featured on numerous css galleries including famous Css Zen Garden official list. Available for contract work.
Enjoyed the article?
Subscribe to our RSS feed or share it with your friends.
Delicious | Digg | Stumble | Reddit | Float | Subscribe to RSS Feed
Comments
Subscribe
Join 5000+ subscribers and receive our content instantly.
Follow us on Twitter
Want to be the first to know when a new article is published?
Sponsors

Sitegrinder 2 Photoshop Plugin

Look Professional with FreshBooks
Recent Articles
- CSS do's and dont's Part 2- Markup
- Easiest Tooltip and Image Preview Using jQuery
- 4 Uber Cool Css Techniques For Links
- CSS do's and dont's Part 1: CSS Selecting
- Easy Scroll: Accessible Content Scroller






3kolone on 14 Feb, 2008 wrote:
Catar4x on 14 Feb, 2008 wrote:
Gloria on 14 Feb, 2008 wrote:
cssglobe on 14 Feb, 2008 wrote:
Ywg on 14 Feb, 2008 wrote:
ul#thumbs a{
display:block;
float:left; //[...]
}
Display block is useless here, because floating an element instantly make it a block : whatever the display property specifies (even if the cascade evaluation is superior) a floating element can be and is ALWAYS a block.
cssglobe on 14 Feb, 2008 wrote:
I have a habit of adding display property everywhere.
EmanuelBlagonic on 14 Feb, 2008 wrote:
Philippine Goji Juice on 15 Feb, 2008 wrote:
Megan on 15 Feb, 2008 wrote:
Kudos good job
Megan
flisterz on 15 Feb, 2008 wrote:
mbhayes on 15 Feb, 2008 wrote:
Thanks for the post!
Brian Rock on 15 Feb, 2008 wrote:
Eric Wendelin on 16 Feb, 2008 wrote:
Pozycjonowanie on 16 Feb, 2008 wrote:
bojan on 16 Feb, 2008 wrote:
Pepe on 20 Feb, 2008 wrote:
I also came up with an idea:
If you have more thumbnails of different width and height and you always want to display the center of the thumbnail you can easily find out which values you have to use for the "top:" and "left:" properties by using the following formula:
width-1 = the actual width of the real thumbnail
width-2 = the width you have used in ul#thumbs a
padding = the padding of the image or the p element, as used in the demo
- value = (width-1 - width-2) / 2 + padding
And the same formula for the height.
And I also found out, that by using this formula and by adding
left: -1px; (probably the size of the border, not quite sure)
top: -1px;
to ul#thumbs a:hover, you can avoid the image-position jumping a few pixels on hovering the image.
Sangesh on 20 Feb, 2008 wrote:
Shreemani on 20 Feb, 2008 wrote:
Tristan Bethe on 20 Feb, 2008 wrote:
thanks for the article
kab on 20 Feb, 2008 wrote:
Dave on 22 Feb, 2008 wrote:
cssglobe on 22 Feb, 2008 wrote:
kab on 24 Feb, 2008 wrote:
Services Group on 25 Feb, 2008 wrote:
Tom on 26 Feb, 2008 wrote:
Anyway I prefer the single image solution, rather than the gallery.
Pablo on 27 Feb, 2008 wrote:
Thanks
varmisin yokmusun on 28 Feb, 2008 wrote:
is ilanlari on 28 Feb, 2008 wrote:
adriana mullen on 29 Feb, 2008 wrote:
Lewis David on 7 Mar, 2008 wrote:
Simon James on 8 Mar, 2008 wrote:
Forumistan on 14 Mar, 2008 wrote:
polarizer on 19 Mar, 2008 wrote:
the polarizer
Mike on 21 Mar, 2008 wrote:
Basically, what this script is doing is cropping the visible image area until you hover over it, right?
So, what I need is to do is resize the image, instead of cropping it. Basically, I have a thumbnail I want scale down a bit, but then when I hover over it I want to show the full-size image, using the same nice look the demo shows here.
Any ideas for me?
Muhammad Usman on 25 Mar, 2008 wrote:
thankyou for sharing this great css trick
Val on 10 Apr, 2008 wrote:
AG on 16 Apr, 2008 wrote:
AG on 16 Apr, 2008 wrote:
.thumbs a {
display:block;
width:75px;
height:75px;
line-height:75px;
position:relative;
z-index:1;
}
.thumbs a img {/* original img is 150x150px resized to 75x75 */
width:75px;
height:75px;
position:absolute;
}
.thumbs a:hover {
position:relative;
overflow:visible;
z-index:1000;
}
.thumbs a:hover img {
width:150px;
height:150px;
}
html:
The original image is resized to 1/4 size and then displayed at full size when hover. Works in FF2x and IE6x.
oyunlar on 19 Apr, 2008 wrote:
kuaför malzemeleri on 7 May, 2008 wrote:
thanks for the demo and example code also.
Sim Kamsan on 20 May, 2008 wrote:
herzausgold on 21 May, 2008 wrote:
b on 21 May, 2008 wrote:
Thumbnail = SMALL file size.
Whith this trick you actualy load the complete galery with FULL picture file size.
One picture - ok, maybe few, galery - NO!
Alex on 27 May, 2008 wrote:
Don on 5 Jun, 2008 wrote:
Pepe on 12 Jun, 2008 wrote:
I used it in the Gallery of my Bandpage.
Keep going.
Astyan Tanks Bitter on 14 Jun, 2008 wrote:
Original use of the overflow property.
Thanks for the article.
josterr on 19 Jun, 2008 wrote:
Ionut Bucur on 25 Jun, 2008 wrote:
Zim on 8 Jul, 2008 wrote:
Oyun on 9 Jul, 2008 wrote:
Michael Study on 9 Jul, 2008 wrote:
utility warehouse on 21 Jul, 2008 wrote: