Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with element alignment

Status
Not open for further replies.

jemminger

Programmer
Jun 25, 2001
3,453
US
hi all,

i've built this widget to simulate the look of a <select size=&quot;1&quot; multiple>, but without the multiple capability. reason being i wanted a <select> list with up & down arrows, but of size 1.

the problem is i can't get the buttons to be stacked on top of each other...i've tried setting their container <span> to a width of 0px or 1px, but no luck.

there's two versions: one with <input type=&quot;button&quot;> for the buttons, another with <span> for the buttons...both with their own alignment problem.

any ideas on how i can solve this? i've been trying everything i know!

here's version 1:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 FINAL//EN&quot;>
<html>
<head>
<title>NumberWidget</title>
<meta name=&quot;author&quot; content=&quot;?&quot;>
<meta name=&quot;keywords&quot; content=&quot;?&quot;>
<meta name=&quot;description&quot; content=&quot;?&quot;>

<script language=&quot;javascript&quot;>
function NumberWidget(name, min, max, value) {
	min = min?min:0;
	max = max?max:100;
	value = (!isNaN(value)?value:&quot;&quot;);
	
	//  store the object's props
	this.name = name;
	this.min = min;
	this.max = max;
	this.html = '<span>' +
		'<span>' +
			'<input type=&quot;text&quot; name=&quot;'+name+'&quot; value=&quot;'+value+'&quot; class=&quot;nw_text&quot; size=&quot;'+max.toString().length+'&quot;/>' +
		'</span>' +
		'<span>' +
			'<input type=&quot;button&quot; class=&quot;nw_btn&quot; value=&quot;5&quot; onclick=&quot;nwIncr(\''+name+'\');&quot;/>' +
			'<input type=&quot;button&quot; class=&quot;nw_btn&quot; value=&quot;6&quot; onclick=&quot;nwDecr(\''+name+'\');&quot;/>' +
		'</span>' +
		'</span>';
	return this;
}

function writeNW(name) {
	NW = getNW(name);
	document.writeln(NW.html);
}

function getNW(name) {
	for (x = 0; x < arNW.length; x++) {
		if (arNW[x].name == name)
			return arNW[x];
	}
}

function getFormEl(name) {
	var e = document.forms[0].elements;
	for (x = 0; x < e.length; x++) {
		if (e[x].name && e[x].name.toLowerCase() == name.toLowerCase())
			return e[x];
	}
}

function nwIncr(name) {
	var el = getFormEl(name);
	NW = getNW(name);
	if (el.value < NW.max) el.value = ++el.value;
}

function nwDecr(name) {
	var el = getFormEl(name);
	NW = getNW(name);
	if (el.value > NW.min) el.value = --el.value;
}

var arNW = [
	new NumberWidget(&quot;hh&quot;,1,12,1),
	new NumberWidget(&quot;mm&quot;,0,59,0),
	new NumberWidget(&quot;ss&quot;,0,59,0)
];

</script>

<style type=&quot;text/css&quot;>
.nw_text {
	border:1px solid threedface;
	margin:0;
}

.nw_btn {
	width:15px;
	height:10px;
	border:1px outset threedface;
	background-color:threedface;
	font:normal 8px webdings;
	line-height:6px;
	text-align:center;
	cursor:default;
}
</style>
</head>

<body>

<form action=&quot;&quot; method=&quot;post&quot;>

<script language=&quot;javascript&quot;>writeNW(&quot;hh&quot;);
document.write(&quot;:&quot;);
writeNW(&quot;mm&quot;);
document.write(&quot;:&quot;);
writeNW(&quot;ss&quot;);</script>
</form>

</body>
</html>

for version two, simply replace function NumberWidget() with this:
Code:
function NumberWidget(name, min, max, value) {
	min = min?min:0;
	max = max?max:100;
	value = (!isNaN(value)?value:&quot;&quot;);
	
	//  store the object's props
	this.name = name;
	this.min = min;
	this.max = max;
	this.html = '<span>' +
		'<span>' +
			'<input type=&quot;text&quot; name=&quot;'+name+'&quot; value=&quot;'+value+'&quot; class=&quot;nw_text&quot; size=&quot;'+max.toString().length+'&quot;/>' +
		'</span>' +
		'<span>' +
			'<span class=&quot;nw_btn&quot; style=&quot;clear:right;&quot; onclick=&quot;nwIncr(\''+name+'\');&quot;>5</span>' +
			'<span class=&quot;nw_btn&quot; onclick=&quot;nwDecr(\''+name+'\');&quot;>6</span>' +
		'</span>' +
		'</span>';
	return this;
}
=========================================================
if (!succeed) try();
-jeff
 
have u tried using <table> for alignment? -There are only 10 types of people in the world, those who understand binary and those who don't-

-pete
 
yeah, the problem with <table> is that it forces a line break between tables, and you end up with:

[widget]
:
[widget]
:
[widget]

instead of [widget]:[widget]:[widget] =========================================================
if (!succeed) try();
-jeff
 
