Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have the following CGI script that creates the radio button in horizontal way.
use CGI; # snip strong('Analysis Type: '),br radio_group( -name => 'analysis', -multiple => 1, -values => [ 'Light', 'Medium', 'Full' ], ), br, br #snip
The problem I have is that these 3 radio button are placed too close to each other. Looks like this:

(*)Light(*)Medium(*)Full # Note that there is no space between text and the button.
Is there a way to space them?

Replies are listed 'Best First'.
Re: Spacing out the radio button in CGI.pm
by liverpole (Monsignor) on Jun 05, 2006 at 11:14 UTC
    Sometimes the simple way is the best.  (Actually, usually the simple way is the best!)

    Try something like this:

    -values => [ ' Light ', ' Medium ', ' Full ' ],

    and play around with the spacing until it looks just right!

    If you need to create multiple spaces, you may of course have to use the "non-breaking space" directive " " ...

    -values => [ 'Light  ', 'Medium  ', 'Full&nbsp +; ' ],

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      But adding space to the value changes the value. I'd use CGI's support for CSS to insert some padding to those elements.

        Then let's do this instead.

        radio_group(-name => "analysis", -multiple => 1, -values => [ "Light", + "Medium", "Full" ], -labels => {"Light", " Light ", "Medium", " Medi +um ", "Full", " Full "})
        That's a perfectly good solution as well!  It's just that it's more complicated (ie. takes more work, takes more code, etc.), so it's not quite as quick a fix.  But if keeping the value unchanged is important, then by all means go with CSS.

        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Spacing out the radio button in CGI.pm
by jdtoronto (Prior) on Jun 05, 2006 at 14:11 UTC
    The easiest way to do this is not with CSS but by using the labels for the buttons. Reading the CGI documentation you will find you can create a HASH which ties a label to the value for each button.
    radio_group(-name=>'group_name', -values=>['eenie','meenie','minie'], -default=>'meenie', -linebreak=>'true', -labels=>\%labels, -attributes=>\%attributes);
    Supplying a ref to this hash allows the labels to be totally unrelated to the value and will allow you to achieve what you want.

    Even more interesting from the layout perspective, is the ability to have the button array created within an HTML table, viz:

    All modern browsers can take advantage of the optional parameters -row +s, and -columns. These parameters cause radio_group() to return an HT +ML3 compatible table containing the radio group formatted with the sp +ecified number of rows and columns. You can provide just the -columns + parameter if you wish; radio_group will calculate the correct number + of rows for you.
    jdtoronto