Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Wrap Text in HTML Option [duplicate]

I want to put a dropdown list on my page, As one option is too long, when I click the select menu, it will expand and cover the other part of the page.

<select id="selectlist" name="SelectProgram">

    <option value="LIR" >LIR</option>
        <option value="How to word wrap text in HTML?">How to word wrap text in HTML? CSS / HTML text wrap for webkit</option>

The second option is too long and will expand. Is there any way I could wrap such a long text into 2 lines? Thank you!

like image 301
Chengzhi Avatar asked Aug 06 '12 19:08

Chengzhi


People also ask

How do you wrap text overflow?

The overflow-wrap CSS property applies to inline elements, setting whether the browser should insert line breaks within an otherwise unbreakable string to prevent text from overflowing its line box.

What is wrap text option?

"Wrapping text" means displaying the cell contents on multiple lines, rather than one long line. This will allow you to avoid the "truncated column" effect, make the text easier to read and better fit for printing.


2 Answers

Seeing as the value and text of a Select can be very different. I would say if in the event a string thats in your select for the text is longer than X truncate it.

Example with jquery

$('#myselect option').each(function()
{
    var myStr = $(this).text();
    if(myStr.length > 15){$(this).text(myStr.substring(15));}
});

put that in your document ready, and it will trim your text down to a better size, wrapping your elements in a div that will hide the overflow is going to make something of a mess later, and you will be back here trying to solve a similar issue caused by that for strings that are smaller.

like image 198
chris Avatar answered Sep 23 '22 02:09

chris


Setting a fixed width can be done without the wrapping div by simply using

<select id="selectlist" name="SelectProgram" style="width: 500px">
   <option value="LIR" >LIR</option>
   <option value="How to word wrap text in HTML?">How to word wrap text in HTML? CSS / HTML text wrap for webkit</option>
</select>

In practice, you should put width: 500px in a matching rule in your CSS file.

like image 31
bkconrad Avatar answered Sep 22 '22 02:09

bkconrad