CSS - What's the best way to create a dynamic resizing input box in web design?

How can I create an input box that automatically wraps text and shrinks to fit its size? What CSS property should I use for this?

I attempted to use width: auto to scale it, but it didn't work as I expected.

Answer №1

The most effective method to achieve this task is by utilizing contenteditable attribute within a div element; otherwise, you would need to depend on JavaScript for implementation

const myinput = document.querySelector('.myinput');
document.querySelector('#mybutton').addEventListener('click', () => {
  alert(myinput.innerHTML);
})
.myinput {
  display: inline-block;
  padding: 5px 10px;
  border-radius: 3px;
  background-color: #e5e5e5;
  min-width: 10px;
  max-width: 400px;
  height: 22px;
  line-height: 22px;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
}
<div contenteditable="true" class="myinput"></div>
<button id="mybutton">Get input value</button>

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

What is the best way to make ng-style append to a pre-existing style?

I am facing an issue with my custom popover directive and another custom directive that uses it. I am attempting to use ng-style to adjust the width of the popover. Below is a code snippet from the directive's html template: <div my-custom-popover ...

Is there a way to center li elements evenly on mobile devices, regardless of the text length?

Take a look at these two images showcasing my website on different devices: Mobile: https://i.sstatic.net/spsYj.png Desktop: https://i.sstatic.net/AvFiN.png I have organized the content using simple ul and li tags. How can I adjust the layout so that ea ...

Having difficulty swapping out images on an HTML website template for Internet Explorer

Recently, I obtained an HTML website template that I am currently working on customizing. One of the tasks I have been tackling is replacing some of the existing images with my own. After resizing the new images to match the dimensions of the originals, ...

What is the best method for retrieving text that does not contain tags (such as text

My challenge involves storing headings and paragraphs into separate arrays, but I am struggling with handling text between <br>. Below is my HTML code and Python snippet: <p><strong>W Ognisku</strong><br>W londyńskim Ognisku ...

The layout of Bootstrap 5.0 features an even number of columns in the ratio of 4-2

Is there a way to have an even number of columns (4, 2 or 1) in my bootstrap row without going through 3 when resizing the browser width? ... <div class="row row-cols-2"> <div class="col-sm"> ... </ ...

Concealing and wrapping text using CSS in a table structure

My table is displayed below: <table style="width: 100%; white-space: nowrap; overflow: hidden;"> <tbody><tr> <th> Department </th> <th> Function </th> ...

Is it possible to create a similar design that is also responsive?

I've been struggling to create a header for a website project I'm working on. My goal is to design something like the image below: https://i.sstatic.net/szQGf.jpg The biggest challenge I'm facing is making this design responsive. The point ...

Tips on Utilizing If/Else Conditions in HTML Table TD_TAGS

Can someone please explain how to implement an if/else condition within this <td> tag? <td data-label="Status">{{$t('Status'+item.cStatus)}}</td> This is what I am trying to achieve: if(item.cStatus == NW){ color:red ...

iOS displaying CSS Transform issue exclusively

Creating a website that is fully mobile responsive is my goal. However, after designing the header, I realized that it wasn't displaying correctly on IOS devices like my iPad. The "Transform: translateX(-100%);" statement was functional on Android pho ...

Another component's Angular event emitter is causing confusion with that of a different component

INTRODUCTION In my project, I have created two components: image-input-single and a test container. The image-input-single component is a "dumb" component that simplifies the process of selecting an image, compressing it, and retrieving its URL. The Type ...

Jquery for controlling the navigation menu

I'm new to JavaScript and facing 3 challenges: 1. When I click on the parent <li>, the correct content of the child <sub> does not show up. Each category like "colors, shapes, sizes" should have corresponding child categories like "green, ...

I need to print out a table, but when I preview the print, I want to rotate the entire page for better visibility

Is there a way to rotate the print review using CSS and HTML? I am facing issues as the CSS styles do not seem to apply properly on it. Can someone help me solve this problem? ...

Is there a way to generate inline block elements that occupy the full 100% width? Furthermore, can the second element contain an additional element that is revealed upon

Currently, I am working on a project that involves displaying images with text overlays. The text overlays are designed as inline blocks with varying background colors. One issue I encountered is that there is unwanted whitespace between the background co ...

Prevent drag and drop functionality in QtWebkit

As I work on creating an HTML GUI for my application using Qt's WebKit, everything is going smoothly with one minor issue. I am trying to prevent users from dragging and dropping images from the GUI itself. While I have successfully disabled text sele ...

Using CSS on a randomly selected div that is chosen after dividing the main div each time it is clicked

Imagine a square box displayed as a "div" element. When you click on it, it splits into an n x n grid with each square having a random background color. However, the issue I am encountering is that I want to apply additional CSS to each of these randomly c ...

Is it possible for a CSS Transition/Transform to ease out smoothly yet not ease in at all?

It appears that the transition is smoother when I stop hovering over the button, compared to when I actually hover. Why might this be? .button_1 { border-width: 0.8px; border-color: Lightgray; background-color: hsla(209, 72%, 59%, 0.85); ...

Enhance your web design with the latest Bootstrap 5.2

I attempted to implement a tooltip using jquery in my jquery datatable, but unfortunately the tooltip is not appearing. Below is the code snippet: render: function (data, type, row, meta) { var total = row.success + row.error; if (row.isConsumed = ...

steps for eliminating specific words from an input field

In my form, I have 13 input boxes where users can only type text and numbers. I am using the following script to enforce this rule: RESTRICT INPUT TEXT $(function() {//<-- wrapped here $('.restrict').on('input', fun ...

Tips for adjusting the Bootstrap grid layout for mobile viewing

As a beginner in web design and utilizing bootstrap, I am creating a website with two columns of size 6 each within a row. || Col-1 || Col-1 || [Web View] ||||||||||| || Col-1 || ||||||||||| || Col-2 || ||||||||||| [Mobile View] However, I would like th ...

Is there a way to trigger an image flash by hovering over a button using the mouseover feature in AngularJS2?

When I hover over the 'click me' button, I want an image to appear on the webpage. When I stop hovering, the image should disappear using the mouseover option. This is what I attempted in my app.component.ts and my.component.ts files: Here is t ...