Display text with ellipsis on one of the two spans within a container

I'm struggling with implementing ellipsis in my HTML code. Here's what I have:

<div id="wrapper">
    <span id="firstText">This text should be displayed with an ellipsis</span>
    <span id="lastText">this text should not be affected by the ellipsis</span>
</div>

Is there a way to achieve this using just CSS? Here are my attempts so far:

#firstText
{
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

#lastText
{
    white-space: nowrap;
    overflow: visible;   
}

This is how it currently appears:

This text should be displayed with an ellipsis but ends up being cut off this text shoul

However, this is the desired result:

This text should be e...this text should not change size

Answer №1

To set the width of your #firstText, use the following CSS:

#firstText
{
    overflow: hidden;
    white-space:nowrap;
    text-overflow:ellipsis;
    width:150px;
    display:inline-block;
}

Take a look at this example

http://jsfiddle.net/Brksp/9/

Answer №2

To achieve the desired effect, you can rearrange the order of your spans and apply a float right to the first span. This allows you to avoid specifying a width for any of your elements:

#firstText
{ 
  font-size: 30px;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#lastText
{ 
  color:red;
  font-size:30px;
  float:right;
  white-space:nowrap;
}
<div id="wrapper">
    <span id="lastText">this text must be shown in full</span>
    <span id="firstText">This text may be cropped</span>
</div>

Answer №3

There's a scenario that hasn't been addressed yet:

Imagine you have a container with an unknown or dynamic size and you want the first child element to expand to fill all available space but show ellipsis if it exceeds the boundary.

Here's a solution using CSS properties like flex:

#wrapper{
  display: inline-flex;
  flex-flow: row nowrap;
  max-width: 100%; /* or width:100% if you want the spans to take all available space */
}

#firstText{
  flex: 1;
  white-space: pre;
  overflow: hidden;
  text-overflow: ellipsis;
}

#lastText{
  white-space: nowrap;
}
<div id="wrapper">
  <span id="firstText">This text should be affected by ellipsis if the container is too small</span>
  <span id="lastText">This text should remain fixed in size</span>
</div>

Answer №4

To achieve a similar effect, consider updating your code by changing the span element to a div, and adjusting your CSS as follows:

#firstText
{
    float: left;
    width: 250px;
    height: 20px;
    overflow: hidden;
    white-space:nowrap;
    text-overflow:ellipsis;
}

#lastText
{
    float: left;
    white-space:nowrap;
    overflow:visible;   
}

Answer №5

If you prefer not to explicitly set the width or max-width, here's a way to achieve it using the display:flex; property.

#container {
  display: flex;
}

#firstElement {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#secondElement {
  /* Text might wrap to the next line */
  white-space: nowrap;
}
<div id="container">
  <span id="secondElement">This entire text must be visible</span>
  <span id="firstElement">Some part of this text can be truncated</span>
</div>

To enable ellipses, two basic requirements need to be met:

  • Add the

    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    properties to the element where you want to show ellipses.

  • Limitedly control the width of that element, whether by setting a fixed width or min-width, utilizing display:flex;, or any other method.

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

`CSS Animation fails to run in Internet Explorer and Microsoft Edge browsers`

My issue with the CSS animation is that while the opacity animates properly, the translate effect does not seem to work as expected. You can see the problem here: https://jsfiddle.net/bLxb8k3s/ It appears that the reason for this issue is because IE and ...

The HTML file contains the complete version number of Firefox

If you're an expert in HTML, take a look at my expandable start page. I know there's room for improvement, and I'm seeking advice on automatically updating the Firefox browser version number in line 106 of the code snippet below. (Linux Mint ...

Update the input type on the fly

Hey there, I've got a bit of an unusual question. I'm looking to create a form with fields that vary for each user, like a text input or a checkbox. I attempted using Angular to achieve this: <div ng-controller="myCtrl" ng-repeat="x in prova" ...

Designing a layout with one box on top and two beneath it, covering the entire page, can be achieved by following these steps

I am currently working on a project where I want to divide the screen into three sections. One section will cover half of the screen to display an image slider, and the remaining half will have two sections which is already done. However, now I need to add ...

