Employing HTML and CSS to restrict the number of characters in sentences

Extracting a few sentences from a JSON file and displaying it in a <p> tag.

    <p>
       Thank you all for another wonderful night spent together 
       this past Sunday at The Hippodrome in Baltimore. Thank yo...
    <p>

I want to further shorten the displayed text, even though it has already been truncated in the JSON. Is it possible to limit its length using only pure CSS or HTML?

I am not looking for JavaScript/jQuery solutions since achieving this with JavaScript is straightforward by manipulating the DOM. I am exploring if there are any ways to achieve this using pure HTML and CSS.

UPDATE 1: Many suggestions involve converting the sentences into a single line using white-space: nowrap and hiding overflow text with text-overflow: ellipsis. However, this approach limits the display to a single line. Are there alternative methods to display multiple lines of text?

Answer №1

For those looking to achieve a single line of text with an ellipsis (overflow `...`), the text-overflow:ellipsis attributes can be used if you are working with a single line. Be sure to also consider adding the width attribute in some browsers.

overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

On the other hand, if your text spans multiple lines, you can utilize the WebKit CSS extension -webkit-line-clamp. It's important to note that this attribute is specific to WebKit browsers and mobile devices (which are predominantly WebKit-based). Keep in mind that this is a non-standard attribute and is not included in the CSS specification draft.

overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;

Answer №2

Even though CSS and HTML are unable to determine the length of a sentence, there is no inherent limit on what can be achieved when it comes to styling text. One way to truncate a line of text using CSS is by setting a maximum width for the element. This can be done using the following code:


p {
    max-width: 100px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

Answer №3

Here is a useful link you can check out: https://developer.mozilla.org/en/docs/Web/CSS/text-overflow

You can achieve text truncation with an ellipsis effect using the following CSS code:

p {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px; /* Set width as needed */
}

Answer №4

Check out this demonstration:

<div style="width: 200px;height: 20px;overflow: hidden;">
   We had such an enchanting evening together last weekend 
   at The Roxy in Los Angeles. Grateful for the memories made...
</div>

Answer №5

If you need to truncate text using CSS, especially for multi-line text boxes that need to properly truncate at the end, you may have to consider using a jQuery plugin like clamp.js or something similar. Anubhav's answer focuses on single-line text truncation, so keep that in mind.

For more information on this topic, check out this helpful thread on Stack Overflow:

In addition, there is a complex but effective CSS solution available here: . It's not as straightforward as using a jQuery plugin, but it can get the job done if needed.

Answer №6

Instead of truncating text with "..." using JavaScript, you can achieve the same effect by utilizing the CSS property text-overflow: ellipsis;. Take a look at the example below for guidance.

<div id="readmore">Thank you all for another enchanting evening spent together 
       this past Sunday at The Hippodrome in Baltimore.
</div>

CSS

#readmore {
    white-space: nowrap; 
    width: 12em; 
    overflow: hidden;
    text-overflow: ellipsis;
    border: 1px solid #000;
}

To explore further information, please visit this resource.

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

Vue Filtering and Pagination Implementation

In Vue pagination, how can you control the number of array objects to be displayed based on a user-selected amount like 10, 15, or 25? I successfully implemented rendering 10 items per page and it's functioning smoothly. ...

Place the image either above or below the text in a relative position

I have a peculiar dilemma that has been on my mind lately. As I work on designing my website, I am nearing completion of the responsive/mobile view. Currently, everything looks fine, but only because I use display: none; to hide images when the viewport is ...

What steps are required to establish a PHP development and production environment?

I find myself in the position of taking over a small PHP site that relies on a database. While I'm well-versed in C# programming and have experience with databases and web design, I am quite unfamiliar with PHP. My boss wants me to establish both a s ...

The content in the div is not contained within a border in the css/html

I am struggling with a div wrapper that has a border, but the border is not wrapping around the content and I can't figure out why. Here is my issue on screen: This is what I want it to look like: Below is my HTML code: <div class="event_area_t ...

"JQuery event handlers not functioning as expected: .click and .on failing