Maybe I don't understand.

Here's a CSS:

Code:
body            {
                  background-color:#ffffff;
                  font-family:&quot;arial narrow&quot;, san-serif;
                }
p               {
                  font-family:&quot;arial narrow&quot;, san-serif;
                }
input.ButtonBoy {
                  width: 130px;
                }
table.Squeezy   {
                  width: 80%;
                  background-color: transparent;
                  border-width: 1px;
                  border-style: solid;
                  border-color: #000000;
                  text-align: left;
                  margin: 0px;
                  cellpadding: 0px;
                  cellspacing: 0px;
                }

and here's an HTML file that uses it:

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;DTD/xhtml1-transitional.dtd&quot;>
<html>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;></meta>
    <title>Sample Table Squeezer</title>
    <link rel=&quot;stylesheet&quot; href=&quot;Sample.css&quot; type=&quot;text/css&quot;></link>
  </head>
  <body>
    <p>A little bit of text...</p>
    <input type=&quot;button&quot; value=&quot;Red&quot;    class=&quot;ButtonBoy&quot;></button><br />
    <input type=&quot;button&quot; value=&quot;Orange&quot; class=&quot;ButtonBoy&quot;></button><br />
    <input type=&quot;button&quot; value=&quot;Yellow&quot; class=&quot;ButtonBoy&quot;></button><br />
    <input type=&quot;button&quot; value=&quot;Green&quot;  class=&quot;ButtonBoy&quot;></button><br />
    <input type=&quot;button&quot; value=&quot;Blue&quot;   class=&quot;ButtonBoy&quot;></button><br />
    <input type=&quot;button&quot; value=&quot;Indigo&quot; class=&quot;ButtonBoy&quot;></button><br />
    <input type=&quot;button&quot; value=&quot;Violet&quot; class=&quot;ButtonBoy&quot;></button>
    <p>And a little more text.</p>
    <p>and now a series of tables</p>
    <table class=&quot;Squeezy&quot;>
      <tr><td><input type=&quot;button&quot; value=&quot;Red&quot;    class=&quot;ButtonBoy&quot;></button></td></tr>
    </table>
    <table class=&quot;Squeezy&quot;>
      <tr><td><input type=&quot;button&quot; value=&quot;Orange&quot; class=&quot;ButtonBoy&quot;></button></td></tr>
    </table>
    <table class=&quot;Squeezy&quot;>
      <tr><td><input type=&quot;button&quot; value=&quot;Yellow&quot; class=&quot;ButtonBoy&quot;></button></td></tr>
    </table>
  </body>
</html>

Now, when you run it as-is, you see a stack of close buttons -- no space between them. Below that, is a stack of tables, each containing one button. The black lines represent the table border. I've set the style to have no space before or after a table of the style &quot;Squeezy&quot;. If you change the CSS line
Code:
border-width: 1px;
to
Code:
border-width: 0px;
the borders will go away and you'll see tables snuggled up closer enough to warrant marriage. Yes, there is some space there between the buttons, but that space is clearly in the <tr> or <td> tags and not between the tables.

Does that help at all, or did I just totally misunderstand the question?

Cheers,

Edward &quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
yeah like Edward says u put the elements in columns of a single row table not in a different table for each element.

does that help? -There are only 10 types of people in the world, those who understand binary and those who don't-

-pete
 
Actually, Pete, I did put the buttons each in their own 1-row, 1-cell table. That was the second example, showing how tables can be adjacent to each other, as long as the margins are set to 0px in the stylesheet.

The first example was simply stacking them together using <br /> tags, which does the vertical squishing right nicely.

If space is a premium, I think there might be a CSS call to modify button height, but I haven't needed it, so I haven't looked.

Cheers,

Edward &quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Thanks for your replies.

Edward, in your example the tables are NOT next to each other - they are each on their own line. I'm using IE6SP1, NS6.2 and Moz 1.0.

View my example above in your browser and you'll see the problem...also if I put a <br/> between the two buttons, it REALLY ruins the alignment.

palbano,
the reason they would be in their own table (or other container) is because they are individual widgets - they are intended to be standalone objects like <select> or <input>

=========================================================
if (!succeed) try();
-jeff
 
There's absolutly no reason that I can see why tables won't do it, each in their own cell, cell cellpadding and cellspaceing and margins of the buttons all set to zero.

Anyway, this was the best I could get with css (in IE6)

this.html = '<span>' +
'<span>' +
'<input type=&quot;text&quot; name=&quot;'+name+'&quot; value=&quot;'+value+'&quot; class=&quot;nw_text&quot; size=&quot;'+max.toString().length+'&quot;/>' +
'</span>' +
'<span style=&quot;position: relative;&quot;>' +
'<input type=&quot;button&quot; class=&quot;nw_btn&quot; value=&quot;5&quot; style=&quot;position: absolute; top:0;left:0;&quot; onclick=&quot;nwIncr(\''+name+'\');&quot;/>' +
'<input type=&quot;button&quot; class=&quot;nw_btn&quot; value=&quot;6&quot; onclick=&quot;nwDecr(\''+name+'\');&quot;/>' +
'</span>' +
'</span>';


