Ways to split text across lines within my list

I am currently developing a shopping list app and everything is running smoothly. However, I am encountering an issue with breaking a long string into the next line. Even though I tried using word-wrap, it doesn't seem to work as intended.

For example, when inputting a very long word like

(Helooooooooooooooooooooooooooooooo)
, I would like it to break the word and display it on the next line like

(Helooooooooooooooo
oooooooooooooooooooo)

This is what my HTML looks like:

<div id="container">
    <input id="add" type="text" placeholder="Type new item here"
            autocomplete="off" autofocus/>
    <ul id="item_list">
        <li id="base" class="hidden">
            <input class="check" type="checkbox"> <span class="item">Item</span>
            <button class="delete_item hidden"></button>
        </li>
    </ul>
</div>

And this is the corresponding CSS code:

.item {
  font-size: 15px;
  /* width: 50%; */
  color: #000;
  margin: 8px 0 0 20px;
  word-wrap: break-word;
  word-break: break-word;
  overflow: hidden;
}

If you'd like to see the full and functioning code, feel free to check out this JS Fiddle - http://jsfiddle.net/varunksaini/Zjxq5/10/

Answer №1

It seems like the issue lies within the .item element rather than the input field.

To fix this, you should adjust your CSS with the following changes:

#item_list li {
  list-style: none;
  border-bottom: 1px dotted #ccc;
  text-overflow: ellipsis;
  padding: 10px 5px 10px 50px;
  text-transform: capitalize;
}

.check {  
  float: left;
  margin-left: -30px;
}

.item {
  font-size: 15px;
  color: #000;
  margin: 8px 50px 0 20px; 
  word-break: break-all;
  display: inline-block;
  text-align: left;
}

View a demo at: http://jsfiddle.net/audetwebdesign/hpa87/

The final results will appear as follows:

In the #item_list li section, remove the rules for overflow, white-space, and word-wrap as they are unnecessary.

Do not use float on the .item element.

For the .item element, set display: inline-block (or block), use word-break: break-all to handle long words, and ensure text-align: left is applied (to override any conflicting center alignment).

Note: If you want to avoid the delete button overlapping any text, add right padding or margin to the .item element.

Answer №2

<textarea id="new-item" type="text" placeholder="Enter new item" autocomplete="off" ></textarea>

#new-item{
    overflow:hidden;
}

Answer №3

To fulfill this specific requirement, consider utilizing a textarea element. To ensure it resembles a text box by default, make adjustments to its height.

For added functionality, you may opt to use the following plugin:

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

Styling with CSS3: positioning floating divs on top of a non-floated div

Checkout this fiddle here: http://jsfiddle.net/PA5T5/1/ I've got a setup with 3 divs - 2 are floated whereas one is not. The two floating divs are positioned on top of the non-floating div. I'm wondering if this approach is correct or if it&apos ...

Tips on arranging check-boxes horizontally in this code repository

