Placing the cursor in front of a non-editable display block element using HTML5's contenteditable feature

Currently, I am faced with the challenge of inserting a span into a contenteditable element to act as a tag or separator. Here is my code snippet:

.tag {
  display:block;
  background-color:red;
}

.edit-box{
  border: 1px solid black;
  padding: 10px;
}
<div id="test" contenteditable="true" class="edit-box">
    <span class="tag" contenteditable="false>NON EDITABLE</span>How do I style the span element so that the cursor can be placed at the beginning?
</div>

My goal is to be able to modify text both before and after the span element, but currently, it's challenging to position the cursor right before the element. Any suggestions on how to style the span element for improved cursor flexibility?

Check out this FIDDLE link

Answer №1

Apply the following CSS styles:

.label {
  display: inline-block;
  width: 100%;
  background-color: red;
}

Answer №2

Consider suppressing events to edit the span content. I found it necessary to separate the span and div elements, although they function correctly in IE11 when combined. It seems like there may be an issue with interception in other browsers. However, the concept is clear and all you need to do is adjust your styling to display it as before.

function handle(e)
{
if (e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40 && e.keyCode != 37)
return false;
}
.tag {
  display:block;
  background-color:red;
}

.edit-box{
  border: 1px solid black;
  padding: 10px;
}
<span class="tag" contenteditable="true" onkeydown="return handle(event)">NON EDITABLE</span>
<div id="test" contenteditable="true" class="edit-box">How to style the span element so it's possible to place the cursor before?
</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

Combining Bootstrap, flexbox, and a mobile-friendly hamburger menu for optimal

Just a quick note to clarify that I am primarily a backend developer, not proficient in frontend. The layout I currently have looks like this: I am unsure whether I should stick with the current setup or switch to using a grid. My main goal is to have a ...

What advantages does Snap.svg offer compared to the HTML5 native SVG DOM API?

Can the differences between Snap.svg and HTML5 native SVG DOM API be compared to jQuery vs HTML5 native DOM? Apart from potential cross-browser disparities, assuming modern browsers support SVG well, what advantages does Snap.svg offer? What are the pros ...

What's causing my CSS layout to get disrupted by this PHP script?

This website utilizes the $_GET method to extract an asset ID and query a MySQL database for information retrieval. If the 'id' does not correspond to any record, no results are displayed but the page layout remains unaffected. However, if &apos ...

The alignment issue with Bootstrap4's align-self-baseline

Is there a way to attach tag buttons to the baseline of the parent div using CSS? If this method is not appropriate, can you suggest an alternative approach? <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap ...

Try utilizing the adjacency selector in Less again

Is it feasible to repeat this pattern an unlimited number of times using Less with the provided CSS selectors? I've been looking into using loops, however, I haven't come across any examples that achieve a similar result. .page-section { ba ...

Applying ngClass to handle positive and negative numbers and adjust color in Ionic/Capacitor

I have data from my API that includes price and percentage data. I want to display negative numbers in red, and zero or positive numbers in green. After exploring Angular documentation, I found that ngStyle and ngClass can be used for this purpose. I chose ...

Utilize JavaScript to trigger a function depending on the selected options from dropdown menus

I've been working on a fun little project for a web page where users can select two items from a drop-down menu and then click a button to play a sound corresponding to their selections. However, I'm facing some challenges with getting my JavaScr ...

Issue with IE failing to load a particular Stylesheet

Have you had a chance to view this live demonstration? My website has two different stylesheets, one specifically for Internet Explorer and another for regular browsers. Here's how they are set up in the header: <link rel="stylesheet" type="text/c ...

Steps to keep only one submenu open at a time and close others

Struggling to figure out how to open one submenu at a time, and I can't seem to locate the issue! I've searched around for solutions but haven't found anything that works for me. The submenu also needs to remain closed when the page loads. - ...

Move one div to reveal another underneath

I currently have a div housing various products that can be added by clicking the add link. Whenever the add link is clicked, an information message appears briefly before fading out after 4 seconds. This message shows up at the top of the childbox-conten ...

Taking pictures with the camera in three.js on an iPhone

While working on a project that required capturing the camera video stream as a texture in three.js for mobile phones, I encountered an issue. The code I was using ran smoothly on Android devices but not on iPhones. Ideally, when the user grants permission ...

There appears to be an error in the @media CSS code that needs to be corrected

Attempting to use an @media query to adjust the height of an element on my website for mobile users. However, I am struggling to make the code work properly. Any assistance in understanding what mistake I'm making and guiding me in the correct directi ...

Ways to utilize media queries for repositioning one div on top of another div

Utilizing Bootstrap 5 for website development, my aim is to enhance the responsiveness of the site across various devices. For mobile users, I envision the image-containing div to appear above the div containing the header and paragraph content, but I&apo ...

Customize dropdown item colors in React using a color picker

I am currently utilizing a react color picker to create a personalized 16 color gradient. The process involves selecting a color from a dropdown menu and then using the color picker to make adjustments. This action updates an array that dictates the stylin ...

What could be causing flickering when animating CSS translate on a black background in WebKit?

Upon viewing the jsfiddle, it's evident that this animation experiences flickering in webkit browsers. Regardless of whether the animation is set to repeat infinitely or not, the issue persists. How can this problem be resolved? I've spent hours ...

What is the best way to enable horizontal scrolling for textarea overflow in a smooth manner like input, without any sudden jumps?

Currently, I am interested in utilizing a one-line textarea (with rows=1 and overflow-x:hidden;) similar to input type="text. However, I have encountered an issue where the content scrolls with "jumps" while typing inside it: https://i.stack.imgur.com/Rzf ...

`CSS Toggle Smooth Transition Effect`

I am working on creating 2 CSS toggle buttons that currently have a fade in and out effect when clicked. I would like them to have a sliding animation from right to left instead. Below is the HTML, CSS, and a link to a fiddle containing my code. Thank yo ...

Creating sitemaps for multi domain websites using NextJS

We are implementing a next-sitemap package to generate sitemaps for our Next.js pages located in the /pages directory. For pages that come from the CMS, we use server-sitemap.xml with SSR. Despite having 6 different domains, we manage them within a single ...

Utilizing Compass SCSS with Blueprint for Optimal Overflow Settings

I've been working with blueprint through Compass, but I'm facing an issue where longer pages don't have scroll bars. It seems like the default setting for overflow is 'hidden'. Since this appears to be the standard, I assume there ...

Executing JavaScript code upon clicking a menu item involves creating event listeners for each menu

Currently, I am working on a Squarespace website that includes a navigation bar. I am looking to incorporate a JavaScript code as a link within the navigation bar. The specific JavaScript code is as follows: <div> <script type="text/javascript ...