PDA

Click to See Complete Forum and Search --> : Text over Image - CSS problem


kevincdg
06-26-2007, 12:37 AM
http://www.thebrotherhoodofveteranwarriors.org/founders.html

I'm using CSS to place the text over the images. For some reason, in both IE 6 (wow) and IE 7 everything works fine.

In firefox for some reason the navigation doesn't work on the left side -- but the text links back correctly.

I can't figure out why, argh!@

Is it because they're javascript swaps, or what?

Any help is appreciated!

hewligan
06-26-2007, 01:01 AM
Well, you ve got a bunch of incorrectly nested tags in there. I don't know if that's causing the problem, but it certainly wouldn't be helping.

For example,

<b><a href="#">[more]</b></a>


Should be:

<b><a href="#">[more]</a></b>


(i.e. You should always close an inner tag before closing the outer one.)

kevincdg
06-26-2007, 01:17 AM
fixed that, i was stressed and ****ed that up -- my bad.

still didn't fix though. i found what it is, but i don't know how to fix it.

it's in the css of the text overlay -- it's not the background (url:) but the actual text. the width and position isn't set over the link divs, so i have no idea why it's happening.

resdog
06-26-2007, 05:01 PM
You code is pretty messed up, which is why it's causing you the problems.

First, you have incorrectly nested table tags (and not correctly marked up).
All tables must have a <table>, <tr> and <td>. Yours just has <table><td>. your missing the <tr> Second, the table SHOULD end after the image swap-outs (here):

<div id="photo"><a href="photogallery.html"><img src="http://www.thebrotherhoodofveteranwarriors.org/images/photo.jpg" border="0" id="Image5" onmouseover="MM_swapImage('Image5','','http://www.thebrotherhoodofveteranwarriors.org/images/photo_over.jpg',1)" onmouseout="MM_swapImgRestore()" /></a></div>
</div>
</td></table>

You ended it at the end of all the divs, save the "container". This throws off the rendering of the page.

Second, you have a lot of ancillary css. I updated the html & CSS (commenting out the unnecessary code), so you can take a look at it and see if that fits your needs. It rendered fine for me on Firefox (mac).

HTML:

<body onload="MM_preloadImages('images/history_over.jpg','images/founding_over.jpg','images/events_over.jpg','images/core_over.jpg','images/photo_over.jpg')">
<div id="container">
<table width="1000" height="1800" bgcolor="#FFFFFF">
<tr><td>

<div id="left"><a href="index.html"><img src="images/header.jpg" border="0" /></a>
<div id="history"><a href="history.html"><img src="images/history.jpg" border="0" id="Image1" onmouseover="MM_swapImage('Image1','','images/history_over.jpg',1)" onmouseout="MM_swapImgRestore()" /></a></div>
<div id="founding"><a href="founders.html"><img src="images/founding_over.jpg" border="0" id="Image2" onmouseover="MM_swapImage('Image2','','images/founding_over.jpg',1)" onmouseout="MM_swapImgRestore()" /></a></div>
<div id="events"><a href="events.html"><img src="images/events.jpg" border="0" id="Image3" onmouseover="MM_swapImage('Image3','','images/events_over.jpg',1)" onmouseout="MM_swapImgRestore()" /></a></div>
<div id="core"><a href="principles.html"><img src="images/core.jpg" border="0" id="Image4" onmouseover="MM_swapImage('Image4','','images/core_over.jpg',1)" onmouseout="MM_swapImgRestore()" /></a></div>
<div id="photo"><a href="photogallery.html"><img src="images/photo.jpg" border="0" id="Image5" onmouseover="MM_swapImage('Image5','','images/photo_over.jpg',1)" onmouseout="MM_swapImgRestore()" /></a></div>
</div>
</td></tr></table>
</div>
<div id="bios">
<div id="contheaders"><img src="images/headerfounders.jpg" /> </div>
<div id="line"><img src="images/line.jpg" /></div>


