Tips for restricting the size of inserted content in a contentEditable paragraph

I am in need of a one-line editable paragraph that can contain text without overflowing its size.

Currently, I am using CSS to hide the overflow and prevent multiple lines by not displaying the children of the paragraph.

However, what I really want is for the text within the paragraph to be cut off as soon as it exceeds the allotted space.

Below is the code I have come up with so far (also available on JSFiddle):

div
{
  width: 50vw;
  height: 50vh;
  margin: 10px auto;
  background-color: gray;
}

p
{
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 25%;
  background-color: brown;
  text-align: center;
  
  white-space: nowrap;
  overflow-x: hidden;
}

/* Avoiding multiple lines */
p *
{
  display: none;
}
<div>
  <p contentEditable="true">
    Edit me! Unfortunately, I can overflow... :(
  </p>
</div>

Answer №1

To solve this issue, you can utilize the word-break: keep-all; property.

div {
  width: 50vw;
  height: 50vh;
  margin: 10px auto;
  background-color: gray;
}

p {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 25%;
  background-color: brown;
  text-align: center;

  word-break: keep-all;
  white-space: normal;
}

p * {
  display: none;
}
<div>
  <p contentEditable="true">
    Edit me! Unfortunately, I can be overflowed... :(
  </p>
</div>

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

Web addresses and the presence of secure HTTP protocol?

Can someone confirm if it is true that when using https, URLs should begin with //[your-domain]/? A friend using WordPress mentioned this but I haven't been able to find specific information on the topic. ...

The Bootstrap modal window refuses to shut down

In my React application, I am utilizing Bootstrap modal functionality. One specific requirement is that the modal window should remain open when clicking on the modal backdrop. To achieve this, I have implemented the following code: $(".modal").modal({"ba ...

Handling right-click events with jQuery live('click')

There has been an interesting observation regarding the functionality of the live() function in jQuery: <a href="#" id="normal">normal</a> <a href="#" id="live">live</a> $('#normal').click(clickHandler); $('#live&ap ...

Patience is key as you wait for the observable to finish

My methods have dependencies where one method needs to complete before the next can be called. process1(data: string) : Observable<string> { this.dataservice.process(data).subscribe( (response) => { return response. ...

Randomly injecting strings like 'jQuery111201xxx' into a string using jQuery Ajax

After implementing a booking system that utilizes FullCalendar, I encountered an unusual issue with the 'notes' field. Occasionally, a strange string is inserted into the notes field at random points. Here's an example of what I found recent ...

Adapting npm scripts with Node.js based on the current context

Can you set up package.json to execute a different npm start script depending on the context? For instance, I want to run DEBUG=http nodemon app.js during development. However, I prefer to run node app.js in production. ...

How to delete a specific element from a JSON object using Node.js

I've been trying to figure out how to remove a specific ID from my ids.json file, but I can't seem to get it right. Here's the block of code that I've been working with: { "48515607821312": { "members": [ "23422 ...

Creating a full-height layout in Bootstrap with a sticky footer

Encountering a minor issue with Bootstrap as I attempt to cover the entire height of the browser window when the content is insufficient. Utilizing Bootstrap within Orchard CMS, in case that impacts the situation. The problem is illustrated in the image b ...

Is there a way to retrieve the index and specific object that was modified using $watch in Angular?

I am receiving an array of objects from an API that is being updated every 2 seconds. I have a $watch function that is monitoring any changes in this array. I want to be able to identify which elements have changed along with their index, so that I can dyn ...

How to Build Node 0.4.7 on Ubuntu 11.10?

Having trouble compiling 0.4.7 for Heroku support as I am unable to enable ssl support required by express. I've tried installing libssl-dev and even attempted manual installation of openssl, but no luck so far. Any suggestions on how to get node up a ...

Navigating the process of returning a list from an AJAX method to a view through a controller in ASP.NET MVC

I need to retrieve a list from my controller and pass it to the view. Currently, I have the following script: function fetchNames(_id, _name) { $.ajax({ type: 'Post', url: 'GetNames/', data: { id: _id, name: _name}, su ...

Instructions on utilizing Bootstrap Tags Input

I'm having issues with implementing the Markup from in my project. .bootstrap-tagsinput { background-color: #fff; border: 1px solid #ccc; box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); display: block; padding: 4px 6px; color: #555 ...

Filtering function that works without specific knowledge of keys

I'm working on a JavaScript method to filter a list dynamically without knowing the specific key (s). I've made some progress, but I'm stuck and not sure how to proceed. The foreach loop I have isn't correct, but I used it for clarity. ...

SignalR blocking axios requests (In a Waiting State)

I am currently developing a project that involves bidding on products in Auctions with real-time updates of the bid price. With potentially thousands or even millions of users worldwide participating in the bidding process. We have implemented SignalR for ...

Issue with readonly is preventing the ability to alter the font color of the input

I need to change the font color of a disabled input. When it is disabled, it appears gray and I want it to be black instead. I attempted to use readonly but that did not have the desired effect, and now the input is showing [object Object]. Below is my HTM ...

Protecting website pages on both the admin and user side in Next.js 14 to ensure maximum security

I'm currently using nextjs 14 and I am working on developing a website similar to a property app. It will have an admin dashboard and a user side. How can I ensure the security of both the admin and user sides, and what should my folder structure look ...

I'm struggling with my project called "Number TSP" and I can't seem to figure out why it's not working properly

Upon reaching the end of my code, I am encountering an issue where instead of seeing the expected output of "Done!", it displays undefined. This is the code in question: const container = document.querySelector(".container") const table = document.querySe ...

"Create dynamic tables with AngularJS using ng-repeat for column-specific rendering

I have a model called Item with the following structure: {data: String, schedule: DateTime, category: String} I want to create a report that displays the data in a table format like this: <table> <tr> <th>Time Range</th&g ...

Is there a way to conclude an Inquirer prompt depending on the response received?

My current project involves creating an application that presents a series of questions to gather information and generate an employee profile. This application is designed to be recursive, meaning that users will have the option to exit and terminate the ...

Tips for changing the height of the arrows in a Bootstrap carousel

After adjusting the height of the .well class, something unexpected happened: https://i.sstatic.net/75KCB.jpg Despite my attempts to change the height for various controls and elements within the carousel, including the carousel itself, I was not seeing ...