kirki icon indicating copy to clipboard operation
kirki copied to clipboard

Font Size in em

Open famfam opened this issue 11 years ago • 5 comments

Using a slider is easy set a font size control when it is in px but when it is in em is so hard select the correct value. Is possible select the value in px and output in em when 1em = 16px?

famfam avatar May 01 '15 16:05 famfam

Why is it difficult to use the slider for ems??

aristath avatar May 01 '15 16:05 aristath

Is hard select a correct value by example 1.090909090909091em

famfam avatar May 01 '15 16:05 famfam

Using a slider to select a value like that wouldn't make much sense.

I can think of 2 alternatives:

  1. Use a text field that will allow the user to just enter the value and units they want to use. Then, on your field you can add a custom callback that will take care of the validation. See the example code below.
  2. Use a slider with .01 steps. This will allow users to select a value of 1.09 for their ems.

Example validation function for the text field:

function my_custom_validation( $value ) {

    // Trim the value
    $value = trim( $value );
    // The array of valid units
    $units = array( 'rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax' );
    foreach ( $units as $unit ) {
        // The raw value without the units
        $raw_value = str_replace( $unit, '', $value );
        // Find what unit we're using
        if ( false !== strpos( $value, $unit ) ) {
            $unit_used = $unit;
        }
    }
    $unit_used = ( isset( $unit_used ) ) ? $unit_used : '';
    $raw_value = (float) $raw_value;

    $value = $raw_value . $unit_used;

    return $value;

}

aristath avatar May 01 '15 16:05 aristath

For now I'll use option two with 0.005 step. Is possible a slider with 4 decimals?

famfam avatar May 01 '15 19:05 famfam

The best way to answer that is to simply test it yourself. Did you try it?

aristath avatar May 24 '15 14:05 aristath