Using an HTML ellipsis without setting a specific width

I am working on achieving a specific layout as shown in the screenshot below.

  • If both SPAN and B fit within the box, they should be displayed one after another.
  • If they do not fit, SPAN should have an ellipsis while B is fully displayed (never larger than the block).

Even when attempting to include B within SPAN or using nested tables, I have not been successful in achieving the desired result.

Expected outcome:

Initial snippet:

div {
  width: 200px;
  white-space: nowrap;
  border: 1px solid red;
  margin-top: 10px;
}

span {
  overflow: hidden;
  text-overflow: ellipsis;
}

b {
  padding-left: 10px;
}
<div>
  <span>test</span>
  <b>12345</b>
</div>

<div>
  <span>test test test test test test test test test test test</span>
  <b>50</b>
</div>

Answer №1

To make the text ellipsis in a flex container, simply add display: flex:

div {
  display: flex; /* new */
  width: 200px;
  white-space: nowrap;
  border: 1px solid red;
  margin-top: 10px;
}

span {
  overflow: hidden;
  text-overflow: ellipsis;
}

b {
  padding-left: 10px;
}
<div>
  <span>test</span>
  <b>12345</b>
</div>

<div>
  <span>test test test test test test test test test test test</span>
  <b>50</b>
</div>

The use of default settings like flex-shrink: 1 in combination with flex allows for ellipsis to show up within a flex formatting context.

Answer №2

To ensure the content displays correctly, make sure to set the display to inline-block and specify a maximum width for the inner span element.

div {
  width: 200px;
  white-space: nowrap;
  border: 1px solid red;
  margin-top: 10px;
}

span {
  overflow: hidden;
  text-overflow: ellipsis;
  display:inline-block;
  max-width:150px
}

b {
  padding-left: 10px;
}
<div>
  <span>test</span>
  <b>12345</b>
</div>

<div>
  <span>test test test test test test test test test test test</span>
  <b>1234</b>
</div>

Answer №3

Sharing the solution to my own query:

In my case, I needed the solution to be compatible with PDFs generated using the wkhtmltopdf library. This particular library is based on QT WebKit which currently does not support flex, but it does support the older syntax of flex known as flex-box.

Below is how you can implement it in wkhtmltopdf (this also works in Chrome):

.flexrow {
  width: 300px;
  margin-top: 30px;
  border: 1px solid green;

  display: -webkit-box;
}

.flexrow .fst {
  background-color: yellow;

  -webkit-box-flex: 0.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 5px; /* for ellipsis */
}

.flexrow .snd {
  background-color: aqua;

  white-space: nowrap;
}

.flexrow .trd {
  background-color: pink;

  -webkit-box-flex: 2147483647; /* max int */
}
<div class="flexrow">
  <div class='fst'>test</div>
  <div class='snd'>12345</div>
  <div class='trd'></div>
</div>

<div class="flexrow">
  <div class='fst'>test test test test test test test test test test test test test test</div>
  <div class='snd'>50</div>
  <div class='trd'></div>
</div>

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

Storing Form Data in MySQL, upon clicking the submit button the user is redirected to a PHP page and the information is not

Currently, I am facing an issue with injecting data into my SQL table. Whenever I input information into the forms and click on the submit button, the page redirects me to my PHP file without actually injecting any data. The tables remain empty. Below is ...

Slider that allows range selection after midday

Currently facing a dilemma using the noUiSlider plugin. I have set up a time range picker starting from 6 am to 6 am the following day. Everything works smoothly until it reaches 23:59, but I need it to display 1:00, 2:00 instead of 25:00, 26:00 for the ...

Adjusting the color of an HTML slider button as it moves

In my setup, I have a straightforward slider that I plan to use for controlling the movement of a stepper motor based on the slider's position. I wanted to give the website where this will be hosted a professional look, so I've spent quite some t ...

"Utilize PHP to link to the same page, pass data through $_POST method, and

I have a database table containing (NumSection (id) and NomSection) In my webpage, I want to display all the data from 'NomSection' as a link. When clicked, I want to open the current page with $_POST['nomSection'] and show the data fo ...

Is there a way to position this Flex-Box Item beside the image?

