Actions

Creating clickable background images using CSS

From Joomla! Documentation

Revision as of 12:57, 29 April 2013 by Wilsonge (Talk | contribs)

What this is about

Okay, lets say you have a mostly finished template, with a typical corporate layout: A header, a content body area, and a footer. In the header you have placed a background image with a big company logo and some nice artwork next to it, with some dynamic content on top of the bottom right corner of the background image. Suddenly you realise that a click on the company logo part of that background image should bring the user back to the homepage. Usually you would cut out the image and place it directly inside the link. However you don't have enough time to cut up the image and re-work your template accordingly, so what you need is a quick-fix.

At this point your HTML structure might look something like this:

<div id="site">
    <div id="full-width-header">
        <div id="header-content">We love using Joomla!</div>
    </div>
    <div id="body-content">OSM saves the world!</div>
    <div id="footer-content">(c) the really cool web-designer</div>
</div>

And your CSS like this:

#full-width-header {
    background: url(header-logo.jpg);
    width: 800px;
    height: 172px;
}
#header-content {
    position: relative;
    float: right;
    width: 400px;
    height: 172px;
    vertical-align: bottom;
    text-align: right;
}

Note: Legitimate uses for the following technique might include things like tab-interfaces where the tabs should be able to stretch and still have a background image, or blog-skins which only differ in CSS.

How to do it

First: You can't just copy the #header-content div, position it over the logo, make the content invisible and enclose it in an anchor-tag. That would be broken HTML, because you can't place block-level elements like div inside anchor tags.

You can however enclose a stretched, one-pixel, transparent GIF image in anchor tags (as per example one) if you have to support old browsers. Otherwise you can simply turn the anchor itself into an inline-block using CSS 2.1 (as per example two).

Example 1 (pre CSS 2.1) HTML:

<div id="site">
    <div id="full-width-header">
        <a href="/"><img src="transparent.gif" id="home-link" alt="Nav: Home" /></a>
        <div id="header-content">We love using Joomla!</div>
    </div>
    <div id="body-content">OSM saves the world!</div>
    <div id="footer-content">(c) the really cool web-designer</div>
</div>

Example 1 (pre CSS 2.1) CSS:

#full-width-header {
    position: relative; /* necassary to
        absolute-position the child-element
        #home-link relative to the header */
    background: url(header-logo.jpg);
    width: 800px;
    height: 172px;
}
#home-link {
    position: absolute;
    width: 200px;    /* width of the logo */
    height: 172px;   /* height of the logo */
    top: 0; left: 0; /* top-left corner of logo */
    border: 0;
    float: left;
}
#header-content {
    position: relative;
    float: right;
    width: 400px;
    height: 172px;
    vertical-align: bottom;
    text-align: right;
}


Example 2 (CSS 2.1) HTML:

<div id="site">
    <div id="full-width-header">
        <a href="/" id="home-link">Home</a>
        <div id="header-content">We love using Joomla!</div>
    </div>
    <div id="body-content">OSM saves the world!</div>
    <div id="footer-content">(c) the really cool web-designer</div>
</div>

Example 2 (CSS 2.1) CSS:

#full-width-header {
    position: relative; /* necassary to
        absolute-position the child-element
        #home-link relative to the header */
    background: url(header-logo.jpg);
    width: 800px;
    height: 172px;
}
#home-link {
    position: absolute;
    display: inline-block;
    width: 200px;    /* width of the logo */
    height: 172px;   /* height of the logo */
    top: 0; left: 0; /* top-left corner of logo */
    border: 0;
    float: left;
    visibility: hidden;
}
#header-content {
    position: relative;
    float: right;
    width: 400px;
    height: 172px;
    vertical-align: bottom;
    text-align: right;
}