<div style="background: url(images/aliduke.jpg); width: 569px; height: 147px; overflow:hidden;">
<div style="position: absolute; bottom: 10px; left: 147px; width: 400px; font-weight: normal; color: #444;"> <p>Born in Cleveland, Ohio, September 29, 1946. Currently residing in Prairie View, Texas. Currently single, father to 7 children, Grandfather to 8 Grandchildren. <br /><br /> Retired US Air Force Master Sergeant with a background in Military Intelligence and the
Air Force Equal Opportunity Program (First Command Equal Opportunity & Treatment NCO... <b><a href="#">[more]</a></b></p>
</div>
</div>

</div>

</body>
</html>


CSS:

/****************
Global Styles
****************/
*
{
margin:0;
padding:0;
}


body, html
{
background-image: url(images/bg.jpg);
}

h1
{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:13px;
}

h2
{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
font-weight:bold;
}
h3
{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:9px;
font-weight:normal;
}

p
{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}

a:link {text-decoration:none; color:#999999;}
a:hover { text-decoration:underline; color:#111111;}
a:visited {text-decoration: none; color:#333333;}
a:active {text-decoration:none; color:#999999;}

#navbar
{
position:static;
}

#container
{
position:absolute;
top:50%;
left:50%;
width:1013px;
height:539px;
margin-top:-269px;
margin-left:-506px;
}

#left
{
position:absolute;
left:50px;
top:40px;
}


/****************************
Index Page
*****************************/

#logo
{
position:absolute;
left:350px;
top:50px;
}

/****************************
Page Headers
*****************************/

/*#contheaders
{
position:absolute;
left:400px;
top:50px;
}

#line
{
position:absolute;
left:400px;
top:180px;

}*/

/***************************
Content
***************************/

#content
{
position:absolute;
left:400px;
top:190px;
width:450px;
}

/***************************
Founders Content
***************************/
#bios
{
position:absolute;
top:283px;
left:360px;
clear:left;
}




/*#alitext
{
position:absolute;
left:400px;
top:183px;
}

#aliduke
{
position:absolute;
left:400px;
top:183px;

}*/

#line1
{
position:relative;
left:398px;
top:-13px;
height:2px;
}

#botsford
{
position:absolute;
left:400px;
top:333px;
}

kevincdg
06-26-2007, 05:22 PM
thank you very much -- as for some of the extra css, it's because i had more content originally there. more bio sections, but i took them down to show that with only one, the links worked fine.

thank you for the code clean-up. i'll check it when i get to my second in-house job and see how it goes. :)

i really need to stop writing this stuff when i'm so tired, it gets really sloppy.

resdog
06-26-2007, 06:01 PM
I figured you had it there for a reason, but when I went in to tinker with it, I commented it out, just so I wouldn't get confused. Let us know if it works good for you!

tZ
06-26-2007, 08:14 PM
I still can't understand why you are using divs to set up a navigation list.

Normally, you want to do something like this:



<ul id="navigation">
<li><a href="www.something.com">something</a></li>
<li><a href="www.hello.com">hello</a></li>
<li><a href="www.dot.com">dot</a></li>
</ul>



Then all you need to do to add a rollover effect get the ul navigation node using:


var nav = document.getElementById('navigation");



var list = nav.getElementsByTagName("li");


From there you loop through the entire array for all the children(which will be the list elements). Then addevent listeners with handlers and your done. Instead of placing the javascript in the markup and creating an unsemantic result.

tZ
06-26-2007, 09:06 PM
This is a full explaination of how to make your life simpler and easier using javascript to make rollovers. It may look like alot but, this will seperate the content from the behavior and make complex tasks much more easier.

This is a function that handles browsers differences when handling events. Just make sure to include it in the javascript doc or externally link it. I'm not going to explain but, basically stated different browsers call something that does pretty much the exact same thing as another by a different name with different parameters. So this function takes care of that cross browser difference essentially. So all you need to do is call this function to add a listener.

elm - stands for the node
evType - stands for the event type
fn - is the function to handle the event when it occurs
- useCapse isn't all that important at this point so I'm not going to explain it.


function addEvent(elm,evType,fn,useCapture) {
if(elm.addEventListener) {
elm.addEventListener(evType,fn,useCapture);
return true;
} else if(elm.attachEvent) {
var r = elm.attachEvent('on'+evType,fn);
return r;
} else {
elm['on'+evType]=fn;
}


The rest of code is used to register the listeners with the appropriate node and assign a handler to the each listener. A handler will handle the event when the node detects the event which it is listening for. Since, what happens is that the object- in this case the img will detect a mouseover or mouseout event- then the listener will send that event handler. The event itself is a seperate object tht contains info on what object/node fired the event and other more usefull things that I won't get into. So each handling function(out & over) have to first find the browser then use the appropriate attribute to find what what object fired the event. Since, the event is a seperate object itself. The code between the /**/ is ommited code that explains what each statement does in particular where they are included.


/* function that handles cross-browser differences for events */
function addEvent(elm,evType,fn,useCapture) {
if(elm.addEventListener) {
elm.addEventListener(evType,fn,useCapture);
return true;
} else if(elm.attachEvent) {
var r = elm.attachEvent('on'+evType,fn);
return r;
} else {
elm['on'+evType]=fn;
}

/*function called when the html page is loaded- We can only find the nodes we are looking for if the page is loaded into the browser. otherwise they will not be rendered yet */
window.onload = function() {
load_listeners();
}

/*This function attaches the listeners to the nodes*/
function load_listeners() {
var nav = document.getElementById("navigation"); //get the navigation node
var list = nav.getElementsByTagName("li"); //get array of all children li nodes
for(var i=0; i < list.length; i++) { //loop through list node

list[i].link = list.getElementsByTagName('a')[0]; // find the a node and assign
list[i].images = list[i].link.getElementsByTagName('img')[0]; //find the image
addEvent(list[i].image, 'mouseover',over,false); // attach eventlistener
addEvent(list[i].image, 'mouseout', out,false); // attach eventlistener

}
}
/*function that is called listener to handle the event of mouseover */

function over(e) {
var el;
if(window.event && window.srcElement)
el = window.event.srcElement;
if(e && e.target)
el = e.target;
While(el.nodeName.toLowerCase() != 'img' && el.nodeName.toLowerCase() != 'body')
el = el.parentNode;
if(el.nodeName.toLowerCase() == 'body');
return;

/* very important
The first statement gets the attribute value of alt. The alt value should be named the same thing as the image which you want to swap. For instance, if the image is named home- then you set the alt value of the image to home.*/
var what_over = el.getAttribute('alt');

/*This statment creates a string that is composed of the images location to be swapped*/ First the folder location is put in a string then the what_over variable with the name is added to the string- then the image type is added. The end result is a string like: "images/navigation/rollovers/home,gif". This string is then assigned to the var swap_image.*/

var swap_over = "images/navigation/rollovers/"+ what_over + '.gif';

/*The last part is to swap the image by rerouting the images src to the swap_over image location. You do this by assigning the images(el) src attribute to swap_over- the string with the new images location*/

el.src = swap_over;
}
/* that step ends this handling method for a mouse over*/

/*now we need to handle the mouseout- so we do this in a new function doing the same exact thing but, changing the swap_image to the location of the origional image.*/

function out(e) {
var el;
if(window.event && window.srcElement)
el = window.event.srcElement;
if(e && e.target)
el = e.target;
While(el.nodeName.toLowerCase() != 'img' && el.nodeName.toLowerCase() != 'body')
el = el.parentNode;
if(el.nodeName.toLowerCase() == 'body');
return;
var what_out = el.getAttribute('alt');
var swap_out = "images/navigation/main/"+ what_out +'.gif';
el.src = swap_out;
}



Then you would save this file as something like listener.js and link it externally to the html like highlighted below. I also included the full mark up to show you how this works. Now everything has been seperated and it is much easier to pin point problem areas then intergrating the javascript into the body of the document.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Fugh Animation</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
<script language="javascript" type="text/javascript" src="javascript/listeners.js"></script>
<link rel="stylesheet" type="text/css" href="fugh.css" />
</head>
<body>
<div id="container">
<div id="banner">
<img src="images/banner/banner.gif" />
</div> <!-- banner -->
<div id="navigation">
<ul id="main_navigation">
<li><a href=""><img src="images/main_navigation/links/about.gif" alt="about" width="76" height="20" /></a></li>
<li><a href=""><img src="images/main_navigation/links/production_notes.gif" alt="production_notes" width="205" height="20" /></a></li>
<li><a href=""><img src="images/main_navigation/links/script.gif" alt="script" width="75" height="23" /></a></li>
<li><a href=""><img src="images/main_navigation/links/storyboards.gif" alt="storyboards" width="159" height="26" /></a></li>
<li><a href=""><img src="images/main_navigation/links/art_direction.gif" alt="art_direction" width="153" height="24" /></a></li>
<li><a href=""><img src="images/main_navigation/links/animatic.gif" alt="animatic" width="109" height="21" /></a></li>
<li><a href=""><img src="images/main_navigation/links/contact.gif" alt="contact" width="103" height="18" /></a></li>
</ul>
</div> <!-- navigation -->
<div id="content">
</div> <!-- content -->
</div> <!-- container -->
</body>
</html>


So basically, by using the alt value of the image you can swap the appropriate image without adding a new class or some other useless attribute to the image. It also condenses javascript becasue instead of registering a event listener with every node instead we can just loop through all them and assign them a "general" handler. Then based the one image the alt attribute can be checked for what image is going to be swaped. The only downside(or upside maybe) is that you need to include have a seperate folders for your main images and rollovers. Since, they are named the same thing. However, you could add another part onto the source string as well and store them in the same location- something like:


var swap_over = "images/navigation/"+ what_over+"_rollover" + '.gif';


This way you can store all your images that make up the navigation and its rollover states in the same location, rather then seperate folders and just add _rollover to the main name. Then add it in the string to create the src.

hope that wasn't to much… sometimes i get of on a tangent,lol-sorry.

resdog
06-26-2007, 09:28 PM
Or, you can make a pure CSS-driven rollover, without ANY javascript:

http://www.tutorio.com/tutorial/pure-css-image-rollovers

tZ
06-26-2007, 09:33 PM
^ yeah… thats no fun,lol

Plus, the above is the basic setup for all very comlpex interaction effects in javascript. So if you learn it you will be able to many things that can't be done with CSS. However, if your just looking to do a simple image swap I would probably recommend the CSS way. Cause it is a alot to wrap your head around the first time through…

kevincdg
06-27-2007, 07:01 PM
Your solution worked, resdog. Thank you!

As for all that java, yikes.

You're probably right about the css rollovers, i'll look into that.

And WTF? I don't remember why I used div for navigation either. i'm just picking up on this after a while off, so I'll look into converting that too. :)

Thanks guys.