Marius
Marius

Reputation: 413

Maintaining white space in html select tag

I have a list of strings that I would like to display in a HTML select object. The strings look something like :

id - name - description

I would like the fields to align however. In PHP I'm using

sprintf ("%4s%10s%20s", $id, $name, $description);

which works fine. The problem is the multiple spaces is compacted to 1 space in the select list. I tried using the pre and white-space CSS properties of the select box, but it has no effect. Any suggestions?

Upvotes: 3

Views: 3644

Answers (7)

sirhc
sirhc

Reputation: 6097

you have to use   instead, they are non-breaking spaces so it won't collapse.

You could do:

str_replace(" ", " ", sprintf("%4s%10s%20s", $id, $name, $description));

Upvotes: 4

Lizard
Lizard

Reputation: 45002

Convert spaces to  ?

Try to use   (Non breaking space) is that what your looking for?

Upvotes: 0

Pim Jager
Pim Jager

Reputation: 32119

Replace the multiple spaces following each other with non breaking spaces:

 

Upvotes: 0

Brent Arias
Brent Arias

Reputation: 30175

Wrap it in the html "code" tag.

Upvotes: 0

Austin Salonen
Austin Salonen

Reputation: 50225

You may have to replace the spaces with   to make this work correctly; though part of me thinks there's a much better to do this but my HTML-fu is weak...

Upvotes: 0

Goran Jurić
Goran Jurić

Reputation: 1839

Replace spaces with   HTML entity.

Upvotes: 0

Scott Saunders
Scott Saunders

Reputation: 30394

Use CSS to set the font of the options to a monospaced font. Use   instead of spaces (you can use str_pad() instead of sprintf()).

https://www.php.net/str_pad

Upvotes: 3

Related Questions