1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

CSS for Textarea With Console Style Layout

Discussion in 'CSS' started by Heretic86, Aug 15, 2021.

  1. #1
    I have a Textarea that I want to behave like a Console / cmd.exe prompt.

    I have applied a cols and rows to control the size, and the following CSS rules:

    word-break: break-all;
    white-space: break-spaces;
    Code (markup):
    The use of width and height properties is also fine, but inconsistent across browsers on Textarea elements, much like <pre> elements work, but <pre> elements do not have additional characteristics that I need.

    Works exactly as I want in Firefox, but not in Chrome:

    Firefox (works like I want)
    [​IMG]

    Chrome (does NOT work like I want, notice that "foo" is word broken at 42 chars)
    [​IMG]
    In Chrome, the issue is that it breaks in the middle of the previous word at the same size, but adjusting that size a bit, the spaces are retained on the previous line and collapsed, which is NOT what I want.

    • I want the space characters after the word is wrapped to the next line.
    • I would like to do so without having to replace User Text with Javascript or additional elements.
    • Each line must display all characters, especially spaces, uncollapsed
    • Each line can not have any more than 43 characters so they should wrap to the next line uncollapsed
    • Line Wraps should NOT occur in the middle of the previous word
    • Line Wraps should NOT wrap spaces to the last line as it exceeds 43 characters
    • Line Wraps should NOT collapse spaces
    In Chrome, the previous line is not wrapped in the correct location. The user should NOT have to hit ENTER, so it should automatically wrap the space to the next line, like a Console.

    https://www.webucate.me/textarea.htm

    How can I make my Textarea break at 43 characters and wrap \n Newlines and preserve Spaces to the next line in consistently?
     
    Heretic86, Aug 15, 2021 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Does it need to be CSS or can we add a javascript listener?
     
    sarahk, Aug 15, 2021 IP
  3. Heretic86

    Heretic86 Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Either. I was hoping to do it in CSS as a listener approach is gonna be a pain. Trust me, I tried... just take a peek at the listeners already in the example page just to limit the number of total lines to a "maxrows" attribute...
     
    Heretic86, Aug 15, 2021 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    qwikad.com, Aug 15, 2021 IP
  5. Heretic86

    Heretic86 Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Tried it. Same effect. Your code works how I would like in Firefox, but not in Chrome.

    It was a good idea. I put it on a <pre> element, but then I am not able to get element.selectionStart or element.selectionEnd on my listeners which I use to limit the input to x lines. Pain in the butt there because we have to factor in the total number of newline \n characters bla bla bla (its unrelated) so the <pre> idea is good, but breaks a lot of stuff. Considering the behavior is different in each browser, I need a different approach and I am plum out of ideas.

    Tried pseudo elements too. They dont work so well in Textareas and Newlines since n is always an unknown.

    This is turning out to be a deceptively difficult problem...
     
    Heretic86, Aug 15, 2021 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Switch break-all for break-word.

    In this case what you're seeing is that FF does NOT implement break-all properly. Chrome's behavior is the correct one for what break-all means.
     
    deathshadow, Aug 16, 2021 IP
  7. Heretic86

    Heretic86 Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    Hmm thats interesting that Chrome is the one with the correct behavior. I havent been able to get ANYTHING to work in chrome for me...

    What I would really like it to do is to not care what the character is and just wrap at the 43rd character, just like a Console...
     
    Heretic86, Aug 16, 2021 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    The combination for console-like behavior is:

    white-space:pre-wrap;
    word-break:break-all;
    Code (markup):
    Which is uniform between FF and Chrome. Just beware that as a form of pre-formatted text, /r/n/t and multi-space are all preserved... though if you're trying for a console-like input/output, that might be exactly what you want/need.

    The "hard' part being to actually get the "43rd character' since that's not actually a value one can reliably set in CSS, since fonts are not a uniform size and non-monospace fonts no two characters have the same width.

    So you'll want to set a monospace font, and even then you'd have to use a webfont for consistency of width across OS platforms.

    Though that raises the responsive/mobile issue where you shouldn't be trying to fix the width of things like textareas. At most you'd set a max-width.
     
    deathshadow, Aug 16, 2021 IP
  9. Heretic86

    Heretic86 Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    Tried the combination of those two suggested lines. The outcome is in Chrome it causes whitespace at the beginning of the line to collapse.

    A mobile version will be very different, and use a page completely independent of the one Im working on. This is styling for desktop like computers in this section.

    The limitations result because the text entered here is eventually rendered in a Canvas element, which, oddly was a thousand times easier to work with. So it has to look the same in this page as it does on my rendered output, which for me are repeatedly inconsistent.
     
    Heretic86, Aug 16, 2021 IP
  10. Heretic86

    Heretic86 Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #10
    Perhaps this visual explanation will work a little better:

    [​IMG]

    I want that space to NOT COLLAPSE or be put on the line above...
     
    Heretic86, Aug 17, 2021 IP