É ::
 
cian,

thanks - that seems to do it.

tables, maybe if you can get these tables to be next to each other?
<table><tr><td>one</td></tr></table>
<table><tr><td>two</td></tr></table>
<table><tr><td>three</td></tr></table> =========================================================
if (!succeed) try();
-jeff
 
<style type=&quot;text/css&quot;>
<!--
.dog {display: inline;}
-->
</style>


<table summary=&quot;&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;dog&quot;><tr><td>one</td></tr></table>
<table summary=&quot;&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;dog&quot;><tr><td>two</td></tr></table>
<table summary=&quot;&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;dog&quot;><tr><td>three</td></tr></table>



É ::
 
I'm a flippin dumbass, always look for the easy solution:

<table summary=&quot;&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<tr>
<td>one</td><td>two</td><td>three</td>
</tr>
</table>


É ::
 
that's the problem though - the NumberWidget is a self-contained element. there may be one or more per page - if they are constructed with a <table> as their container, they will force themselves to their own line.
=========================================================
if (!succeed) try();
-jeff
 
Ooooh, the &quot;up&quot;/&quot;down&quot; buttons. You want them on top of each other?

Edward &quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Hmmm... tricky. I see what you're talking about now.

Here.

Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 FINAL//EN&quot;>
<html>
<head>
<title>NumberWidget</title>
<meta name=&quot;author&quot; content=&quot;?&quot;>
<meta name=&quot;keywords&quot; content=&quot;?&quot;>
<meta name=&quot;description&quot; content=&quot;?&quot;>

<script language=&quot;javascript&quot;>
function NumberWidget(name, min, max, value) {
    min = min?min:0;
    max = max?max:100;
    value = (!isNaN(value)?value:&quot;&quot;);
    
    //  store the object's props
    this.name = name;
    this.min = min;
    this.max = max;
    this.html = '<table>' +
        '<tr>' +
            '<td><input type=&quot;text&quot; name=&quot;'+name+'&quot; value=&quot;'+value+'&quot; class=&quot;nw_text&quot; size=&quot;'+max.toString().length+'&quot;/></input></td>' +
            '<td><input type=&quot;button&quot; class=&quot;nw_btn&quot; value=&quot;5&quot; onclick=&quot;nwIncr(\''+name+'\');&quot;/></input><br /><input type=&quot;button&quot; class=&quot;nw_btn&quot; value=&quot;6&quot; onclick=&quot;nwDecr(\''+name+'\');&quot;/></td>' +
        '</tr>' +
        '</table>';
    return this;
}

function writeNW(name) {
    NW = getNW(name);
    document.writeln(NW.html);
}

function getNW(name) {
    for (x = 0; x < arNW.length; x++) {
        if (arNW[x].name == name)
            return arNW[x];
    }
}

function getFormEl(name) {
    var e = document.forms[0].elements;
    for (x = 0; x < e.length; x++) {
        if (e[x].name && e[x].name.toLowerCase() == name.toLowerCase())
            return e[x];
    }
}

function nwIncr(name) {
    var el = getFormEl(name);
    NW = getNW(name);
    if (el.value < NW.max) el.value = ++el.value;
}

function nwDecr(name) {
    var el = getFormEl(name);
    NW = getNW(name);
    if (el.value > NW.min) el.value = --el.value;
}

var arNW = [
    new NumberWidget(&quot;hh&quot;,1,12,1),
    new NumberWidget(&quot;mm&quot;,0,59,0),
    new NumberWidget(&quot;ss&quot;,0,59,0)
];

</script>

<style type=&quot;text/css&quot;>
.nw_text {
    border:1px solid threedface;
    margin:0;
}

.nw_btn {
    width:15px;
    height:10px;
    border:1px outset threedface;
    background-color:threedface;
    font:normal 8px webdings;
    line-height:6px;
    text-align:center;
    cursor:default;
}
</style>
</head>

<body>

<form action=&quot;&quot; method=&quot;post&quot;>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
  <tr valign=&quot;top&quot;>
    <td valign=&quot;top&quot;><script language=&quot;javascript&quot;>writeNW(&quot;hh&quot;);</script></td><td valign=&quot;top&quot;>:</td><td valign=&quot;top&quot;><script language=&quot;javascript&quot;>writeNW(&quot;mm&quot;);</script></td><td valign=&quot;top&quot;>:</td><td valign=&quot;top&quot;><script language=&quot;javascript&quot;>writeNW(&quot;ss&quot;);</script></td>
  <tr>
</table>
</form>

</body>
</html>

Have fun!

Edward &quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
thanks Edward and cian for your help - i ended up using cian's suggestion of using css positioning in case the document isn't surrounded by a table.

working version 1.0 is here:

i also added support for holding down on the buttons triggering a rapid increment/decrement =========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top