Creating clickable background images using CSS: Difference between revisions
From Joomla! Documentation
m added Category:Template Development using HotCat |
Markup, spelling and punctuation changes. |
||
| Line 1: | Line 1: | ||
{{version/tutor|2.5,3.1}} | {{version/tutor|2.5,3.1}} | ||
== What | == What This is About == | ||
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 | At this point your HTML structure might look like: | ||
<pre> | <pre> | ||
<div id="site"> | <div id="site"> | ||
| Line 33: | Line 33: | ||
</pre> | </pre> | ||
Note: Legitimate uses for the following technique might include | 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 | == How To Do It == | ||
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). | |||
You can however enclose a stretched, one | |||
Example 1 (pre CSS 2.1) HTML: | Example 1 (pre CSS 2.1) HTML: | ||
| Line 56: | Line 55: | ||
<pre> | <pre> | ||
#full-width-header { | #full-width-header { | ||
position: relative; /* | position: relative; /* necessary to | ||
absolute-position the child-element | absolute-position the child-element | ||
#home-link relative to the header */ | #home-link relative to the header */ | ||
| Line 80: | Line 79: | ||
} | } | ||
</pre> | </pre> | ||
Example 2 (CSS 2.1) HTML: | Example 2 (CSS 2.1) HTML: | ||
| Line 97: | Line 95: | ||
<pre> | <pre> | ||
#full-width-header { | #full-width-header { | ||
position: relative; /* | position: relative; /* necessary to | ||
absolute-position the child-element | absolute-position the child-element | ||
#home-link relative to the header */ | #home-link relative to the header */ | ||
Latest revision as of 16:20, 24 October 2022
What This is About
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
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;
}