I have prepared this example with display: inline-block
, instead of table-cell, so they can wrap and I removed all unneeded HTML markup.
Main CSS Properties
The text is centered using a pseudo element, .product:before
. Essentially this element forces the inline text to vertically align in the middle. If you need this to work in IE6 - 7, it can be replaced with a div.
The text is given 0 opacity until .product
is hovered.
The image is position: absolute
and will position itself in relation with it's position: relative
parent. This is needed to pull it out of the flow in order to align the text.
The transitions smooth out the opacity changes. .product
is given a white border so that it can transition to a black border when hovered.
Note: Currently there is no fallback for browsers which don't support opacity
, namely IE6 and 7. This can be achieved, if needed, with display: none
in an IE 6 / 7 conditional stylesheet.
2 Examples
"Show code snippet" and run it.
We don't need no silly div
The class .product
is given to the <a>
element and the entire box is a link.
<a class="product" href="href='http://kingfisher.org.au/library-shelving">
<img src="http://kingfisher.org.au/pic/library_shelving.jpg" />
<h2>Library Shelving</h2>
</a>
.product {
width: 250px;
height: 250px;
display: inline-block;
vertical-align: middle;
position: relative;
border: solid 1px #FFF;
transition: border 1s;
text-align: center;
}
.product:before {
height: 100%;
content: '';
display: inline-block;
vertical-align: middle;
}
.product img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: opacity 0.5s;
}
.product:hover {
border: solid 1px #000;
}
.product:hover img {
opacity: 0;
}
.product h2 {
opacity: 0;
transition: opacity 0.5s;
font-weight: none;
display: inline;
font-size: 1em;
}
.product:hover h2 {
opacity: 1;
}
.product {
text-decoration: none;
}
<a class="product" href="href='http://kingfisher.org.au/library-shelving">
<img src="http://kingfisher.org.au/pic/library_shelving.jpg" />
<h2>Library Shelving</h2>
</a>
With a div - "I don't want them clicking just anywhere!"
The class .product
is given to a div that contains the image and text. Only the text is a link.
The <a>
is position relative so it can be given z-index: 2
to ensure that it is clickable. This is mainly for IE8 and 9.
The image is given z-index: 1
so that it positions itself underneath the <a>
<div class="product">
<img src="http://kingfisher.org.au/pic/library_shelving.jpg" />
<a href="href='http://kingfisher.org.au/library-shelving">
Library Shelving
</a>
</div>
.product {
width: 250px;
height: 250px;
display: inline-block;
vertical-align: top;
position: relative;
text-align: center;
border: solid 1px #FFF;
transition: border 1s;
}
.product:before {
height: 100%;
content: '';
display: inline-block;
vertical-align: middle;
}
.product img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: opacity 0.5s;
z-index: 1;
}
.product:hover {
border: solid 1px #000;
}
.product:hover img {
opacity: 0;
}
.product a {
position: relative;
text-decoration: none;
font-weight: normal;
opacity: 0;
transition: opacity 0.5s;
z-index: 2;
}
.product:hover a {
opacity: 1;
}
a:hover {
text-decoration: underline;
}
<div class="product">
<img src="http://kingfisher.org.au/pic/library_shelving.jpg" />
<a href="href='http://kingfisher.org.au/library-shelving">
Library Shelving
</a>
</div>