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

I'm having trouble with aligning my input checkbox within the form

My form includes multiple checkboxes, but I am having trouble aligning them properly. <!-- Multiple Checkboxes (inline) --> <div class="form-row"> <label class="chkdonar" for="donarchkform-0">Yes, I want to donate to AMIAB</label> ...

Positioning a div with a set size to the right while allowing a div with a variable width to the left

Even though I achieved the desired outcome in this example (http://jsfiddle.net/jcx3X/40/), I am intrigued by the reasons why this other scenario (http://jsfiddle.net/jcx3X/41/) does not work. What is the significance of the div being floated first in th ...

I successfully corrected the selectable options list in HTML/CSS and am now working on linking it to a Django char field. I'm currently facing some difficulties in figuring out

Below is a Django form field I have defined: source_currency = forms.CharField(max_length=5) Here is an example of Select/Option HTML: <select name="fancySelect" for="{{ form.source_currency.id_for_label }}" class="makeMeFancy" id="drop1"> ...

Material UI does not have built-in functionality for displaying colored text within a multiline textfield component in React

Attempting to utilize the material ui library in a react app has brought an issue to light. It appears that styling colored text does not work as expected for multiline textfields. Consider the following scenario: import React, { Component } from 'r ...

When trying to serialize elements from a form loaded by Firefox using AJAX, encountering difficulties due to

Visit the live version of this at . When prompted for a formId, input BroadRunTrack2013. Follow by adding an item to cart and entering player name information (you can use random data for now). Select the Runner's Hat, choose color/size, specify quant ...

Issues arise when attempting to determine the accurate dimensions of a canvas

Looking at my canvas element: <canvas id='arena'></canvas> This Element is set to fill the entire website window. It's contained within a div Element, both of which are set to 100% size. I attempted running this script: var c ...

Position filter forms on the left side to align with bootstrap cards

I'm having trouble aligning the cards using Bootstrap's row and col- classes. The first three are behaving as expected, but the fourth one isn't cooperating. Any idea why this might be happening and any solutions you can suggest? View the i ...

When will the search bar toggle and the logo appear sliding down?

.navbar { background-color: #F91F46; } .src-bar { border: 0; border-radius: 5px; outline: none; padding-left: 15px; width: 30vw; } <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" in ...

What specific width range would be best for my needs?

I used to have the default font style of Myriad Pro Light and a padding setting for my main menu on my website as follows: .menu > li > a { padding: 17px 15px 15px 15px; } Recently, I changed the default font style to Montserrat. However, this chang ...

The v-checkbox appears much larger in size and has a different row size when compared to the v-radio

Currently, I am working on an application using Vuejs 3 with Vuetifyjs 3, and I have encountered an issue regarding the row size difference between a v-checkbox and v-radio when set to density="compact". The discrepancy in line height can be seen in the im ...

transition effect of appearing and disappearing div

Having trouble creating a fade out followed by a fade in effect on a div element. The fade out happens too quickly and the fade in interrupts it abruptly. Here is the JavaScript code: $('#fillBg').stop(true,false).fadeTo(3000, 0); $("#fillBg"). ...

Steps for inserting a clickable phone number link within innerHTML1. First, create an

I've been trying to include a specific phone number using the <a href> tag within the innerHTML. I attempted using both double and single quotes. In the first case, nothing happens at all. In the second case, although the phone number appears, ...

What is causing Firefox to display an additional line in this section?

After much effort, I've been attempting to eliminate the extra spacing on this page in Firefox: Do you see the menu? If you compare it to Chrome, you'll notice that the menu is positioned too low beneath the background, giving the layout a broke ...

Angular2: Leveraging click events to manage CSS classes on elements

I am currently developing a web app using Angular 2. I'm facing an issue where the active element should receive an extra CSS class when clicked, but adding the ":active" CSS attribute with a custom class doesn't work as expected. The ":focus" pr ...

Guide on incorporating JSON information into an HTML table

Currently, I am retrieving data that needs to be added to an HTML table once it is received. I have attempted some methods, but I am unable to dynamically add the data after the page has loaded. I aim to accomplish this using either JavaScript or jQuery. ...

Placing an image within a table row that spans multiple rows to maintain center alignment in relation to other rows that are not span

Having trouble with centering the image in an email signature when viewed in GMail. The layout works fine in JSBin but appears uneven in GMail. Any suggestions on how to make sure it stays centered? Check out the code here <table cellspacing="0" cel ...

How to emphasize a clicked hyperlink in AngularJS

Here's the HTML code I've been working on: <div id="Navigation" class="md-dialog-full"> <div class="products-options"> <a ng-click='addType("Physical")' href="javascript:void(0);"> < ...

Designing an HTML banner with 100% width and 660 pixels in height for optimal responsiveness

I am in need of a responsive banner that will be displayed at 100% width and 600px height on fullscreen. Below is the code I have: <style> /* default height */ #ad { height: 660px; width: 100%; } @media o ...

Faulty toggle functionality within a JavaScript-created accordion panel

HTML I'm looking to add a FAQ question within the div with the class "section-center" <body> <section class="questions"> <div class="title"> <h2>FAQ SECTION</h2> < ...

Content not being hidden by Twitter Bootstrap tabs

I am encountering an issue with my code. When I click on an inactive tab, the content of the first tab does not hide and both tabs' content is displayed simultaneously. Is there a solution to this problem? Thank you for your help! <ul style="l ...