Expanding container width as input fields are dynamically added

I have a tab with a title. I would like it to become editable when the user double clicks on it. Currently, I hide the div containing the title and replace it by adding an input element dynamically. However, when I add the input element dynamically, the size of the header-wrapper div increases. I want the dynamically added input to take the same size as the hidden title element. Additionally, I want the size of the wrapper div to remain consistent. I attempted a solution using JavaScript, but now I am seeking a method that solely relies on CSS. Is it possible to achieve this use case using CSS alone? My goal is for the header-wrapper to be flexible rather than having a fixed width. I simply want the input to match the width of the div with the class "head". The input should share the same width and size as the head element.

function dblClickHanlder(){
  console.log("dbl click handler called");
  let inputEl = document.createElement("input");
  inputEl.style.display="inline-block";
  let headEl = document.querySelector(".head");
  headEl.style.display="none";
  document.querySelector(".header-wrapper").appendChild(inputEl);
  inputEl.focus();
}
* {
    box-sizing: border-box;
}
.wrapper{
  border: 1px solid black;
  display:flex;
  flex:1 0 auto;
}
.header-wrapper{
  display: flex;
  border: 1px solid red;
}
<div class="wrapper">
  <div class="header-wrapper">
    <div ondblclick="dblClickHanlder()" class="head">Heading</div>
 </div>
</div>

Answer №1

To retrieve the previous width and height of the head element before removing it, and then set those values to the newly created input:

  inputEl.style.width = headEl.offsetWidth + "px";
  inputEl.style.height = headEl.offsetHeight + "px";

For reference, see the code snippet below:

function dblClickHanlder(){
  console.log("dbl click handler called");
  let headEl = document.querySelector(".head");
  let inputEl = document.createElement("input");
  inputEl.style.width = headEl.offsetWidth + "px";
  inputEl.style.height = headEl.offsetHeight + "px";
  headEl.style.display="none";
  document.querySelector(".header-wrapper").appendChild(inputEl);
  inputEl.focus();
}
* {
    box-sizing: border-box;
}
.wrapper{
  border: 1px solid black;
  display:flex;
  flex:1 0 auto;
}
.header-wrapper{
  display: flex;
  border: 1px solid red;
}
<div class="wrapper">
  <div class="header-wrapper">
    <div ondblclick="dblClickHanlder()" class="head">Heading</div>
 </div>
</div>

Answer №2

To apply the desired styling, simply include the following CSS class:

.header-wrapper input {
  max-width: 50px;
}

Here is a demonstration of how it works:

function doubleClickHandler() {
  console.log("Double click handler called");
  let newInput = document.createElement("input");
  newInput.style.display = "inline-block";
  let headerElement = document.querySelector(".head");
  headerElement.style.display = "none";
  document.querySelector(".header-wrapper").appendChild(newInput);
  newInput.focus();
}
* {
  box-sizing: border-box;
}

.wrapper {
  border: 1px solid black;
  display: flex;
  flex: 1 0 auto;
}

.header-wrapper {
  display: flex;
  border: 1px solid red;
}

.header-wrapper input {
  max-width: 50px;
}
<div class="wrapper">
  <div class="header-wrapper">
    <div ondblclick="doubleClickHandler()" class="head">Title</div>
  </div>
</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

Fixed background & cover design for Internet Explorer 11

Visit this link for more information: http://apolytos.com/new/img/test.html <!doctype html> <html> <head> <meta charset="utf-8"> <style> body { background:url("background.jpg"); background-repeat:no-repeat; backgr ...

How to properly align a form with a multi-lined label in HTML and CSS?

I'm looking to create a form label that spans multiple lines and align the inputs with the labels. The goal is for the "Releasse Date" label to display as: Release Date (YYYY-MM-DD): [input box] Here's how it currently looks: https://i.sstatic ...

CSS: Determining the Best Time to Utilize Display Flex Versus Display Inline-Block

https://i.sstatic.net/SHfd0.png Hey there! I'm looking to create a layout similar to the one shown above. I'm using Bootstrap4 and I know that I can utilize either display:flex or display:inline-block to achieve this. However, I'm unsure of ...

How can you elevate an HTML element to the front using the "z-index" property or the "append" method?