For some time now, I've been facing this issue and I'm at a loss trying to figure it out. I have attempted various solutions such as using .click(), .on(), .delegate, and even utilizing .find() to locate the specific element causing the problem. ...

unable to show image retrieved from JSON data

Looking to showcase the data from my JSON objects, which are displayed in 2 images below https://i.stack.imgur.com/yhqFy.png https://i.stack.imgur.com/DUgFO.png In the data, I have properties like c_name, max_slots, and an array slots. Within the array, ...

Tips for saving information from a textarea into an HTML file

I have a unique question regarding the usage of $fopen and $fwrite functions. My goal is to include a button named "save as HTML" below a textarea. Upon clicking this button, I want a popup box to appear mimicking the 'save as...' dialog window ...

What is the correct way to add "itemCondition" and "logo" schema tags to schema.org product snippets?

I'm in the process of enhancing my product page with schema.org snippets. My client has specifically requested the inclusion of certain schema.org tags on the product's individual page. 1) itemCondition 2) logo (displaying only the brand image ...

How can you change the list-style-type to switch between lower-alpha and decimal on nested items within an ordered list?

I am attempting to reverse the list-style-type of nested items within an ordered list, including sub-items and sub-sub-items. I have tried using CSS counters but it doesn't seem to be working. Can anyone point out what I might be doing incorrectly? ol ...

How can you correctly include a link to a plain text sitemap file in an HTML document?

Could this code effectively inform Google to index my sitemap (or acknowledge its presence)? <link rel="sitemap" href="./sitemap.txt" type="text/plain" title="Sitemap" /> Google's guidelines allow plain t ...

The functionality of List.js is currently not optimized for use with tables

I'm currently experimenting with list.js in order to create a real-time search feature for a table. I have successfully tested it on lists (similar to the example provided at ). However, I am facing difficulty replicating this functionality for tables ...

Stop Gulp Inline CSS from enclosing HTML content within html and body elements

I need to work on a piece of HTML that I plan to copy and paste directly into an existing document. Here is the code snippet: <div id="someDiv"> </div> <script src="js/someScript.js" inline></script> <style> div#someDiv { ...

What steps can I take to address the div issue in my React application?

After implementing Browser Router, I am encountering whitespace on the left and right side of the navbar. How can I resolve this issue? Below is the code snippet: <div className="container my-3"> <BrowserRouter> ...

Encountering a predicament when attempting to retrieve an image from an alternative

[index.jsp][5] style.css [problem][6] [folder hierarchy][7] Although everything runs smoothly when the file is located in the css directory, it fails to show any images if placed in the images folder. Kindly provide assistance. ...

Issue with displaying data using a custom pure pipe and boolean in ngIf condition

For my current web project, I require a friendship/follow feature. There are two roles involved: admins and regular users. Regular users have the ability to follow other users, while admins do not possess this capability. When a user wishes to follow anot ...

Issues arise when using the "placeholder" attribute within a <p:inputText tag while the inputText is devoid of any value

A current project involves implementing JSF Mojarra 2.3.9.SP02 alongside PrimeFaces 7.0 on Wildfly 17 using the Sapphire template provided by PrimeFaces. However, I've encountered a significant issue with a specific <p:inputText element within my f ...

What are some ways to modify a Vue template without the need to recompile?

Is it feasible to modify a Vue template's HTML without the necessity of recompiling the Vue file? ...

I'm having trouble with fixing the TypeError that says "Cannot read properties of undefined (reading 'style')." How should I address this issue

I'm having trouble with a slideshow carousel on my website. When the site loads, only the first image is shown and you have to either click the corresponding circle or cycle through all the images using the arrows for the image to display. I'm al ...

Is there a way to eliminate the space between the content inside a table cell and its borders?

I am struggling with a table setup that includes three columns and multiple rows. The first column contains an image, causing the cells in that row to resize based on the image size. When text is added to the 2nd and 3rd columns, it automatically centers w ...

Enhancing interactivity with jQuery's ajax event functionality

Alright, let me share my code with you: $("#content_Div").ajaxStart(function () { $(this).css('cursor', 'wait') }).ajaxComplete(function () { $(this).css('cursor', 'default') }); I'm facing a simple is ...