[ Home ] - [ Articoli ] - [ Traduzioni ] - [ Altro ] - [ Appunti sui CSS ] - [ L'autore ]
Sei qui: Home > Altro > CSS Articles > Styling an image gallery with CSS
In this article I'll explain how to create and stylize an image gallery by using CSS.
We start with the following markup:
<div id="gallery"><h2>Kurt Cobain</h2><ul id="main"><li><p><img src="../img/1.jpg" alt="Kurt Cobain 1" /></p><p class="desc">Kurt 1</p> </li>...omission...</ul></div>As you can see, we use an unordered list to wrap the six images. Each image is contained inside a paragraph and has its own description.
The first styles are really simple.
#gallery {padding: 2em 0;margin: 0 auto;width: 600px;font: small Verdana, sans-serif;}#gallery h2 {margin: 0;border-bottom: 1px solid #666;}#gallery #main {width: 100%;margin: 0;padding: 1em 0;list-style: none;}#gallery #main li p {margin: 0;}We've centered the main container through its horizontal automatic margins, set a base font size and resetted all margins of the elements and used padding to add some space between the children of the main container.
We want to use a background image for the gallery's title, so our code will be the following:
#gallery h2 {width: 100%;height: 40px;background: transparent url("../img/title.png") no-repeat 0 0;text-indent: -1000em;}We've used an enormous amount of negative text indent to replace the text of the h2 element with our
background image. Now it's time to start with positioning and formatting of the elements.
We'll use the float property to horizontally align our image. The code is the following:
#gallery #main {padding-left: 20px;}#gallery #main li {float: left;width: 160px;margin: 0 40px 0 0;}#gallery #main li img {display: block;width: 100%;}To make floats appear on a new line, we set a global width for each list item that is equal to 200 pixels (160 pixels width plus 40 pixels of right margin). Since the container has a global width of 600 pixels, there is no room available for more than three floats per line. The last rule is very important and deserves a further discussion. As you can see, we've set a width in percentages on each image after turning it into a block-level element. What does it mean? Simply put, now the images will take the dimensions of their containing block. This technique works well only when the intrinsic dimensions of the images are greater (or equal) than those of their containing block. In this case, our images are 320 pixels wide and 240 pixels high, so this technique can be successfully applied. Keep in mind that percentages are always computed in pixels, so you should be very careful when using this tecnique with elements that have intrinsic dimensions (such as images).
Finally, we set backgrounds and colors on each list item.
#gallery #main li {background: #def url("../img/tl.png") no-repeat top left;color: #333;margin-bottom: 0.5em;}#gallery #main li p {padding: 0.5em 1em;}#gallery #main li p.desc {background: transparent url("../img/br.png") no-repeat bottom right;font-style: italic;}We've set a bottom margin on each list item in order to keep boxes separated. Then we've applied a background image (a rounded corner) to each list item and to the paragraph containing the textual description. To make room for the background color and images, we've set some padding on the paragraph that wraps each image. Note how each image fits its containing block thanks to the dimensions expressed in percentages.