We are working with an autogen form in PHP, and the way the checkboxes are currently displayed is demonstrated in the following code: case FIELD_CHECK_BOX: $fieldHtml = printCheckbox($mode, $field->getId().'[]', $field->get ...

Modifying the font style of a Flot bar chart

Issue I am facing a challenge with adjusting the small numbers displayed on top of a bar chart created using Flot. It seems like these numbers are generated through canvas, making it difficult for me to customize them to match the styles I have set for th ...

Is there a way to access the font characteristics of a website?

I'm currently in the process of customizing my WordPress theme and I'm looking to adjust the font sizes for certain elements such as pagination. However, I'm finding the style.css file to be quite convoluted and I'm having trouble ident ...

What is the best way to justify Bootstrap 5 navbar items to the right side?

How can you right-align Bootstrap 5 navbar items? In Bootstrap 4, it was ml-auto. However, this does not work for Bootstrap 5. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" dat ...

Achieving left text alignment in CSS for an excerpt using the Ultimate Posts Widget on Wordpress

I've been trying to align the text in the excerpt of a widget called "To-Do List" on the left sidebar. I attempted to set the text-align property to left for the excerpt text, targeting the p tag within the .widget_ultimate_posts, .uw posts using CSS ...

utilizing array.map() to nest multiple layers of arrays

I am facing a challenge with a JavaScript array structure that contains three top-level objects. Each of these objects has an "teamLineup" array, which in turn holds two more objects named "team". These "team" objects have another array called "starters", ...

Prevent the event from bubbling up on active elements

Having trouble with stopping event propagation? Picture this situation: <table id="test"> <tbody> <tr> <td class="row"> </td> <td class="row"> </td> <td class="ro ...

Is it Possible to Ajaxify a Forum Using a Bookmarklet?

My coding knowledge is limited to HTML. However, I am curious about the possibility of creating a bookmarklet that could ajaxify a forum, automatically refreshing and adding new posts to the page. Is such a thing possible? It's frustrating having to ...

Efficiently managing multiple editing forms on a single Rails 4 page

I am working on creating a one-page brochure in Rails 4. I'm looking to edit multiple forms on a single page. How can I accomplish this? I checked out Railscast 346 Wizard form, but it didn't provide the solution I needed. I want something simi ...

problem with displaying sidebar in Ember template

Using Ember, I have a login page where I don't want to display the header or sidebar navigation for my site until the user is authenticated. Once they are logged in, I want the header and sidebar to be visible. Currently, I am achieving this by checki ...

Can the hover effects be disabled when the cursor is in motion?

Is it possible to create menus similar to those in Opera or Chrome browser where hover effects are ignored while the cursor is moving, but activated when the cursor stops at a certain element's position? Here's an example of the menu I want to c ...

Transforming Form Input Fields into a Nested JSON Format with the Power of JQuery

I'm facing a challenge in converting the input fields below into a nested JSON file structure in my HTML page. Despite trying various methods, I haven't been successful yet. These inputs are retrieved through AngularJS ng-repeat. Any assistance o ...

Fetching data from Page 1 and passing it to Page 2 using Javascript

I'm currently experiencing an issue with my AJAX function where it needs to transfer a value from page 1 to page 2 for storage. Let's start with the AJAX function on page one: top.location.href = 'http://www.something.com/redirect.php?emai ...

What is the most efficient way to send an email with all fields from a massive PHP-enabled web form containing approximately 75 fields?

In the past, I have successfully created Ajax-enabled contact forms with around 12 fields. Now, I am facing a new challenge - developing a PHP-enabled web page for job applications that includes approximately 100 fields. My previous method of sending requ ...

The Vue JS application is experiencing an issue with the Node JS (Express) server as it lacks the necessary 'Access-Control-Allow-Origin' header on the requested resource

I've been using Vue.js for the frontend and Node.js (Express) for the backend. Despite trying numerous solutions found online, I'm still unsure about what's causing the issue. Note: I am using the build or production version (dist) of Vue.j ...

Creating two adjacent squares can be achieved by arranging columns and rows in a specific way

Looking to create a website with a gallery block but running into some issues. I only want to utilize Bootstrap along with my custom CSS for the finishing touches. The layout should consist of 2 squares next to each other, with the left square being 2 colu ...

I require the ability to imprint an image using jquery

Can someone assist me with adding an ID to a cloned image using Jquery? I have almost completed the code but I am facing issues defining the ID for the cloned image. Below is the code I have written: //Make element click $(".drag").click(functi ...

Tips for automating dialog box scroll with selenium in Java

I'm attempting to automate the Instagram website, but when I click on the followers link it opens a dialog box with users to follow. However, when I try to scroll within that dialog box, it ends up scrolling the main page rather than the followers dia ...

A step-by-step guide on injecting HTML code dynamically within a Django application

I have a project that involves creating an online encyclopedia. To achieve this, I must generate a webpage for each entry file written in Markdown. Converting Markdown to HTML is essential before uploading them to the site. Instead of relying on external l ...