Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML <pre> tag values get horizontal scroll bars,how to format it?

Tags:

html

pre

This image show what i happens in my code This is my blog page. Sometimes I have to use C#,PHP and other code snippets in the blog. Therefore I use pre tags to make it looks nice and keep the formatting code as it is. pre tag width is 100% and it has a background colour. However, the text went away from it as in the above image and horizontal scroll bar going to appear. How can I overcome this? I don't want the bottom scroll bar.

like image 487
Elshan Avatar asked Dec 03 '12 03:12

Elshan


People also ask

How do I fix the horizontal scroll bar in HTML?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML.

How do you code a horizontal scroll in HTML?

To make a scroll box with a horizontal scroll, you need to use the overflow-x property. Specifically, you need to use this code: overflow-x:scroll; . This tells your browser to create scroll bars on the x (horizontal) axis, whenever the contents of the container is too wide.

What is the HTML tag to make text scroll horizontal?

The <marquee> tag in HTML is used to create scrolling text or image in a webpages. It scrolls either from horizontally left to right or right to left, or vertically top to bottom or bottom to top.

How do you code a scroll bar in HTML?

HTML File Syntax for ScrollbarsBy using the <Style> tag, we will add the scroll options in HTML Page itself.


2 Answers

There are a few solutions:

The simplest is to place line breaks in the text. This probably is not what you want though, since it will either leave large right margins or still cause scroll bars, unless the browser window is just the right size.

Another is to use white-space:pre-wrap; in the CSS for your <pre> tag. This will cause the text to wrap to a new line as necessary, but still preserve all whitespace present in the source. See https://developer.mozilla.org/en-US/docs/Web/CSS/white-space for more information about this CSS property.

Alternatively, if you do not want any line breaks added (either manually or through automatic word wrap), you can use one of the following CSS properties for your <pre> tag:

  • overflow-x:hidden; This will simply cut off the text that extends past the right edge. It will not be visible at all.
  • overflow-x:scroll; This will cause scroll bars to appear within your webpage in case text extends past the right edge. This will prevent the entire page from getting so wide that scroll bars appear at the bottom. See http://www.w3schools.com/cssref/playit.asp?filename=playcss_overflow-x&preval=scroll for an example.
  • overflow-x:auto; Like overflow-x:scroll; except the scrollbars only appear if required. (Thanks ccalvert in the comments)
like image 50
Dominick Pastore Avatar answered Sep 17 '22 04:09

Dominick Pastore


add style:

pre {      white-space: pre-wrap;      word-break: break-word; } 
like image 28
Rayees Pk Avatar answered Sep 19 '22 04:09

Rayees Pk