Editable content within a div will reposition when there is no content present

Take a look at this example: http://jsfiddle.net/nojv18wf/

<div>
    <div class="display">Blah blah blah?</div>
    <div class="input" contenteditable="true"></div>
</div>

<br /><br /><br /><br />

<div>
    <div class="display">Blah blah blah?</div>
    <div class="input border" contenteditable="true"></div>
</div>

Observe how the cursor behaves when you click on "blah blah blah."

In the first scenario, it moves to before the blah blah blah, but once you start typing, it then appears in the correct position.

However, in the second case with the border, it functions correctly from the beginning.

Is there a way to make the cursor show up in the right place without resorting to adding an unsightly border?

UPDATE: It seems this issue is only present in Chrome; everything works fine in Firefox and even IE.

UPDATE 2: I am using inline formatting to ensure that the text wraps smoothly as if it were a seamless continuation, rather than wrapping the entire block. For example: http://jsfiddle.net/khkurs5t/

Answer №1

If you want to ensure your input text stays on the same line, try adding a transparent border like this: Check out this Fiddle for an example.

border: solid 1px transparent;

This simple trick works in most browsers, except guess who doesn't play nice... IE. But don't worry, it's all good in other browsers. Best of luck!

Answer №2

Combining the inline-block layout with min-width can be a useful technique.

.box{ display: inline-block; min-width: 1px;}

Try it out on JSFiddle

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

Incorporate Bootstrap into your Bigcommerce stencil theme for seamless design integration

As a newcomer to BigCommerce's Stencil theme, I am looking for guidance on integrating Bootstrap into the theme for local development. Is it feasible to incorporate custom CSS and JS into the Stencil theme as well? ...

Utilize separate CSS files in React components for better organization

Hello, I have encountered an issue with splitting my components and using react-router-dom. I decided to split the CSS for each component, such as Main.jsx - Main.css and CustomerBase.jsx - customerbase.css. However, the problem I am facing is that the CSS ...

Python button Selenium

I am new to using selenium and need assistance with clicking a button located between button tags. I attempted to use find_element_by_tag_name, but it did not work because there are other button tags scattered around before. If I try by_class_name or by_xp ...

The alignment of the HTML and CSS speech bubble is experiencing issues

I need help with CSS as I am new to it and trying to create a speech bubble. However, when the text inside the bubble is long, it overlaps the timestamp. I am currently using `display: inline-block;` in my CSS. Below are snippets of my HTML and CSS code: ...

Add additional text using the ::after pseudo-element exclusively for a particular class

Here is the HTML code snippet I am currently working with: <style> p::after { content: "%"; } </style> <div class="counter-box"> <p data-value="185">100</p> </div> At present, my CSS applies text injection to ...

Looking for a solution to a problem with your vertical CSS/jQuery dropdown menu

My web app features a vertical CSS menu that is working correctly except for one minor issue. When I mouse out on all elements with the class a.menutoggle, the last dropdown menu remains open. I am struggling to find a solution to hide it. Can someone plea ...

What are some creative ways to customize the aesthetics of Angular UI pagination?

Currently, I am utilizing Angular UI 2.5.0 for pagination on my website. I am interested in customizing the look of the pagination bar to resemble the design shown in the image below. Current Pagination Style: https://i.sstatic.net/Z6D0Y.png Desired Cus ...

Using a variable to store the value of the id attribute in HTML code

How can I dynamically add an ID attribute to an HTML element using a variable declared in JavaScript? Using jQuery var table_row = $('table').find('tr#' + pid); var name = table_row.find('td:nth-child(1)').html(); table_ ...

How can I ensure that my image remains responsive and aspect ratio-friendly?

Is there a way to accomplish this magical effect? I find myself in a similar situation. I require an image that remains centered while adjusting to fit the screen, all while maintaining its aspect ratio. ...

Troubleshoot: Why is my Bootstrap 3 responsive navigation bar not functioning

When using drop down items that are longer in length, they tend to wrap down to the next level rather than overflowing. I am not well-versed in @media queries, but I've noticed that on mobile devices the dropdown collapses in the navigation menu, whil ...

What is the relationship between three.js transforms and CSS3 3D-transforms?

I am developing a unique open-source tool for exploring and visualizing the complexities of human anatomy. At the center of this tool is a dynamic 'chessboard' that occupies the majority of the screen. As you drag the board, various CSS3 3D-tran ...

Tips for updating existing data with Angular JS?

I'm looking to edit a posted JSON data form record by clicking a button, but I'm having trouble populating the form with the old data for editing. <button style="margin:3px;" ng-click="put1(student)" onclick="opendiv();">Edit</button> ...

Tips for stopping Razor from altering HTML strings

In order to display the body of a message, which is a piece of HTML, using Razor, follow these steps: Within your cshtml file, include the following code: <div> @message.Body </div> For example, if the content of message.Body is: <p&g ...

Struggling to make an AJAX form function properly

I'm running into an issue while setting up a basic AJAX form. My goal is to have a message display on the same page upon successful login using a PHP form through AJAX, but currently, I get redirected to the PHP file after form submission. Can anyone ...

Prioritize the first child in Flexbox by truncating its text before the second

I am working with a flex-box container named .search-bar, which contains two divs with text, labeled as .title. My goal is to make the second child take up any extra space available and only start ellipsizing when there is no more space left for the first ...

Using Multiple SCSS Files to Reference a Centralized Style Sheet

I am currently developing a React application and facing an issue with my SCSS files. I have three SCSS files, one main file that contains theme variable colors, and the other two are located in component folders. When I import the main SCSS file into the ...

Devices are not displaying media queries as expected

@media screen (max-width: 750px) or (screen and (max-width: 750px) (-webkit-min-device-pixel-ratio: 2),(min-resolution: 192dpi)) { div.body, div.body div.links, div.body div.links div, div.footer, div.footer div { display: block ...

What is the best way to hide or show child elements within a tree structure using a toggle function

My HTML code needs to be updated to include a toggle span and JavaScript functionality. This will allow the child elements to be hidden and only displayed when the parent is clicked. I am a beginner with JavaScript and would appreciate any help in resolv ...

Determine the elapsed time in seconds between two specified moments

I am trying to implement two input fields in my HTML, one for a starting point and another for an end point. The user will enter two times like this: For example: [8:15] - [14:30] alert("XXXXX seconds") I want to calculate the number of seconds between 8 ...

Can the functionality of a button be disabled after being clicked a total of 5 times?

Once a user clicks the button five times, I plan for it to be disabled. Can this be achieved solely with HTML, or would JavaScript be necessary? ...