School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,032 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,217 people online right now. Registration is fast and FREE... Join Now!




Weird div alignment?

 

Weird div alignment?, HTML/CSS/JS

AUAN

30 Oct, 2009 - 04:57 AM
Post #1

D.I.C Head
**

Joined: 30 Jul, 2009
Posts: 65


My Contributions
Heya people!

Just a small problem here.

I am trying to align some text in a div so that there is a main part to it, and it has a sub part attached to it within another div which is attached to a JS action that fades it in and out when a "X" is pressed.

The JS and everything works fine, but when I put the sub part within the div it aligns it a line down, not next to the main part. I would just like to know why it is doing this, as I see no line break tags around.

html
CODE

<body>
    <div id="container">
        
        <div id="cross">
            <a href="#" onclick="fade('fadeBlock');" >X</a>
            
        </div>
        
    <div id="left">
    
    <div class="line1">
       Helvetica <div id="fadeBlock" class="size1"> at 60pt</div>
    </div>
    

    </div>    
    </div>


    </body>


css
CODE

*
{
margin:0;
padding:0;
background-color:#000000;
}

a
{
text-decoration:none;
color:#ffffff;
font-family: Helvetica, Arial, sans serif;
font-size: 30px;
}

#container
{
color:#ffffff;
font-family: Helvetica, Arial, sans serif;
font-weight:900;

}

#cross
{
text-align:right;
padding-right:10px;

}

#left
{
padding-left:50px;

}

.line1
{
font-size:60pt;
width:700px;
}

.size1
{
width:700px;

}


javascript(if needed)
CODE

var TimeToFade = 1000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
  
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
    
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}



function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var element = document.getElementById(eid);

  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }

  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}


Any help is appreciated!

Cheers.

This post has been edited by AUAN: 30 Oct, 2009 - 04:58 AM

User is offlineProfile CardPM
+Quote Post


thehat

RE: Weird Div Alignment?

30 Oct, 2009 - 05:10 AM
Post #2

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
A div tag will automatically move onto the next line. This is because it's default display mode is 'block'. There are two things you could try:

1. Make your div a span instead. A span is an inline element.
2. Set your div's display mode to inline with css. div#fadeBlock { display:inline; }
User is offlineProfile CardPM
+Quote Post

AUAN

RE: Weird Div Alignment?

30 Oct, 2009 - 02:57 PM
Post #3

D.I.C Head
**

Joined: 30 Jul, 2009
Posts: 65


My Contributions
Well the inline style helped bring up the div, but now the JS isn't wanting to work.

html
CODE

<body>
    <div id="container">
        
        <div id="cross">
            <a href="#" onclick="fade('fadeBlock');" >X</a>
            
        </div>
        
    <div id="left">
    
    <div class="line1">
       Helvetica <div id="fadeBlock" class="size1" >at 60pt</div>
    </div>
    </div>    
    
    </div>


    </body>


css
CODE


*
{
margin:0;
padding:0;
background-color:#000000;
}

a
{
text-decoration:none;
color:#ffffff;
font-family: Helvetica, Arial, sans serif;
font-size: 30px;
}

#container
{
color:#ffffff;
font-family: Helvetica, Arial, sans serif;
font-weight:900;

}

#cross
{
text-align:right;
padding-right:10px;

}

#left
{
padding-left:50px;

}

.line1
{
font-size:60pt;
width:700px;
}

div#fadeBlock
{
display:inline;
}

.size1
{
width:500px;
height:100px;
}

User is offlineProfile CardPM
+Quote Post

thehat

RE: Weird Div Alignment?

30 Oct, 2009 - 03:25 PM
Post #4

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
Rather than the inline style try making the div a span, which is inline element to start with. The following works for me in Firefox. I can't test it in IE though, currently on a mac!
CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
*
{
margin:0;
padding:0;
background-color:#000000;
}

a
{
text-decoration:none;
color:#ffffff;
font-family: Helvetica, Arial, sans serif;
font-size: 30px;
}

#container
{
color:#ffffff;
font-family: Helvetica, Arial, sans serif;
font-weight:900;

}

#cross
{
text-align:right;
padding-right:10px;

}

#left
{
padding-left:50px;

}

.line1
{
font-size:60pt;
width:700px;
}

.size1
{
width:700px;

}

</style>
<script type="text/javascript">
var TimeToFade = 1000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
  
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
  
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}



function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;

  var element = document.getElementById(eid);

  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }

  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

</script>
</head>

<body>
    <div id="container">
      
        <div id="cross">
            <a href="#" onclick="fade('fadeBlock');" >X</a>
          
        </div>
      
    <div id="left">
  
    <div class="line1">
       Helvetica <span id="fadeBlock" class="size1"> at 60pt</span>
    </div>
  

    </div>    
    </div>


    </body>

</html>

User is offlineProfile CardPM
+Quote Post

AUAN

RE: Weird Div Alignment?

30 Oct, 2009 - 07:43 PM
Post #5

D.I.C Head
**

Joined: 30 Jul, 2009
Posts: 65


My Contributions
Thanks alot friend! I'll try some IE hacks to see how it goes.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 08:30AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month