When working with HTML documents, we often come across various HTML elements like div, p, img, and more. Many of these elements are dynamically created, especially if helpful CSS such as "position: absolute;" is used. After every onClick event, it is nec ...

Having trouble retrieving the selected value from a Django dropdown box with an onChange event?

I am working on a feature where a dropdown selection of titles triggers a specific action upon selection. Here is what I have so far: form.py class CronForm(forms.Form): title = forms.ModelChoiceField(queryset=GlobalFID.objects.all().order_by(&apos ...

Experimenting with incorporating easing effects into jQuery tabs

Could someone please help me implement an easing/fading effect on tabs using jQuery? I am looking to add an easing effect to my current code and also incorporate two CSS classes to change the background color along with the text color. Thank you for any as ...

Tips for adjusting the default width of the container in Bootstrap3

Modifying the default width of a Bootstrap 3 container without causing any alignment issues can be a challenge. The default container width currently set is .container { width: 1170px; } However, I am looking to adjust it to .container { wid ...

What could be causing my transform scale to not function properly in Bootstrap?

As a complete beginner, I am currently in the process of learning bootstrap. I have noticed that the animation breaks when I apply it, specifically the scaling part. Interestingly, the animation works perfectly fine without Bootstrap. I am wondering if the ...

Empty space to the right of the page when viewed on mobile devices

I am currently learning how to develop websites. I am in the process of building a page for practice, but I have encountered an issue. The page is not responsive below 630px width, and there seems to be white space appearing on the right side of the page. ...

Ensuring consistent placement and scrollability of two divs across all screen sizes

I am in need of a solution to fix the top and bottom divs in the given image. The scroll should only occur when there is overflow. <!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1.min.js"></script> ...

When static files are deleted or switched off, localhost:3000 fails to update my CSS or load at all

I've encountered a problem with my Rails project after precompiling assets for deployment on Heroku. Despite following advice to delete the static file, the app now remains stuck in a buffering state and won't load. I've attempted setting co ...

CSS - Struggling to identify the source of unwanted horizontal scrolling?

Recently, I've been attempting to make adjustments to a responsive website that utilizes media queries. Despite my efforts, whenever the site is viewed on a mobile-sized screen, it consistently requires horizontal scrolling regardless of the screen&a ...

Utilize Sass @extend to incorporate imported CSS files in the build process

In my development setup, I have a global CSS file that houses all of the generic CSS styles. My goal is to extend the classes from this global CSS file in any of my SCSS files. However, when I attempt to do so, I encounter an error stating that the .xyz c ...

Strategies for reducing the amount of space between cards in Bootstrap 4

After creating 4 cards using Bootstrap version 4.5, I noticed that the spacing is not right. I'm struggling to reduce the spacing between the cards. Have a look at the image below if you're still confused: https://i.sstatic.net/RrJLL.jpg .car ...

Different ways to customize buttons using CSS

Looking for help with css button styles as a beginner in html. https://i.sstatic.net/LZOlO.png I can comprehend the .button code, but struggling to round the border in red color. Any experts in html and css who can provide assistance? Any guidance would ...

What is the best way to attach a CSS class using an onclick function?

I need to dynamically apply a CSS class to a specific div when clicked using JavaScript. Here is the HTML structure: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv=&qu ...

What is the best way to incorporate background colors into menu items?

<div class="container"> <div class="row"> <div class="col-lg-3 col-md-3 col-sm-12 fl logo"> <a href="#"><img src="images/main-logo.png" alt="logo" /> </a> ...

Rotational transformation in CSS not displaying in Webkit / iPhone browsers, yet functioning properly in Chrome Devtools device toolbar

tl;dr: My CSS transform rotate is not rendering on Webkit / iPhone, but it works in Chrome Devtools device toolbar. Update: I've also opened a separate GitHub issue that is more concise and easier to read, without code excerpts, here. Giving some co ...

Immersive Video Background Taking up the Entire Screen with Content Hidden Below

Currently working on integrating a full screen video background with content positioned below the fold. Successfully implemented the full screen video without any issues. However, struggling to figure out how to add content underneath the video while ensu ...

JavaScript button is not functioning properly to increase or decrease the value in the input field

I'm facing an issue with the javascript increase/decrease buttons on my website. When I assign my JS variable as the class name of the input field, pressing the button results in all input fields being affected simultaneously: https://i.stack.imgur.c ...