What are some ways to keep text within the boundaries of a div element?

I have tried multiple solutions for this issue, but none seem to be working for me. When I append a paragraph to a div, the text extends beyond the element. Below is the code I am using. Any assistance would be greatly appreciated.

CSS:

.chat-h {
  margin-left: 7px;
  width: 249px;
  background: red;
}

#chat-p {
  width: 150px;
  background-color: #fff;
  word-wrap: break-word;
}
.msg span {
  font-weight: bold;
  color: blue;
}

JQuery:

function appendMessage(user, message) {
    var container = $("#chat-h");
    container[0].scrollTop = container[0].scrollHeight;
    container.append("<p class='msg' id='chat-p'><span>" + user + ": </span>" + message + "</p>");
}

Answer №1

Check out the text-overflow property

.example-class {
    text-overflow: clip;
}

Answer №2

Implement the following CSS properties to control text overflow:

#chat-p {
  width: 150px;
  background-color: #fff;
  white-space: nowrap; /* or `pre` */
  overflow: hidden;
}

You can also choose to append an ellipsis at the end of the line:

text-overflow: ellipsis;

Check out this example for reference

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

Comparing the syntax of JSON to the switch statement in JavaScript

I recently came across a fascinating post on discussing an innovative approach to utilizing switch statements in JavaScript. Below, I've included a code snippet that demonstrates this alternative method. However, I'm puzzled as to why the alter ...

Comparing tick and flushMicrotasks in Angular fakeAsync testing block

From what I gathered by reading the Angular testing documentation, using the tick() function flushes both macro tasks and micro-task queues within the fakeAsync block. This leads me to believe that calling tick() is equivalent to making additional calls pl ...

How can you align square cells within a container using CSS grids while maintaining a consistent grid gap?

Our objective is to perfectly align square cells with the edges of their container while maintaining a consistent gap between cells in each row and column. Although this Codepen solution is close, there are two key issues: (1) vertical spacing doesn' ...

Using JavaScript/JQuery, change relative or viewport sizes to fixed sizes when the page loads

Wishing you a delightful day. As I work on my website, I find myself relying heavily on viewport units like vw and vh for all measurements such as font size, padding, margin, width, and height. These units provide the flexibility needed to ensure that the ...

Transforming the hue of a radio-button

When it comes to the default CSS code for radio buttons, they appear gray when unselected and blue when selected: https://i.stack.imgur.com/hsazr.png However, I have a specific requirement for them to be black in both states. In order to achieve this, I ...

Connecting jQuery with Google Maps on iPhones for seamless navigation

I'm trying to develop a web-based app for iPhone that includes Google Maps with a marker indicating a shop location. I'm looking for a way to enable users to tap on the marker and have it open the Google Maps app, allowing them to navigate to the ...

CSS text wrapping will now occur on underscores as well as spaces and hyphens

I am facing an issue with long file names in my HTML formatting causing overflow. These file names use underscores instead of spaces, making it difficult to wrap them properly. For example: Here_is_an_example_of_a_really_long_filename_that_uses_underscore ...

When is the best time and place to break out Yahoo's Mojito Manhattan cocktail mix?

Yahoo! made headlines earlier this month by unveiling their new Mojito framework, set to be open sourced in 2012. I've been eager to learn more about this promising framework but haven't had any success so far. Does anyone know where I can find ...

Retrieve the URL action and return it as a JSON object within the MVC framework

After my controller method returns a JSON object with a URL formed as shown below: return Json(new { url = Url.Action("ShowContact", "Customer", new { vm = contactVM }) }, JsonRequestBehavior.AllowGet); I am encountering an issue in the success code of m ...

Tips for setting environmental variables to match ID values in html documents

In my API reference call, I have stored the key in a separate file and protected it using .gitignore since I am using GitHub. However, I am facing an issue on how to transfer that key from my JavaScript file into the HTML data-key attribute using a variab ...

Dynamic loading of lists necessitates encapsulating nested lists

I'm currently working on a 3-level dropdown list that pulls content from a database for my project. I want the top level to resemble a tab menu, with a border surrounding the area underneath where the additional lists will appear. However, I am facing ...

Issues with retrieving the output of a function nested within another function through an ajax request

I am attempting to use jQuery to add the results of an ajax call to a paragraph. My goal is to extract the "myResult" variable from the inner getResult function and transfer it to the outer buildParagraph function. Unfortunately, the returned value is und ...

Angular mat-select is having difficulty displaying options correctly on mobile devices or devices with narrow widths

In my Angular project, I've encountered an issue with mat-select when viewing options on mobile or low-resolution screens. While the options are still displayed, the text is mysteriously missing. I attempted to set the max width of the mat-option, but ...

Troubleshooting why content set to a <div> element with JavaScript / jQuery is not persisting after a

This is the current code snippet I am working with: <asp:Button ID="btnSave" runat="server" OnClick="Save" CssClass="StylizedButton" resourcekey="btnSave" /> <div id="lbltot"></div> Below is the JavaScript portion of the code: $(do ...

Issue with dynamic form JavaScript functionality after removing curly braces { } from a select tag in Rails

In my Rails form, there is a gender field defined as follows: <%= f.select :gender, ["Male","Female"],{class: "gender"} %> I also tried adding an onclick event like this: <%= f.select :gender, ["Male","Female"],{class: "gender"},onclick: "categ ...

core.js:15723 ERROR TypeError: Unable to access the 'OBJECT' property because it is undefined

Whenever I attempt to run this function, I encounter an issue. My goal is to retrieve the latitude and longitude of an address from Google's API. This error message pops up: core.js:15723 ERROR TypeError: Cannot read property 'geometry' of ...

Span tag not respecting CSS width property

One of my spans is causing an issue. Here are the styles in question: .form_container .col1che { float: left; width: 4%; text-align: left; } .form_container .col2che { float: left; width: 80%; text-align:left; } .form_container .col3che { float: ...

Sending handlebars variable to the client-side JavaScript file

I am currently working on an application using node.js along with express and handlebars, and I'm trying to find a way to transfer handlebars data from the server to client-side JavaScript files. Here is an example: //server.js var person = { na ...

Does using breakpoints in strict mode affect the outcome?

Here is the code snippet: 'use strict'; var foo=function(){ alert(this); } var bar = { baz:foo, }; var x = bar.baz; x();//1 After executing the code directly, everything works fine and it alerts undefined. However, when I insert a break ...

Looking for assistance with coding to show dates?

Seeking assistance with implementing an Ajax code to retrieve Google Calendar events and dynamically inserting two dates into the URL string fetched from Google Calendar. The initial date (timeMax=2021-02-28) should acquire the current date plus 30 days. ...