my simple textarea doesn't show a horizontal bar when text overflows. It wraps text for a new line. So how do I remove wordwrap and display horizontal bar when text overflows?
6 Answers
textarea {
white-space: pre;
overflow-wrap: normal;
overflow-x: scroll;
}
white-space: nowrap
also works if you don't care about whitespace, but of course you don't want that if you're working with code (or indented paragraphs or any content where there might deliberately be multiple spaces) ... so i prefer pre
.
overflow-wrap: normal
(was word-wrap
in older browsers) is needed in case some parent has changed that setting; it can cause wrapping even if pre
is set.
also -- contrary to the currently accepted answer -- textareas do often wrap by default. pre-wrap
seems to be the default on my browser.
-
20With FF 20, you still need wrap="off" in the html textarea tag! This technology is such inconsistent crap. Apr 21, 2013 at 22:05
-
4+1 for providing a CSS solution and pointing out that textareas DO wrap by default– YePhIcKJan 27, 2014 at 0:12
-
1Ah, there seems to be a feature request on Firefox from 2001 but with some recent (2014) supporting comments for Chrome behavior by dev Ehsan Akhgari: bugzilla.mozilla.org/show_bug.cgi?id=82711 . Go and upvote it! Aug 28, 2014 at 13:00
-
1This now works in Firefox (Aurora channel, v36) - see jsfiddle.net/mlhDevelopment/gvjy14xu the green textarea.– mlhDevDec 30, 2014 at 19:52
-
2Personally, I think it's better to replace
overflow-x: scroll
withoverflow: auto
. The scroll bars reduce the available typing space in the text area. Also, IE11 rendered a vertical scroll bar unnecessarily with your way, butoverflow: auto
fixed it.– SamJun 15, 2016 at 0:11
Textareas shouldn't wrap by default, but you can set wrap="soft" to explicitly disable wrap:
<textarea name="nowrap" cols="30" rows="3" wrap="soft"></textarea>
EDIT: The "wrap" attribute is not officially supported. I got it from the german SELFHTML page (an english source is here) that says IE 4.0 and Netscape 2.0 support it. I also tested it in FF 3.0.7 where it works as supposed. Things have changed here, SELFHTML is now a wiki and the english source link is dead.
EDIT2: If you want to be sure every browser supports it, you can use CSS to change wrap behaviour:
Using Cascading Style Sheets (CSS), you can achieve the same effect with
white-space: nowrap; overflow: auto;
. Thus, the wrap attribute can be regarded as outdated.
From here (seems to be an excellent page with information about textarea).
EDIT3: I'm not sure when it changed (according to the comments, must've been around 2014), but wrap
is now an official HTML5 attribute, see w3schools. Changed the answer to match this.
-
9The "wrap" attribute works in Firefox 3.6, but isn't valid HTML5. However, the CSS solution doesn't work, as if "white-space:nowrap" is ignored. Mar 21, 2011 at 21:56
-
9
white-space: pre;
(orpre-line
/pre-wrap
) had the same affect aswrap="off"
for me (whereaswhite-space: nowrap
didn't respect thepadding
)– philfreoMar 18, 2013 at 21:34 -
5
-
4@Mottie It would be much better to link to current version of HTML5 specification than to Mozilla docs. Here's the link for current version – w3.org/TR/2014/CR-html5-20140731/forms.html#attr-textarea-wrap Sep 10, 2014 at 14:08
-
3
wrap="off"
is no longer valid however it is requirement for both IE and Edge!– JoolsDec 17, 2018 at 11:06
The following CSS based solution works for me:
<html>
<head>
<style type='text/css'>
textarea {
white-space: nowrap;
overflow: scroll;
overflow-y: hidden;
overflow-x: scroll;
overflow: -moz-scrollbars-horizontal;
}
</style>
</head>
<body>
<form>
<textarea>This is a long line of text for testing purposes...</textarea>
</form>
</body>
</html>
I found a way to make a textarea with all this working at the same time:
- With horizontal scrollbar
- Supporting multiline text
- Text not wrapping
It works well on:
- Chrome 15.0.874.120
- Firefox 7.0.1
- Opera 11.52 (1100)
- Safari 5.1 (7534.50)
- IE 8.0.6001.18702
Let me explain how i get to that: I was using Chrome inspector integrated tool and I saw values on CSS styles, so I try these values, instead of normal ones... trial & errors till I got it reduced to minimum and here it is for anyone that wants it.
In the CSS section I used just this for Chrome, Firefox, Opera and Safari:
textarea {
white-space:nowrap;
overflow:scroll;
}
In the CSS section I used just this for IE:
textarea {
overflow:scroll;
}
It was a bit tricky, but there is the CSS.
An (x)HTML tag like this:
<textarea id="myTextarea" rows="10" cols="15"></textarea>
And at the end of the <head>
section a JavaScript like this:
window.onload=function(){
document.getElementById("myTextarea").wrap='off';
}
The JavaScript is for making the W3C validator passing XHTML 1.1 Strict, since the wrap attribute is not official and thus cannot be an (x)HTML tag directly, but most browsers handle it, so after loading the page it sets that attribute.
Hope this can be tested on more browsers and versions and help someone to improve it and makes it fully cross-browser for all versions.
-
4+1 .wrap='off' is the magic that did the trick in FF 14.0.1... Thanks.– ShanimalJul 19, 2012 at 14:40
-
The JavaScript does not seem to work on Safari 5.0.6 (Last PPC version), even though you can use wrap="off" in the HTML.– user1985657May 19, 2013 at 2:36
-
.wrap is what did it for me, using Chrome. I also used it in conjunction with the no-wrap in css. Jan 31, 2016 at 3:31
-
This question seems to be the most popular one for disabling textarea
wrap. However, as of April 2017 I find that IE 11 (11.0.9600) will not disable word wrap with any of the above solutions.
The only solution which does work for IE 11 is wrap="off"
. wrap="soft"
and/or the various CSS attributes like white-space: pre
alter where IE 11 chooses to wrap but it still wraps somewhere. Note that I have tested this with or without Compatibility View. IE 11 is pretty HTML 5 compatible, but not in this case.
Thus, to achieve lines which retain their whitespace and go off the right-hand side I am using:
<textarea style="white-space: pre; overflow: scroll;" wrap="off">
Fortuitously this does seem to work in Chrome & Firefox too. I am not defending the use of pre-HTML 5 wrap="off"
, just saying that it seems to be required for IE 11.
-
Nice one, saved my day! Was breaking my head over why programmatically adding newlines failed using the code from the accepted answer.– TumFeb 24, 2018 at 19:07
-
1
If you can use JavaScript, the following might be the most portable option today (tested Firefox 31, Chrome 36):
- a div with
contenteditable="true"
- the styles suggested by Partly
- JavaScript form submission on button click: How to submit a form using javascript?
http://jsfiddle.net/cirosantilli/eaxgesoq/
<style>
div#editor {
white-space: pre;
word-wrap: normal;
overflow-x: scroll;
}
<style>
<div contenteditable="true"></div>
There seems to be no standard, portable CSS solution:
wrap
attribute is not standardwhite-space: pre;
does not work for Firefox 31 fortextarea
. Fiddle, open feature request.
Also, if you can use Javascript, you might as well use the ACE editor:
http://jsfiddle.net/cirosantilli/bL9vr8o8/
<script src="http://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
<div id="editor">content</div>
<script>
var editor = ace.edit('editor')
editor.renderer.setShowGutter(false)
</script>
Probably works with ACE because it does not use a textarea
either which is underspecified / incoherently implemented, but not sure if it is uses contenteditable
.
-
1+1 Out of all the answers here, the CSS here is the only one that works reliably on both Chrome and Safari's textarea. Other answers just work on Chrome only Nov 17, 2021 at 5:37