need some css help

mit74

Free Member
Jun 4, 2010
2,463
447
Anyone know how to auto margin buttons so they're evenly spaced within a div and when the number of buttons is dynamic. I know the width of the outer div (banner) and buttons. I was thinking I can easily do it using php but is there a CSS method?
 

Rudi

Free Member
Feb 14, 2012
321
72
Manchester
Not that I can think of, unless you make the parent relative, float the inner elements and use percentage widths for everything within?

EDIT: Actually no, that wouldn't work either. You could use JS to calculate the percentage widths you need to use.. But that's going against your requirement for CSS!
 
Last edited:
Upvote 0

mit74

Free Member
Jun 4, 2010
2,463
447
i have a outer div with specified width. I have buttons of say 100px width within that that need to be spaced evenly across the width regardless of how many buttons there are. If the user adds a button then the margin auto shrinks to fit the extra button in.
 
Upvote 0
W

WickITServices

I confess to occasionally resorting to good old fashioned tables, which is no longer regarded as best practice as there still seem to be real problems in achieving cross browser compatibility. But using tables is really not a good solution, particularly if you want your site to look okay on smart phones and other small screens.

You could play about with putting each button in a div of it's own with margin:0 auto; and position:relative; but it is certainly an exercise for when you have plenty of time to fiddle about.
 
Upvote 0

mit74

Free Member
Jun 4, 2010
2,463
447
Hi,

You could do it with jQuery - count the buttons, get the width of the div/parent element, calculate the spacing needed, then set it. Should work on any browser that has JavaScript enabled.

Cheers,

i've done it with php now thanks. same principal of counting buttons and calculate space and inject variable into margin-right.
 
Upvote 0

Rudi

Free Member
Feb 14, 2012
321
72
Manchester
i've done it with php now thanks. same principal of counting buttons and calculate space and inject variable into margin-right.

He's right though, to slightly change what I said you should use Javascript to do this. PHP doesn't really have any place deciding how your page should be laid out, I know you consider PHP to be a hack which it is, so is the JS really, but it's less of a hack :)
 
Last edited:
Upvote 0

mit74

Free Member
Jun 4, 2010
2,463
447
He's right though, to slightly change what I said you should use Javascript to do this. PHP doesn't really have any place deciding how your page should be laid out, I know you consider PHP to be a hack which it is, so is the JS really, but it's less of a hack :)

well if JS is turned off then I would probably use php just in case anyway as it already uses php.
 
Upvote 0

Rudi

Free Member
Feb 14, 2012
321
72
Manchester
well if JS is turned off then I would probably use php just in case anyway as it already uses php.

One way to think about it yes, but if a user has JS disabled they should expect something to be up, at worst it would surely just throw your box alignment off to the next line?

My main point is that PHP should be everything to make your application work, and not make it look pretty.

I guess it depends just how far you want to take good practice and maintanability, everyones views are different on this side of things (the others are wrong :D) :)
 
Upvote 0

Latest Articles

Join UK Business Forums for free business advice