The width of the textarea is too small to accommodate the CodeMirror highlighted JS code text

Today I spent time working with CodeMirror to build a specific environment for editing PHP code stored in a secure database. Everything is functioning properly - the editor, code highlighting, indent tabs - except for one issue that has been bothering me. When the code within my CodeMirror textarea exceeds the length of the textarea, it disappears off the screen (refer to the screenshot at the end of this post).

I am seeking a solution where the code can continue on a new line below without adding an extra line number. Is this a common problem and is there an easy fix available?

View the screenshot here: http://www.example.com/codemirror.png

Thank you for any help provided.

//Edit: issue resolved

I have successfully solved the problem by making adjustments to the CSS file. For those interested, here is the fix:

.CodeMirror {
  overflow-y: auto;
  overflow-x: scroll;
  width: 700px;
  height: auto;
  line-height: 1em;
  font-family: monospace;
  _position: relative; /* IE6 hack */
}

By setting overflow-y to auto (allowing the height to adjust automatically), using overflow-x to scroll (preventing CodeMirror from exceeding the textarea's width), and specifying a fixed width, the issue was resolved. You can also add a max-height, but may need to set overflow-y to scroll as well in that case.

Answer №1

To activate word wrapping in CodeMirror, simply set the lineWrapping parameter to true. For example:

<textarea id="code" name="code">
      ...
</textarea>

<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
tabMode: "indent",
matchBrackets: true,
theme: "night",
lineNumbers: true,
lineWrapping: true,
indentUnit: 4,
mode: "text/javascript"
});
</script>

Answer №2

The issue has been resolved, please refer to the question for further information in case someone else encounters the same problem.

Answer №3

In agreement with Chris' input, the solution outlined in the question may no longer be effective.

Nevertheless, including max-width: ...ex; in the CSS code appears to enforce a horizontal scrollbar.

Furthermore, solely utilizing width: ... without implementing lineWrapping: true in CodeMirror (as suggested in fywe's response) could lead to noticeable stretching of the box for long lines. It is important to note that while lineWrapping can address this issue, it comes with its own limitations such as navigation difficulties with Home/End keys.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Align the final inline-block to the right

Imagine having an unordered list with 5 list items. The list items have been styled to display: inline-block in order for them to stack horizontally, left aligned. However, the challenge arises when you desire the very last list item to be right aligned. ...

Struggling to Locate the Chat Element on Google Hangouts Using Selenium Python

Lately, I have been struggling with the selenium module in Python. I am attempting to create a script that can automatically send messages to my friends as requested by one of them. Although I can successfully log into Google Hangouts using Selenium, I am ...

When my website is viewed on a local server, it is responsive and utilizes flex and bootstrap. However, now that it is hosted online, the flex function is not working properly on the mobile site

As a novice web developer, I've created a portfolio site hosted at www.j-price.co.uk. However, I've encountered a responsiveness issue after hosting it on GitHub Pages and GoDaddy. The site displays differently compared to when it's hosted l ...

Get rid of the "clear field" X button for IE11 on Windows 8

After trying out the suggestions provided in Remove IE10's "clear field" X button on certain inputs? .someinput::-ms-clear { display: none; } I successfully removed the X button on IE 11 running on Windows 7, but unfortunately it didn&a ...

Disappearing Data in Chart.js When Window is Resized

I am utilizing the Chart.js library to present a line chart in a div that is enclosed within a <tr>. <tr class="item">...</tr> <tr class="item-details"> ... <div class="col-sm-6 col-xs-12 chart-pane"> <div clas ...

An issue arises in CSS when the styling is not correctly implemented on the HTML elements

I am currently trying to recreate this code on my own page: https://codepen.io/Vlad3356/pen/PoReJVg Here is the code I have written so far: https://codepen.io/Vlad3356/pen/GRxdMPz I'm puzzled as to why, when I click on a star in my version of the co ...

Adjusting the width of a div element horizontally in Angular 4 and

As someone new to Angular 4, I've been attempting to create a resizable div (horizontally) without success. My goal is to be able to click and drag a grabber to resize the text area horizontally. However, in the example provided in the link below, the ...

How to Fix Tags Moving to Bottom when Hovered Due to Limited Space?

Recently, I've been facing an issue with the tags on my website. When I hover over them, a remove icon appears, causing the tags to increase in size. This results in a problem where, if I hover over the rightmost tag and there isn't enough space ...

What is the best method for adjusting the size of a button on a video as needed?

@import url("https://fonts.googleapis.com/css2?family=Luxurious+Script&display=swap"); * { margin: 0px; padding: 0px; box-sizing: border-box; font-family: "Luxurious Script", cursive; } body { background-color: #e97272; } .navbar { backgr ...

Centralize Arabic symbol characters

Is it possible to center the symbols that by default go above characters such as مُحَمَّد? .wrapper { text-align: center; vertical-align: center; } span { font-size: 4rem; display: inline-block; padding: 2rem; margin: 0.2rem; ba ...

The generated div from the Repeater Control is lacking appropriate CSS styling

I have a Repeater Control that is being used to generate div elements. While the DIV elements are successfully generated, the CSS formatting is not appearing correctly. Below is my HTML Code: <asp:Repeater ID="rptr" runat="server"> <Item ...

Issue of Modal not hiding persists despite attempted solutions using JS, jQuery, and CSS

I am attempting to display a modal exclusively on mobile devices. Despite my confidence in the correctness of my JavaScript, it is not functioning as expected. Would anyone be willing to review and provide assistance? <div class="col-5 col-sm-2 ml-au ...

Best practices for customizing form controls in Material UI to appear as TextFields

I am currently in the process of creating a form that includes multiple TextField elements. Within this form, there are various other elements in addition to TextFields, but unfortunately they do not blend well with the TextFields in terms of appearance. Y ...

Set the border-bottom style to match the width of the navigation bar menu

Struggling to align a horizontal line perfectly with the container and text below on my WordPress site. Check out the image reference here The bottom border doesn't seem to be matching up with the body-container of my website. Is there a way to adjus ...

Sass: Setting a maximum width relative to the parent element's width

I have a resizable container with two buttons inside, one of which has dynamic text. Within my scss file, I am aiming to implement a condition where if the width of the container is less than 200, then the max width of the dynamic button should be 135px, ...

Tips for preventing Stack Overflow errors in recursive methods that run infinitely

While I know that stack overflow errors are a common issue and I understand why mine is happening, my question revolves around finding an alternative to recursion in this specific scenario. I have a class that establishes and maintains a client connection ...

Is there a way to adjust the placement of the h1 tag using the before pseudo-element?

I'm looking to place an image on the left side of each h1 tag. I've been attempting to utilize the before pseudo element for this task. The issue arises when there is no content, causing the before pseudo element to overlap with the h1 tag. How ...

Ways to reduce lag while executing a for loop

How can I optimize my prime number generation process to avoid lagging? For example, is there a way to instantly display the results when generating primes up to 1000? HTML: <!DOCTYPE html> <html> <meta name="viewport" content="width=dev ...

Tips for eliminating overlapping borders in a table using CSS

I've encountered an issue with my table where the border becomes darker and thicker at the intersection of two cells. Is there a way to resolve this problem? I have included my CSS code below. .custom-table-2 { border-collapse: separate; /*border- ...

404 error occurs when Spring-Boot fails to include CSS file through ResourceLocations

My spring-boot application is currently running smoothly on a local computer, but I have encountered an issue. When I run mvn package, none of my CSS or JavaScript files located in /src/main/wepapp/css are included in the jar file created in the target d ...