Tips for ensuring a minimum width and line break for contenteditable elements simultaneously

When creating a text with a contenteditable area, I encountered an issue: I wanted the contenteditable area to have a minimum width so that users could still see the area even after deleting text. To achieve this, I used display:inline-block. However, the contenteditable area does not break to the next line; it only expands. When I remove display:inline-block, the contenteditable area breaks to the next line when reaching the end of the line, but then I lose the ability to set a min width, resulting in the text disappearing when deleted by the user and requiring a page reload. How can I achieve both functionalities?

span.a{
    
       display:inline-block;
       min-width:20px; 
       border:1px solid black;
   
   }
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
      Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, <span  class="a" contenteditable="true" >AAAAAAAAAAAAAAAAAAAAA </span> took a galley of type and scrambled it to make a type specimen book. 
      It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 
      It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
      and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    

Answer №1

Here's a clever suggestion: try keeping the element inline and utilizing the :empty pseudo class to apply padding when there is no content present. By doing this, you can ensure that the area will always remain visible:

span.a {
  border: 1px solid black;
}
span.a:empty {
  padding-left:10px;
}
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, <span class="a" contenteditable="true">AAAAAAAAAAA AAAAAAAAAA </span> took a galley of type and scrambled
it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

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

Transitioning to EM-Based Media Queries

After the recent resolution of the WebKit page-zoom bug, what are the primary benefits of utilizing em-based media queries over pixel-based ones? Incentive I am offering a reward for my question as many prominent CSS frameworks and web developers use em- ...

Eliminating the GWT Window Padding in LibGDX

Explanation: I am currently in the process of deploying a LibGDX GWT application to the browser. Challenge: In the image below, you can see the top left portion of my GWT application showcased by the large black rectangle. The issue here is the unwanted ...

Customize the position of carousel controls in Bootstrap 4

I currently have a straightforward bootstrap 4 (beta-v2) carousel set up: <div id="carouselSteam" class="carousel slide" data-interval="false"> <div class="carousel-inner"> {% for key, value in results.items %} {% if fo ...

GWT-dropdown design

I decided to explore the SuggestBox in GWT, so I experimented with the code provided on this website. However, I am unsure of how to connect the style with the SuggestBox. I am utilizing uiBinder... And in the class, I have included the line of code: ac ...

Show the entire webpage in an iframe with specific dimensions

Greetings, fellow internet users! I'm currently facing a challenge involving displaying a website within an iframe. My goal is to have the iframe show the entire screen of the website without any scrollbars. So far, the only solution I've found ...

You can only import Next.js Global CSS from your Custom <App> and not from any other files

Initially, my React App functioned perfectly with global CSS included. However, after running npm i next-images, adding an image, modifying the next.config.js file, and executing npm run dev, I encountered the following notification: "Global CSS cannot ...

Sticky table header with scrolling body

I am trying to display a scrollable table list with a fixed header, but I'm having issues with the header content not displaying properly without word wrapping. Additionally, there seems to be a 1px gap above the sticky header when scrolling through t ...

Tips for maintaining a single-line div while adding ellipses:

What is the CSS way to ensure that a div or class contains only one line of text, with ellipses added if it exceeds that limit? I am aware that setting a specific height and using overflow:hidden can achieve this, but it doesn't quite work for my need ...

Is there a way to create a Swipe slideshow without relying on plugins or external tools?

How can I create a responsive swipe image slideshow using CSS and JQuery that changes the image when swiping from right to left? I want it to work seamlessly on both computer and mobile screens. ...

Ways to center-align this menu (Spanish language)

I am working on optimizing the alignment of the menu on my website: My goal is to center the menu, as the English version looks good. #site-navigation { width: 980px; margin: 0 auto; text-align: left; } After analyzing the code, we have iden ...

JavaScript is unable to identify the variable containing parsed JSON

I have been working on coding some gauges for the index page of the R/Shiny dashboards used by our company. The layout consists of several styled divs that link to different R apps. Recently, our executives have shown interest in having fuel-gauge style g ...

What is causing the collapsed-animation to malfunction in Vue3?

Why won't the transition animation function in vue3js? The animation does not appear to be working for me. I've implemented this library https://github.com/ivanvermeyen/vue-collapse-transition <template> <nav class="navbar color-d ...

Using JavaScript to close a menu when clicking outside of it

I am currently working on enhancing a menu with a basic functionality. The goal is to toggle the menu between showing and hiding when a button is clicked, managed by a CSS class using JavaScript. However, I have encountered an issue when attempting to com ...

Looking for assistance with CSS | Implementing hover image with zoom effect

Currently, I have a page created using DIVI WordPress: My issue arises when attempting to implement a zoom effect on images upon hovering, as the image exceeds its designated div area. I attempted to utilize CSS code from this source: https://codepen.io/ ...

Split the pane into divisions with a minimum width restriction

Is there a way to have two divs at 50% width, even if the content of the first div has a minimum size? <div class="col-xs-6"> <div style="min-width:1000px"> <label>The value here...</label> </div> </d ...

In this guide, we will explore the process of designing unique styles for ng

What is the proper way to customize CSS for a specific element? &.ng-select-focused { &:not(.ng-select-opened) > .ng-select-container { border-color: $ng-select-highlight; box-shadow: $ng-select-box-shadow; } } The offi ...

Issue with top-level navigation menu dropdown functionality

I am experiencing an issue with a dropdown menu that has top level and two sub levels. The problem is that while the sub levels function correctly when clicked, the top level does not navigate to the intended page upon clicking. var menu_Sub = $(".men ...

Configuring Dialog Placement to the Top in Material-UI

I'm in the process of creating a popup dialog, but I'm encountering an issue where the dialog pops up in the center of the page. How can I position it at the very top of the page when the popup button is clicked? Below is the code snippet: < ...

How to vertically align a div inside another div

My Goal: Ensuring that the divs with "col-md-1" (the vote total and vote arrows) are vertically aligned in the center of the parent div HTML Structure: <div class="row container-fluid"> <div class="col-md-1 centered-div"> <p id ...

What could be the reason for the malfunctioning dropdown menu in my navigation bar upon clicking it?

After spending hours practicing creating a navbar using Bootstrap 3.3.7, I've hit a roadblock - the dropdown is not working when clicked on. It's frustrating because I have double-checked all my scripts and ensured that I have the latest version ...