Creating clickable background images using CSS

From Joomla! Documentation

Revision as of 11:20, 24 October 2022 by Cmb (talk | contribs) (Markup, spelling and punctuation changes.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

What This is About[edit]

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 company logo and some artwork next to it, with some dynamic content on top of the bottom right corner of the background image.

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. What you need is a quick-fix.

At this point your HTML structure might look like:

<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 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[edit]

First, you can't just copy the #header-content division, position it over the logo, make the content invisible and enclose it in an anchor tag. That would be broken HTML. 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 in 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 in 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; /* necessary 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; /* necessary 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;
}