Sizing a <td> from a string
I'm not sure if this is a HTML related issue or PHP, but:
I am loading a lo开发者_如何学JAVAng string from a database into tags. I want it to make it break into lines (instead if a giant line that spans past your screen size)when the string is loaded. How can i do this?
Use a <p></p>
tag to contain the text you're trying to load and use CSS to constrain the size of it.
Also, I'd question the use of a table at all unless you're loading data that should be presented in a tabular format.
I'd also avoid making any formatting decisions on the PHP side unless you absolutely have to. Using PHP to auto-add <br>
tags to everything, or using <br>
tags to enforce page width is a really bad method. If they resize the browser window or change text height (accessibility) your forced formatting will get in the way.
TD tags get weird with content depending on the browser. If you can avoid using tables, I'd start there. If not, encasing the text in another block element, like <p>
might keep it from stretching the <td>
into one line, especially if you use a CSS style like width: 100%
on the <p>
. In general, PHP should do no formatting if you can help it, and HTML+CSS should do the rest. Tables are a bad choice when attempting to lay out a page, and should be replaced with <div>
tags and appropriate CSS, with the only exception being data presented in tabular format, such as a table of data.
Combine PHP's wordwrap()
and nl2br()
functions to limit line size and then convert the newlines generated by wordwrap()
into HTML <br />
.
echo nl2br(wordwrap($alongstring, 72));
Not sure how this affects the <td>
tags mentioned in your question title. How are you using those?
If you set a width to the container, it will wrap the text automatically when it reaches the end of the container unless you add white-space:nowrap;
in CSS.
精彩评论