4

I'm trying to disable horizontal scrolling in HTML pre tag

I'v tried this:

pre {
    height: auto;
    max-height: 500px;
    overflow:hidden;
    background-color: #eeeeee;
}

but it does not work correctly

it appear like this:

enter image description here

The text keep going to left edge. (the direction of my html page is RTL Right to Left)

Any Suggestion?

askm
  • 195
  • 1
  • 7
  • 19

3 Answers3

12

overflow-x: hidden cuts the text off, but doesn't wrap it.

The white-space CSS property instead sets how white space inside an element is handled; pre-wrap preserve sequences of white space, lines are broken at newline characters, at <br>, and as necessary to fill line boxes.

pre {
  white-space: pre-wrap;
}

This article gives a lot of great info on the pre tag: https://mediatemple.net/blog/tips/considerations-for-styling-the-pre-tag/

Ghini Antonio
  • 2,992
  • 2
  • 25
  • 48
kashgo
  • 141
  • 1
  • 6
1

Try adding this:

text-align: right !important;
overflow-x: hidden !important;

Also, this article could be interesting for your work: https://alfy.me/2014/07/26/lets-talk-about-rtl.html

Hope it helped! Good coding! ;)

Edit:

I will explain myself.

By adding text-align: right !important; we are forcing the text to be aligned right, being your website a RTL, this is the way to go.

By adding overflow-x: hidden !important; we are saying that we don't want horizontal scrolling.

The !important statement is used to give priority to this css rule, it is used to override other styles.

SrAxi
  • 19,787
  • 11
  • 46
  • 65
  • When you start using `!important` there is no way back :) there is always a way to get around without using it. – Morpheus Mar 21 '17 at 09:59
  • The same problem again currently the text keep going to the left edge i want to make the text stop at specific limit, then start a new line. – askm Mar 21 '17 at 10:02
  • @Morpheus Yes, indeed. `!important;`is a Rambo solution, but I figured out that @askm had to override styles – SrAxi Mar 21 '17 at 10:06
  • @askm Then I think that your question wasn't well explained or I understood badly. If you want the text not to 'touch' the edge of the screen. Put your text inside a container (div) and give it a size and a position so you can control it. Also, read the article I linked, it can help you with the style of website you want to make. Cheers! – SrAxi Mar 21 '17 at 10:08
1

Answer is found in this post: How do I wrap text in a pre tag?

pre {
    white-space: pre-wrap; /* Since CSS 2.1 */
    white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
    white-space: -pre-wrap; /* Opera 4-6 */
    white-space: -o-pre-wrap; /* Opera 7 */
    word-wrap: break-word; /* Internet Explorer 5.5+ */
}
Ooker
  • 1,969
  • 4
  • 28
  • 58
dripcode
  • 31
  • 2