Ensure that both textarea and pre elements have equal dimensions and line wrapping behavior in Internet Explorer

I am in the process of implementing a dynamic textarea that resizes automatically, based on a technique discussed in this article: Alistapart: Expanding Textareas (2011).

The idea is quite straightforward: by using a pre element to mirror the input in the textarea, you can resize the pre element which will in turn resize its container and the absolutely positioned textarea. So far, this method has been successful for me in Chrome, Safari, and even Firefox after adjusting a small padding issue found on Stack Overflow.

However, I have encountered an issue with modern versions of Internet Explorer (9+). The key to making this technique work is ensuring that the pre element mirrors not just the size of the textarea, but also its font characteristics and wrapping points.

To demonstrate this issue, I have created a sample on JSFiddle.

In the demonstration, the content of the textarea is displayed in red while the content of the pre element is shown in blue. Ideally, the textarea's content should overlay the pre's content perfectly, displaying only the red text.

While this synchronization works well in Chrome and Safari, it becomes out of sync in IE as the text wraps after a certain point. For example, in IE 11, the string:

Here's some text. It appears to do we

illustrates the problem:

We can only see the blue pre text on "we" because the textarea content has already wrapped.

Do you have any suggestions on how to keep the text synchronized between the textarea and the backing pre element in IE? Initially, I thought it might be related to differences in size or font, but after several attempts, it seems more like an inconsistent wrapping issue?

Although there are many JavaScript solutions available for resizing textareas, I was hoping to make this CSS approach work, if only for the learning experience.

Thank you in advance!

Answer №1

The issue you are facing is that Internet Explorer is not recognizing the short-hand font rule. When inspecting the style in IE's style inspector, you will notice it being underlined, especially in IE11. To resolve this, simply include a space between the font size and font family values. Check out the updated JSFiddle for reference.

Solution for IE:

font: 400 1em 'Helvetica', 'Arial', sans-serif;

Instead of:

font: 400 1em'Helvetica', 'Arial', sans-serif;

It's important to note that the single quotes are only necessary if the font name contains spaces or is a CSS keyword.

This adjustment should fix the wrapping issue. However, unlike WebKit browsers (Chrome/Safari), you may not be able to completely hide the underlying text within the pre element. In such cases, making it invisible is recommended as mentioned in the referenced article.

.expandingArea.active > pre {
  display: block;
  /* Hide the text; solely for sizing purposes */
  visibility: hidden;
}

Be sure to apply this additional step as well.

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

Creating a dynamic dropdown menu that changes based on the selection from another dropdown menu

I'm working on a project that requires users to make specific selections in dropdown menus that are interconnected. Does anyone know how to set up a form so that the options in dropdown menu #2 change based on what the user selects in dropdown menu #1 ...

Receive JSON data in URL as an array in PHP

What I aim to accomplish is: I have a JSON object structured like below var foo = { format:"json", type:"test", id:"26443" }; and my goal is to convert it into a URL in the following format 'http://example.com/a:3:{s:6:"format";s:4:" ...

Split the content of a textarea before it reaches the next element

I have a textarea in my html file and I am trying to prevent the text from overlapping with an emoji icon button. I would prefer not to use a z-index for the emoji as it may cause issues with other elements on the page. Below is the code snippet: HTML & ...

Utilize CSS transition to smoothly reveal a hidden div with a fading effect

I am trying to create a hover effect where a caption displayed over an image fades in with a background color change. I have attempted to use transitions but they do not seem to work for block elements. For the code and JSFiddle, please visit this link. ...

Create an image on a node's backdrop using a library of graph theory/networking techniques

I have a set of data that I need to visually represent as a graph on a web browser. While creating the graph itself is not an issue, I am looking to dynamically draw unique icons for each node. These icons are specific to the characteristics of each node ...

The Facebook comment section is taking control

<div class="fb-comments" data-href="https://www.facebook.com/pages/Societ%C3%A0-Ginnastica-Triestina-Nautica/149520068540338" data-width="270" data-num-posts="3"> I'm having trouble with the comment box extending past my div height and overlapp ...