Please refer to the image How do I rearrange this Flex-Box Item to be positioned next to the image? <div class="brif fb"> <div class="sml-con"><img class="sml-thumb" src="images/chosen/edward.jpg" alt=""></div> ...

Steps to include a HTML webpage within another page

I need assistance with a scenario involving two separate HTML pages on different servers. Merchant Form - Server A Payment Form - Server B Here's the desired scenario: The user completes all necessary fields on the Merchant Form and clicks submit. ...

The intricate dance between JAVA and HTML

Can Java be compatible with HTML and JS? Is it possible for them to cooperate without using JSP? In order to connect the WEKA function, we utilized Java code through a JAR file, but now we also require HTML and JS links for visualization. Is there an alte ...

Validation is not being enforced for the input field labeled as 'name' in the form

Can anyone assist me with a form validation issue I'm encountering while working on my project? The validation is functioning correctly for email and institution fields, but it seems to be ignoring the name field. Any suggestions or help would be grea ...

Leveraging LESS variables within media queries

After inputting the less code below into : @item-width: 120px; @num-cols: 3; @margins: 2 * 20px; @layout-max-width: @num-cols * @item-width + @margins; @media (min-width: @layout-max-width) { .list { width: @layout-max-width; } } The resul ...

Utilizing Boostrap to create a background image positioned on the far left of the screen within a .container

I am currently working with bootstrap 4 and have a layout that includes a container class: body #content.container .row .col-6 | Great content .col-6 | I really like it .row .col-12 And so forth .row ...

Unexpected Error with Background Position Variable

Hello, I am attempting to create an animated background effect that moves up and down using the .animate() and .hover() methods in jQuery. Within my DOM, there is a div with id="#menu" containing a UL list where each item has a background positioned at dif ...

What methods can be used to cloak JavaScript functions from the end user?

Looking to utilize jQuery AJAX calls? Here's an example: function addNewTeacher(){ $.ajax({ type: "POST", url: "/actions/dboss/newteacher.php", data: "uname=" + $("#newteacheruname").val() + "&upass=" + $("#new ...

What is the best way to animate specific table data within a table row using ng-animate?

Are you working with Angular's ng-repeat to display a table with multiple rows? Do you wish to add an animation effect to specific cells when a user hovers over a row in the table? In the scenario outlined below, the goal is to have only certain cell ...

As you scroll, a box blocks off half of the screen

Hey everyone, I could really use your assistance! I'm working on developing a javascript code and here is the idea - if anyone can contribute to it. It's like a social media platform where users enter the site and the is_user_logged_in parameter ...

Display the website logo when users share on WhatsApp

My goal is to have my website icon appear in WhatsApp when I share it (similar to the example below). https://i.stack.imgur.com/KCWi8.jpg I initially tried using this code, but it didn't work: <link rel="shortcut icon" href="css/img/favicon/favi ...

As you scroll, this smooth marquee effect gently shimmers with each movement

I've been trying out this code to create a scrolling marquee text, but the issue is that after 7000 milliseconds it starts to jitter, which doesn't make the text look good. Any suggestions on how I can fix this problem? Let me know! <script ...

Tips for styling your Angular Material Card on mobile devices

Currently, I am very happy with my desktop layout which looks like this: https://i.stack.imgur.com/tG0pw.png However, when it comes to the mobile version of my site, here is what I have so far: https://i.stack.imgur.com/KD1hh.jpg While I do like the ho ...

Hiding an element with display: none will prevent the hover effect from

I have integrated a CSS dropdown menu from this link: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button Additionally, I am utilizing jQuery .on("click", event with an AJAX call. Following the successful AJAX call, I am using the foll ...

I can only use innerHTML once in my React application

I am attempting to clear my container div when the user clicks the search button in order to remove all existing search results. However, I am encountering an issue where using innerHTML to clear the container div only works the first time. If a second sea ...

Tips for adjusting the height of a jQuery UI widget control through CSS

I have implemented a jquery widget on my asp.net webforms and am trying to adjust the height of the select control from the default 22px to 33px. I noticed in Google developer that the height is originally set like this: The current height setting is loca ...