I am using PHP to generate my page and CSS within the generated page to layout a form with absolute positioning.
I know the font being used (unless the user overrides it in the browser, of course) and the text of the button. Can I calculate the width of the button – even approximately – in PHP?
What I am trying to do is to have a for who’s controls are dynamically assigned and have 3 buttons – cancel/reset/submit – at the approximate left/middle/right of the forms controls, rather than of the browser screen.
You can use imagettfbbox() to get the size of the text. Using this you can calculate the average button size (you just need to add the distance between the text and border of the button).
However, buttons should be on the left or on the right. That’s how basically every native dialog does it and staying consistent with common UI paradigms/guidelines is usually a good thing.
Besides that, there’s most likely a way to do it using CSS and not precalculating any sizes (and if you really have to do it, do it in JavaScript).