Having issues with CSS list hovering functionality in Google Chrome

One of the challenges I'm facing with my HTML list is that when hovering over each list item, the text along with the bullet point should change color. Here's the structure: <ul> <li>A bullet point</li> <li>Another ...

Styling with CSS will keep your content centered on the page, while the background

I'm looking to create a layout with the following specifications: The content wrapper will have a fixed size and be centered: .content { width:960px; margin:0px auto; } My issue is how to implement the left-aligned background bar for the su ...

Utilizing the '<' or '>' operator in combination with an if statement within a Repeater component

Is it possible to use an if statement in a repeater like this? <%#Eval("FN").ToString().Count > 0 ? "SB" : "" %> When I try to implement this, I receive an error 73 indicating that the > operator cannot be used. How can I adjust it so that i ...

I would like to automatically log out when closing the tab in Mozilla, Internet Explorer 8.0, or Chrome

Hello there, I was wondering if it's feasible to end the session and log out when I close my tab. I'd appreciate it if you could confirm whether this feature has been implemented on your end. Thank you, Manish ...

Is it possible to ensure uniformity in the appearance of all images with MVC Image Handling?

I'm currently developing an MVC 4 application that allows users to upload images. These uploaded images are then displayed in a different view. Users can upload images up to 10MB in size, which are then resized to 800 x ? pixels using the WebImage (Sy ...

Interactive button visual effect

I have a piece of code that currently plays audio when I click on an image. However, I am looking to modify it so that not only does clicking on the image play the audio, but it also changes the image to another one. Can anyone assist me with this? Here i ...

What are the best ways to ensure text stays center aligned and prevent position absolute elements from overlapping?

I'm currently working on a project that involves center-aligning the page title within the parent div. However, I have run into an issue with an overlapping back button when the page title is too long due to its absolute positioning. Can anyone provid ...

Add space between the first and second rows in the grid gallery display

.gallery{ display: grid; grid-template-columns: repeat( auto-fit, minmax(250px, 1fr) ); grid-template-rows: repeat( auto-fit, minmax(250px, 1fr) ); } view grid image here Could you assist me with identifying the spacing issue between the first and second ...

Is there a quick way to import all available font weights from a Google font?

I am looking to experiment with importing all weights and styles of a Google font, such as Roboto or Roboto Slab, in a CSS file. Is there a more efficient way to do this rather than listing out each individual style in the @import? ...

Achieving the desired results through the utilization of CSS3 rather than relying on images

I am currently exploring the use of CSS3 to create a menu instead of relying on images over at . You can view my progress and code (including images and styles) here: Although I attempted to use CSS3 for the border shadow and matching the background of ...

Move the location of the mouse click to a different spot

I recently received an unusual request for the app I'm developing. The requirement is to trigger a mouse click event 50 pixels to the right of the current cursor position when the user clicks. Is there a way to achieve this? ...

The string data needs to be extracted from the HTML page and then saved in the XML file within the respective categories

As part of a project, I am required to extract the value of a specific character ('v') from an HTML page that I have created. Within the HTML page are several links surrounded by irrelevant data: /watch?v=blablabla&list=blablabla&index=7&featu ...

Click the button in Javascript to add new content

I am currently experimenting with JavaScript to dynamically add new content upon clicking a button. Although I have successfully implemented the JavaScript code to work when the button is clicked once, I would like it to produce a new 'hello world&ap ...

A Challenge with Session Variables in Internet Explorer 8 and Classic ASP

I run a website that has a stock management system with a table displaying stock and inventory information. When the "images" link is clicked, the Session variable for images switches between "show" and "hide" and then reloads the page with the updated va ...

choose exclusively the text within the elementor navigation menu

I've been tinkering with this issue for a few hours now. I have a vertical Elementor navigation menu and I'd like to add a hover effect to it. So far, I can only select the entire column and apply the effect to that, not just the length of the t ...

What causes the content of a sticky navbar to overflow?

I have a situation where my navbar has two styles (.navbar and .navbar_fixed), but the links in the navbar are not fitting properly. I attempted to remove padding and add left: 0;, however, these adjustments did not work as expected. Any suggestions on how ...