Minimize excessive white space in cases of text overflow, ensuring that text is limited to a maximum of 2 lines

When text exceeds its container, it automatically adjusts the width of the parent element and fills up any extra white space even after overflow. I'm trying to find a solution to allow for text overflow while still restricting it to just 2 lines and resizing the parent container to reduce unnecessary white space.

I managed to resize the container using flex-box, but then I couldn't limit the text to 2 lines.

If you'd like to see my current progress, here's a codepen link: https://codepen.io/illyabalakin/pen/KKGNvbY

.btn {
  max-width: 220px;
  height: 50px;
  padding: 0 30px;
}

.flex {
  display: flex;
  flex-grow: 1;
  width: min-content;
}

span {
  display: -webkit-box;
  hyphens: auto;
  overflow: hidden;
  overflow-wrap: break-word;
  text-transform: uppercase;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}
<button class="btn">
  <span>Extended Text Overflowing overflow</span>
</button>

<button class="btn">
  <span class="flex">Extended Text Overflowing overflow</span>
</button>

Check out this image to see the excess empty space that I want to eliminate

Answer №1

.btn {
  max-width: 220px;
  height: 50px;
  padding: 0 30px;
}

.flex {
  display: flex;
  flex-grow: 1;
  width: 100%;
  min-width: 0;
  align-items: center;
}

span {
  position: relative;
  overflow: hidden;
  white-space: wrap;
  text-overflow: ellipsis;
  text-transform: uppercase;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;  
}
<button class="btn">
  <span>Extended Text Overflowing overflow</span>
</button>

<button class="btn flex">
  <span>Extended Text Overflowing overflow</span>
</button>

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

How can I align an input to the right using Bootstrap?

I found an example of a navbar that I liked and decided to copy it. Here is the code snippet: <div class="container"> <h3>Nawigacja zamknieta w kontenerze</h3> <nav class="navbar navbar-toggleable-md navbar-light bg-faded"> < ...

When clicking, the images are displayed within a faded-in div

I'm trying to create a layout where all the images within the #ImageContainer are displayed in a fixed Div. Everything seems to be working well except for the fact that when I click on an image, it disappears from the #ImageContainer and shows up in t ...

How can I open a PDF file on the frontend using the Django framework?

I am attempting to extract content from a PDF file using the Django framework. Here is what I have tried so far: def upload(request): try: if request.method == 'POST': if request.FILES['document'].name.split ...

What could be causing the overflow of the div element within my header?

I recently embarked on a personal project to create my own portfolio website using HTML, CSS, and JavaScript. As I was working on styling the header section with bright colors for visualization purposes, I encountered an issue where the green box was overf ...

I'm having trouble making my jQuery banner responsive - which property should I be using in this situation?

I am experiencing an issue with my jQuery banner not shrinking properly. In my mobile.css file, I have set the class .banner width to 100%, but it is not shrinking to a width of 260px as expected. I would like to know which property I need to use in orde ...

Click the button to automatically generate a serial number on the form

My form includes three input fields: sl no, stationerytype, and stationeryqty. By clicking the symbols + and -, I can add or delete these fields. I am attempting to automatically generate a Sl no in the sl no field when I click the plus symbol, and adjust ...

Modify the text inside the <span> tag using the :hover and :hover:after pseudo-classes

This code is working properly: HTML: <span class="counter" >test</span> CSS: .counter:hover:after{ content: "hello"; } Result: testhello However, I am trying to replace 'test' with 'hello'. I attempted the foll ...

Sticky header in an Angular Material table

Using Angular 7 with Angular material design templates, my current result looks like this: https://i.sstatic.net/G4W78.jpg However, I am aiming for a result similar to the following - with a scrollbar under the sticky header. https://i.sstatic.net/WgjVh ...

Avoiding Page Reloads with Ajax while Removing Entries from a Table in Laravel

I am encountering an issue where I can click the button, but it requires a refresh to execute the delete action. Is there something in my ajax code causing this problem and why is it refreshing? I want the button to instantly delete without the need for a ...

Expanding the Width of a Web Page with CSS on a Squarespace Website

Looking to adjust the width of my Squarespace website on the SKYE template to have minimal padding around images and content. Check out the website here using the password123. I've attempted to make changes with no success using the following code: ...

Adjust the height of a div according to the width using CSS in a progressive manner

I'm looking to create a fluid container that adjusts its height gradually based on the width of the window (as the div's width is set to 100%). Similar to the behavior achieved with the aspect-ratio CSS rule, I want the height to smoothly increas ...

CSS problem: alignment issue between text and checkbox

I'm experiencing a CSS style issue where the text box and text are not aligned properly. Can someone assist me in fixing this problem? Below is the code I am using: Thank you for your help. <div> <input type="checkbox" ...

Modifying the appearance of the initial column in a panelGrid

Is there a way to left align the first column and right align the second column of a <h:panelGrid>? I've attempted to assign a class to elements in the first column for left alignment, but as they are outputText elements nested in a span inside ...

What is the most effective way to transfer an array from one PHP file to a different JavaScript file?

Utilizing AJAX to send a request to another php page and retrieve the result of a query, I originally used xmlhttprequest to pass the php logic along with the query information. However, I wanted to separate the presentation logic from the actual code logi ...

Automatically closing HTML tags using Vanilla JavaScript

When a user inputs a tag, such as <div>, JavaScript is supposed to close it with </div>. However, this should not happen for non-HTML tags like <hello>. I attempted to achieve this using the method str.repeat, but the results were not as ...

CSS has the ability to override the height and width of images

<img src="img_beach.jpg" alt="Beach" width="800" height="600"> In my HTML body tag, the line above is what I have. However, the specified height and width are being replaced by the values set for the image in the stylesheet. What could be causing t ...

How can I connect a custom Web Resource button in Dynamics CRM 2015 to a specific function within a Javascript Library integrated into the form?

I have added a button to the form using the following code: <button type="button" onclick="btnCalculate_onClick();">test</button> Within the form, I have included a function called btnCalculate_onClick() from a Library named dc_btnAutofillAdd ...

What is the best way to insert space between spans in HTML when designing email templates?

I am currently designing email templates, which means I have some limitations. Although I have used align="center", I still require some spacing between the two spans. How can I achieve this? CODE: <td width="659" height="28" bgcolor="#999999" align= ...

Distinguishing between these two hover text types: JQuery CSS, what sets them apart?

As a total jQuery novice, I've been curious about the difference between these two types of text that appear when you hover over something. Can someone please clarify what each one is? Thank you very much. First hover text: When hovering over the up- ...

"Applying a class to a div element when it becomes visible within

I am attempting to dynamically add a class to a div element when it comes into view on the webpage. I am utilizing a minified jQuery 1.11.0 script to achieve this functionality. Below is the jQuery code I have implemented: function isScrolledIntoView(ele ...