When a text is lengthy, the ellipsis (three dots) will not be displayed

I have applied the following style:

div>span {
  display: flex;
  flex: 1 0 0;
  align-self: center;
  align-items: center;
  justify-content: flex-end;

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

Interestingly, the styling for ellipsis only seems to work with hidden and nowrap, but not with ellipsis. After researching, I found that it has something to do with the available width calculation, which is not clear to me how it can be helpful.

Evidently, the width of the content exceeds the container (hence being cut off). The closest working solution I found is by setting the display property to block instead of flex. However, I prefer using flex as it resolves many other issues.

I am struggling to find a way to maintain the flexibility offered by flex while making sure the text is ellipsized correctly. Is there a way to adjust this example to achieve this?

Answer №1

The problem at hand arises from the utilization of display: flex on the <span> element where you intend to implement ellipsis. Though switching it to display: inline-block or block would resolve the issue, I acknowledge your preference for using flex with the span element.

My recommendation is to enclose the label within an additional <span> tag. Subsequently, apply the CSS for ellipsis to the recently nested inner span element (You can check out a live demonstration here.):

div>span {
  display: flex;
  flex: 1 1 0;
  align-self: center;
  align-items: center;
  justify-content: flex-end;
  padding-right: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

div>span>span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div>
  <span><span>{{caption}}</span></span>
  <input type="text"
         placeholder="{{caption}}"
         [ngModel]="value">
</div>

Answer №2

If you want to utilize text-overflow: ellipsis; alongside flex-box, the only way to do so is illustrated in the following example:

div {
  display: flex;
  align-self: center;
  align-items: center;
  justify-content: flex-end;
}

div>span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div>
  <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et earum quo tenetur, neque facilis voluptates odio. Neque, facere totam sit!</span>
</div>

This method was sourced from a post on CSS-Tricks, which can be accessed here. It may not be the exact solution you were hoping for, but it could still prove useful.

Answer №3

Using both the properties display: flex and text-overflow: ellipsis on one element is not possible, but you can achieve a similar effect by following this example:

span {
  width: 100px;
  display: flex;
  min-width: 100px;

  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<span>
This is a block-level element
</span>

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

Obtain the Class Name Value by Utilizing the "Begins With" Selection Method

Consider the following HTML structure: <a href="#" class="a b nl-3522">First</a> <a href="#" class="a b nl-7352">Second</a> <a href="#" class="a b nl-4874">Third</a> <!-- Note that classes nl-* are being added dynami ...

"Mastering the art of underlining individual components in React with the power of

Hey there, I'm having some issues working with the CSS for my component. I've created a joke component that has joke and punchline props. The issue is that when it appears in the compiler, my joke component (which I call twice) only shows up as t ...

The content entered into the text area does not appear as it was originally typed

Users have reported an issue with the input area - when they update the text, it does not display as expected. For instance, new lines are not being shown. Here is a snapshot of what the user typed in: https://i.sstatic.net/JK0OQ.png However, after upda ...

Using JQuery to handle click events on an array of checkboxes and displaying the value of the selected

I am struggling to show the label text and checkbox value from a checkbox array whenever each one is clicked (for testing purposes, assume I want it to appear in an alert). My HTML looks like this: <label class="checkbox"> <input type="checkbox" ...

PHP is causing disruptions to HTML

Every time PHP code is present in my file, the page appears blank and upon checking the source code, there are no tags visible on the page. However, once I remove the PHP code, the form displays correctly without any issues. <?php $title = $_POST[&apos ...

Automatically scrolling a div to the bottom in a React component

I am currently working on developing a chat application for my school project, and I would like to implement the feature that automatically scrolls to the bottom of the chat window. Despite trying various solutions and encountering React errors, I have not ...

SVG: organizing objects based on event priority

I am struggling with layering and event handling in an SVG element. Check out the example here: https://stackblitz.com/edit/angular-ivy-rkxuic?file=src/app/app.component.ts app.component.ts import { Component, VERSION } from '@angular/core'; @ ...

Ensure that the height of the content in JQuery Mobile is set to 100

I'm currently working on an app using JQuery Mobile. Check out this link for my HTML code. There's a full-screen <iframe> within my page. Even though I've specified width:100%; height:100%; border:0% for the iframe, it ends up being ...

Allowing the primary content to span the entire width of the mobile screen

I have scoured various forums in search of answers, but unfortunately, I haven't found a solution that fits my specific query. I am looking to ensure that the .main-content (article text and images) occupies the full width of the mobile screen without ...

Progressing through the Material UI LinearProgress bar in steps

How can I split a progress bar into more steps to achieve a design like this? https://i.stack.imgur.com/lRMj6.png I attempted using this code but couldn't find an option for splitting: <LinearProgress variant="determinate" classes={{ ...

When trying to insert glyphicons into a textarea, the result is displaying the actual text instead

When the glyphicon is clicked, I want the entered content to be added dynamically to the textarea. $(document).ready(function(){ //Click Event of glyphicon class $(document).on('click','.glyphicon',function(){ ...

Ways to ensure that when changes occur in one component, another component is also updated

How can I update the cart badge value in the navbar component every time a user clicks the "Add to Cart" button in the product-list component? The navbar component contains a cart button with a badge displaying the number of products added to the cart. n ...

Positioned in the middle is a collection of left-aligned divs

While many questions address similar issues, the focus is always on having all items centered (accomplished by using display:inline-block). However, my requirement is to have the last line of items floated neatly left and only center the container itself ( ...

Create a dropdown menu option that changes based on the number of items that have been

I need a selection option that displays values 1 to 5. Depending on the selected value, it should generate select options equal to the number selected. For example, if I choose 3, then it should generate 3 select options. Is it possible to loop the ajax in ...

Is there a way to utilize PHP/HTML to exhibit a diverse array of random images as dynamic background visuals?

I'm currently learning the process of constructing a website. I've successfully constructed the index page, and below you will find the PHP and HTML code: <?php session_start(); $pngFiles = array('image1.png', 'image2.jpeg' ...

Creating a grid with individual node delays in CSS animations using ReactJS

Struggling with a CSS animation issue in ReactJs. I suspect the problem, but unsure how to solve it. I'm rendering a 5x5 grid and displaying it using this function: DisplayNodes = () => { const {grid} = this.state; // get the node array fro ...

html - Bootstrap Navbar - Tips for aligning content on both sides

Exploring new frontiers with Bootstrap in Angular. I have successfully integrated a Bootstrap template navbar into my html-site. The navbar consists of text items in an ordered list and a search button along with a related form. My goal is to align the tex ...

Ways to ensure the input placeholder remains visible as the user is typing

I'm facing a fresh and fascinating challenge. Rather than the typical behavior of the placeholder text vanishing as soon as the user begins typing, I'd like it to stay in place but move over to the right side of the input box. Can this be accomp ...

Creating Double Diagonal Borders and Corners in CSS

Hey everyone, I'm currently working on a new project for the website and am trying to recreate something similar to this design: view image here At this point, I have managed to create half of the shape using the following code: <div id="bloc ...

The styled-components in React are having trouble recognizing the HTML attribute 'autoplay' when used with a video tag

I have created a custom styled component for a video tag. const Video = styled.video` ... ` When I use this component like below: <Video width='200px' height='200px' autoplay='autoplay' muted loop> <source src= ...