2

I'm trying to build a button bar component where there can be a variable number of buttons. I can do anything I need to wrap these buttons, etc, but I need to be able to size the buttons based on percent width inside a div. Since I'm porting over flash to html5, I need to be able to specify this percent width as well as place a 1-10 pixel gap between the buttons, depending on the implementation. The buttons themselves are expected to have things like borders, etc, all the usual properties and the buttons will need to be absolutely positioned due to the way the legacy component engine works.

Is there any way through some combination of html / css to create a % width and reduce the size of each button by pixels within that % width? I realize this could easily be solved using explicit values and jQuery, but for many complicated reasons relating to dynamic resizing / tweening I would strongly prefer to let css handle the positioning.

I started a fiddle below:

http://jsfiddle.net/maKuG/

Ry-
  • 218,210
  • 55
  • 464
  • 476
Shane
  • 4,921
  • 5
  • 37
  • 53

3 Answers3

0

It's a rather dirty solution, but I usually just use absolute positioning in these cases. Example:

#elmt1 {
    position: absolute;
    left: 0;
    width: 300px;
}
#elmt2 {
    position: absolute;
    left: 300px;
    right: 50%; /* Half way on the page */
}
#elmt3 {
    position: absolute;
    left: 50%;
    right: 0;
}
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • Apparently I responded before the entire answer was posted, I'll play around with that some, thanks for the direction. – Shane Nov 26 '11 at 22:39
  • Yeah, sorry 'bout that, I accidentally pushed tab before placing one of the many spaces we need here on SO :-) – Tom van der Woerdt Nov 26 '11 at 22:41
  • No worries. As an aside, I'm tinkering with these things in real-time here: http:webezerk.com I built this thing about 6 years ago or so and it only works in ff, never really made it "public facing", but it comes in handy sometimes. – Shane Nov 26 '11 at 22:43
  • Not sure I follow the logic on closer inspection. Am I missing something? http://jsfiddle.net/maKuG/1/ – Shane Nov 26 '11 at 22:56
  • Yes - you have to do all the positioning manually. You have to specify either the `left:` or `right:`, AND the `width:` (or the other left/right). You have to define to X-coordinates in order for this to work. Relying on the previous element for the starting X coordinate does not work well when using absolute positions. – Tom van der Woerdt Nov 26 '11 at 23:07
0

From my experience % definitions never work as desired in ALL popular browsers... nevertheless simply add some padding or margin spacing in the middle to ensure a 10px gap between the two buttons whilst maintaining % defined layout.

An example CSS for the two buttons:

<style type="text/css">
    .button1 {
        padding-right: 5px;
    }
    .button2 {
        padding-left: 5px;
    }
</style>

<table width=500 height=50 cellpadding=0 cellspacing=0 border=0>
    <tr>
        <td width=50%><div class="button1"> </div></td>
        <td width=50%><div class="button2"> </div></td>
    </tr>
</table>

Then in your HTML table give each button a cell with 50% width each of the parent container.

To answer the second part of your question the best option is a browser sniffer in JavaScript. Get the screen dimensions, do the calculations and size your elements accordingly. You can't define % minus pixel in CSS.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ozzy
  • 8,244
  • 7
  • 55
  • 95
  • Forget about ALL browsers, I've seen % widths break across two instances of IE8 running on different machines. – yoozer8 Nov 26 '11 at 22:50
  • Agreed... Fortunately my main concern is webkit. Flash still works in most browsers for now and I'm shooting for feature parity. http://jsfiddle.net/maKuG/2/ Am I placing the padding in the wrong area? Not seeing the spacing in win7 FF8. – Shane Nov 26 '11 at 22:55
  • Lol. It helps to use a doctype strict dtd statement and a fixed table layout. Even then a 100% height object could fill a different parent object between IE and firefox/chrome. Its best to stick with strict pixel definitions. – Ozzy Nov 26 '11 at 22:57
  • Completely agree that strict pixel definition is my the path of least resistance in 90%+ cases, in this situation it's a last resort unfortunately.Trying to optimize a recursive tweening and resizing engine is going to keep me out of the sunshine for a while if I go with left, top, width, etc. When the number of elements starts to increase, CSS does a much better job of dynamic resizing than javascript in my experience. – Shane Nov 26 '11 at 23:06
0

I found the general answer here: CSS 100% height with padding/margin

and applied it to my case below:

<div id="buttonBar">
    <div class="buttonWrapper">
        <div class="innerWrapper"><button>My Button</button></div>
    </div>
    <div class="buttonWrapper" style="left:33.333%;" >
        <div class="innerWrapper"><button>My Button</button></div>
    </div>
    <div class="buttonWrapper" style="left:66.666%">
        <div class="innerWrapper"><button>My Button</button></div>
    </div>
</div>

CSS::

*{
  position:absolute;
}

div#buttonBar{
 width: 300px;
 height: 35px;
 background: #933;
}

div.buttonWrapper{
  width: 33.333%;
  height: 35px;

}

button{
  display: block;
  width: 100%;
  height: 100%;
  border: 6px solid #333;
  background: #eee;
}
div.innerWrapper{
    bottom:0;
    top:0;
    left:0;
    right:0;
    border-right: 5px solid transparent;
    border-left: 5px solid transparent;
}

There's a lot of wrapping going on, but that part won't interfere with my architecture. Here's the example where it's working:

EDIT: looks like I had a link to the wrong version previously, updated link below: https://jsfiddle.net/hyperthalamus/maKuG/32/

Thanks for all the suggestions.

Syscall
  • 19,327
  • 10
  • 37
  • 52
Shane
  • 4,921
  • 5
  • 37
  • 53
  • Taken this a little bit further to demonstrate why this can be important. Got some tweening examples here, though tweening isn't the point so much as this helps achieve automatic adaptive layout through css alone: http://jsfiddle.net/hyperthalamus/jg5pn/ – Shane Nov 28 '11 at 13:15