Using AngularJS to retrieve JSON data with the HTTP module

I am a beginner in the world of angularjs and I need some guidance. Below is the code I have written: <!DOCTYPE HTML> <html ng-app="myapp"> <head> <meta charset="utf-8"> <title>Angularjs project</title> <script type= ...

iOS Safari enlarges frameset beyond viewport limits

I am currently designing a website that utilizes a frameset featuring a horizontal split - a menu sidebar on the left and a content area. <!DOCTYPE html> <html> <head> <title>Frameset Test</title> </head> ...

Issue arised while trying to open Bootstrap modal window due to conflict with surrounding elements

I'm facing a challenge with getting the bootstrap modal window to pop up on my website. Despite trying various solutions and troubleshooting steps, I haven't been able to resolve the issue. Even after eliminating all scripts except for the boots ...

Is your Ajax response suddenly failing to work after the initial attempt?

Describing my predicament: The code snippet below is what I have been using to insert a custom-designed div into my webpage. Initially, the div is successfully added; however, it stops working after the first instance. $('#addanother').click(fu ...

The Zurb Foundation grid system is causing issues when trying to implement vertical tabs

My vertical tabs are giving me trouble when I try to nest grid or block elements, causing everything to overflow outside the element. For example, in the image below, the numbers 1,2,3 should be in the right section. <div class="row"> <div class ...

Is it possible to verify the authenticity of JSON data retrieved from

I'm currently working on validating JSON input from users and came across a challenge. I've found a way to check if the text a user enters is valid JSON using a simple function, like below: function IsJsonString(str) { try { JSON.par ...

A lesson is what I'm seeking, as I face an Uncaught TypeError: Unable to locate the property 'split' of an undefined value

Although this question has been asked multiple times before, I am a beginner and eager to learn. I would greatly appreciate it if someone could take the time to explain this to me. I have tried to find a solution to this error using the existing answers, b ...

When a Vue.js Directive is inserted or bound, it actually takes precedence over the click event

Imagine having multiple dropdowns or elements on the page that all utilize a directive called "closable". This directive triggers an expression if the element clicked is outside of the element using the directive. The intended behavior is that when clicki ...

The mapDispatchToProps function is failing to work, throwing an error: Uncaught TypeError: _this.props.addCountdown is not a function

Currently, I am facing an issue while working on my first app. The problem arises with a form component that should send an object via an onSubmit handler. onSubmit = (e) => { e.preventDefault(); this.props.onSubmit({ title: ...

"Learn the process of extracting information from a database and exporting it to a CSV file with Angular 2

Currently, I am facing an issue where I need to retrieve data from a database and export it to a CSV file. While I am able to fetch all the data successfully, I am encountering difficulty when trying to fetch data that is in object format as it shows up as ...

Is it possible to create a secondary <li> dropdown menu using JavaScript or jQuery from a PHP-generated dropdown menu?

Using a foreach loop to query the database, I have created a drop-down menu. Now, what I want is that when I select a subitem from the menu using JavaScript, I can retrieve the value and make an Ajax request to fetch all the values corresponding to the su ...

Utilizing JavaScript to implement conditional if-else statements on CSS properties

I am trying to implement conditions on CSS properties in JavaScript. What is the most efficient approach to achieve this? <html> <head> <title>TOOLTIP USING JAVASCRIPT</title> <style> span{ curso ...

deleting the current product information during an ajax request

After executing the query provided below, I have $product_list in this code. However, I want to use ajax so that when I click on the button link1, $product_list gets emptied. How can I clear the content within products_list using an ajax call triggered by ...

Attempting to scroll through a webpage and extract data using Python and Selenium in a continuous manner

Recently, I posed a question (you can find it here: Python Web Scraping (Beautiful Soup, Selenium and PhantomJS): Only scraping part of full page) that shed light on an issue I encountered while attempting to scrape all the data from a webpage that updates ...