CSS help with a floating element

Status
Not open for further replies.

LargeSock

Ars Tribunus Militum
2,776
I have an example set up at http://lunelson.com/misc/uitest.html. In IE9, the "show" button is much further down than the label. In Chrome/Firefox, the "show" button is noticeably lower than the label but still sort of on the same line. How can I get it to be vertically aligned with the "Query name" label? Thanks.

Here's the code:
Code:
<html>
<head>
	<style type="text/css">
	.tableName
	{
		background-color: LemonChiffon;
		font-weight: bold;
		padding:0.15cm;
		width:30%;
	}
	.showHideButton
	{
		float: right;
		padding-right:0.1cm;
	}
	</style>
</head>
<body>
	<div id="title0" class="tableName">
		<span>Query name: </span>
		<span>25</span>
		<input type="button" value="Show" class="showHideButton">
	</div>
</body>
</html>
 

LargeSock

Ars Tribunus Militum
2,776
That certainly seemed promising; unfortunately, it didn't quite work out right. I updated the original link. Current code is:

Code:
<html>
<head>
	<style type="text/css">
	.tableName
	{
		background-color: LemonChiffon;
		font-weight: bold;
		padding:0.15cm;
		width:30%;
	}
	.tableLabel
	{
		float: left;
	}
	.showHideButton
	{
		float: right;
		padding-right:0.1cm;
	}
	</style>
</head>
<body>
	<div id="title0" class="tableName">
		<span class="tableLabel">Query name: 25</span>&nbsp;
		<input type="button" value="Show" class="showHideButton">
	</div>
</body>
</html>

Without the &nbsp; the background color does not cover the span and input elements. But unfortunately, with or without it, the input element is still offset lower than the span.
 

wallinbl

Ars Legatus Legionis
14,332
Subscriptor
Code:
<!-- saved from url=(0036)http://lunelson.com/misc/uitest.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<style type="text/css">
	.tableName
	{
		background-color: LemonChiffon;
		font-weight: bold;
		padding:0.15cm;
		width:30%;
		border: solid 1px;
		float: left;
	}
	</style>
</head>
<body>
	<div id="title0" class="tableName">
		<div style="float: left;">Query name: 25</div>
		<div style="float: right;">
			<input type="button" value="Show">
		</div>
	</div>

</body></html>
I hate CSS. That's not saying it's bad, it's just the next to last thing I want to be dealing with (right above JavaScript in a browser).
 

LargeSock

Ars Tribunus Militum
2,776
I keep feeling like I'm getting closer, but this is brutal. wallinbl, your code makes IE look a lot better (pretty much like FF/Chrome), but the Show button is still not aligned with the label. I'll just give up and fake it; even though I hate doing stuff like this, I think specifying units in "em" makes it scale properly with browser zoom, etc., right? I assigned a margin-top of -0.15em to the show button and it's finally looking right.

http://lunelson.com/misc/uitest.html

EDIT: Never mind, that margin-top broke IE somehow. Back to the drawing board.

Also, as for the suggestion to get IE9 out of quirks mode, that would be awesome; unfortunately, this is for an Opensocial widget and I can't change that part of the code where this is being displayed.
 

LargeSock

Ars Tribunus Militum
2,776
Code:
<html>
<body>	
	<div style="padding:0.5em;height:1.5em;width:7em;background-color:#aff;">
		<div style="float:right;border:1px solid black;"><input type="button" value="button"></div>
		<div style="float:left;border:1px solid black;">div 1</div>
	</div>
</body>
</html>
So now that I've simplified things, I see what the browsers are doing. They are aligning the top of the div containing the "div 1" text and the top of the div containing the button. The problem is that the button is taller than the text. So is there any way to align these two elements along their centers instead of along their tops?

uitest.png
 

LargeSock

Ars Tribunus Militum
2,776
Ugly solution, but I'll take it. Turns out most of the offset was due to an implied margin on the button. I removed that, and then just had to shift the label down slightly. I also had to do a smaller padding on the bottom of the larger div so that elements are still centered within it.

Code:
<html>
<body>	
	<div style="padding-bottom:6px;padding-top:8px;padding-left:8px;padding-right:8px;height:1.5em;width:7em;background-color:#aff;">
		<div style="float:right;"><input type="button" value="button" style="margin:0px;"></div>
		<div style="float:left;margin-top:2px;">div 1</div>
	</div>
</body>
</html>
 

mormy

Seniorius Lurkius
38
npHardProblem":tovrln1u said:
As Inscitia said, you should include a doctype. Also consider using a CSS reset stylesheet to remove the default browser styles.
what he/she said.. include Paul Irish' HTML5 reset... set your doctype to html... and, do it like this

Code:
<div style="WHATEVER YOU WANT">
                <div style="float:left;display:inline;">div 1</div>
		<div style="float:left;display:inline;"><input type="button" value="button" style="margin:0px;"></div>
	</div>
</div>

if you need to change the elements position, go right ahead... This should hold... No margins, just float and display :)
 
Status
Not open for further replies.
\n\t