Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the width size in <pre>

Tags:

html

I'm trying to make a pre to limit the width size that does not exceed the table, try using this tag:

<?php
echo "<pre style='max-width:1px;'>".$long_string."</pre>";
?>

both the <pre> and the table but does not work in any case.

anyone can help me?

like image 200
Nick FuryReturn Avatar asked Sep 25 '14 01:09

Nick FuryReturn


People also ask

What is pre ></ pre?

The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.

What is Max-width and width?

The difference between width: 100% and max-width:100% is that: First, width define the width of specific element while max-width define the maximum size the element is allow to have.

What does pre mean in HTML?

<pre>: The Preformatted Text element. The <pre> HTML element represents preformatted text which is to be presented exactly as written in the HTML file. The text is typically rendered using a non-proportional, or monospaced, font. Whitespace inside this element is displayed as written.

How do I change font size in pre tag?

While the font-size cannot be changed for text directly inside pre tags, you can always wrap that text in another element (a span, for example) and change the font size of that element.


1 Answers

Setting max-width on a pre element actually restricts it width. You can see this by inspecting the element using a browser’s Developer Tools and also simply by setting a background color on it, say pre { background: yellow }. Note: testing this with the outline property gives a wrong result in Firefox; but even it implements the border property correctly.

As usual in rendering elements, content may overflow the width of the element, and the default is visible overflow. If you wish to cut it off, rendering just the part that fits in, set pre { overflow: hidden }. If you wish to indicate truncation with an ellipsis “…”, set also pre { text-overflow: ellipsis; }.

If you would want to make long lines wrap when needed to make the content fit, you can instead set pre { white-space: pre-wrap }. However, long strings without spaces could still cause overflow. This can be prevented by adding pre { word-wrap: break-word; } (old syntax) or pre { overflow-wrap: break-word; } (new syntax, with slightly more limited browser support).

like image 157
Jukka K. Korpela Avatar answered Sep 21 '22 03:09

Jukka K. Korpela