tags:

views:

92

answers:

2

In the HTML below, the layout works when the actionBar class has a float (left or right only), however, if I remove it, the enclosing div stretches across the whole page and messes up the layout. I'd really like this "structure" to be centered on my page. Currently with the "float" specified in the actionBar class, it's either left aligned or right aligned. Can someone explain to me why it does that (the "gets-all-messed-up-without-the-float" part)?

Of course, being a relative HTML rookie, if you have some other brilliant way of achieving the same layout, that's always welcome. For now, I'm just looking for an explanation so I can try and find a solution.

Note: The ugly greens and blues and reds are just for illustration. The real HTML uses images to give a rounded corner type effect to what is essentially a collection of buttons.

Thanks!

<html>
<head>
<style>
.actionButtonLeft {
  padding: 0; margin: 0; margin-right: 1;
  float : left;
  width : 8px; height : 26px;
  background-color: green;
}
.actionButton {
  padding: 0; margin: 0; margin-right: 1;
  background-color : blue; color : #ffffff ;     
  height : 26px;
  float : left; 
}
.actionButtonRight {
  padding: 0; margin: 0;
  float : left;
  width : 8px; height : 26px;
  background-color: green;
}
.actionBar {
  float:left; /* or float: right; -- doesn't work with no float */
  background-color:#112554;
  border: 2px solid red;
}
</style>
</head>
<body>
<div class="actionBar">
<div class="actionButtonLeft" ></div>
<input type="button" value="OK" name="OK" id="okButton" class="actionButton"
       style="WIDTH:100px; " onclick="doSomething();">
<input type="button" value="Cancel" name="Cancel" id="cancelButton" 
       class="actionButton"
       style="WIDTH: 100px" onclick="cancelThis();">
<input type="button" value="Help" name="Help" id="helpButton" 
       class="actionButton"
       style="WIDTH: 100px" onclick="helpMe();">
<div class="actionButtonRight"></div>
<div style="width:0px;clear:right;"></div></div>
</body>
</html>
+2  A: 

That's because floated elements no longer stretch their containing elements. Since the elements inside the actionBar div are floated, the actionBar div without the float property no longer has anything defining its height. Divs by default take up the entire available width, which in your case causes the wide, thin box that the other elements stick out of. Adding the float property causes the actionBar div to stretch to contain its contents both horizontally and vertically.

Here are a couple articles that explain the float property, and how it works:

GApple
+1  A: 

Removing the float and adding a width on the actionBar style class should do the trick - looks like a width of about 327px (which may change when you remove the borders).

Matt Hamsmith
GApple gives an actual answer to your question (+1 for him/her). My "answer" is more directed towards "so I can try and find a solution".
Matt Hamsmith
Yes, I did notice after I posted that adding a width of about 327 (with borders) kinda works. Of course, I can't do that because at some point I might have 2 or 4 buttons in there. So now I'm looking for a way to make the parent div shrink-wrap its children without using a fixed width or float. I tried min-width but that didn't seem to do it.
Adnan