Is there a way to automatically shorten a text when it goes beyond the boundaries of a div container?

How can I prevent a paragraph in one of two adjacent div containers from having a line break without changing the container's size? The surrounding div uses display: flex to position its children side by side. Currently, I've applied the following CSS styles to the paragraph:

   white-space: nowrap;
   overflow: hidden;
   width: 300px;

This solution works, but I want the paragraph to adapt to the normal size of the container and not exceed it. If it does, I'd like the container to maintain its original size while cutting off any excess text within the paragraph.

Answer №1

If you're unsure about what you want to accomplish, have you experimented with utilizing the CSS property text-overflow: ellipsis;? Let me guide you through the implementation:

.mother_div {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.mother_div>div {
  flex-basis: 50%;
}

.text-holding_div {
  background-color: red;
  white-space: nowrap;
  overflow: hidden;
}

.text-holding_div p {
  text-overflow: ellipsis;
  overflow: hidden;
}

.second_child {
  background-color: green;
}
<div class="mother_div">
  <div class="text-holding_div">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt, ex vel bibendum egestas, tellus dui fringilla tellus, eget imperdiet odio quam at nisi. Phasellus ultrices non diam quis consequat. Vivamus eget sapien eget nunc consequat fermentum
      vitae nec neque. Nam magna lorem, convallis id euismod sit amet, egestas sed tortor. </p>
  </div>
  <div class="second_child"></div>
</div>

This approach eliminates the need to manually set a fixed width for the paragraph tag, and it also gives a cleaner appearance to the cut-off text.

Answer №2

If you wish to allow two lines and avoid using white-space: nowrap, consider implementing line-clamp: 2 in this way:

.mother_div {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.mother_div>div {
  flex-basis: 50%;
}

.text-holding_div {
  background-color: red;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.second_child {
  background-color: green;
}
<div class="mother_div">
  <div class="text-holding_div">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt, ex vel bibendum egestas, tellus dui fringilla tellus, eget imperdiet odio quam at nisi. Phasellus ultrices non diam quis consequat. Vivamus eget sapien eget nunc consequat fermentum
      vitae nec neque. Nam magna lorem, convallis id euismod sit amet, egestas sed tortor. </p>
  </div>
  <div class="second_child"></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

Please proceed with submitting your choices in the order that you have selected

My goal is to submit options from a dropdown list in the order they are selected, rather than in the order they appear in the list. How can I achieve this? Here is the HTML code for the dropdown: < select style = "padding: 1em;" name = "skills" multi ...

Add two columns for mobile devices in the Woocommerce platform

How can I display 2 columns of products in my Woocommerce shop using a child theme based on 'Shopisle'? Is a CSS-only solution the best approach for this task, and will it work smoothly without any bugs? I suspect that the theme is built on Boot ...

Stop the page from scrolling when scrolling within a specific DIV to address the [Violation] warning appearing in the console

Initially, it may seem like a duplicate question that has been answered here, but there are additional aspects that need to be addressed. How do I resolve the following [Violation] warning in the Google Chrome console? [Violation] Added non-passive eve ...

When the next button is clicked, the button content disappears

I am struggling with a problem involving two buttons that store tables. The issue is, I want the table to disappear when the second button is clicked and show the contents of the second button immediately after clicking it once, rather than having to click ...

Incorporate dc.js into your Cumulocity web application

Our team is facing a challenge while trying to integrate dc.js into our Cumulocity web application. While the standalone version works perfectly, we encounter issues when attempting to use it within Cumulocity. Below is the code for our standalone applica ...

Slider text in Master not adjusting properly for mobile devices

I am at my wits' end trying different solutions to make this work properly. The text on the slider of my homepage appears jumbled up when viewed on a mobile device. I suspect it might be related to responsive design? Could someone please take a look ...

What is the best way to eliminate the white margin that is showing up on the right side of my website?

Here's a question that has been bugging me recently... So, I've been working on creating my own version of an Airbnb website called 'tombnb' for a few weeks now. Everything was going smoothly until I encountered a persistent issue. Th ...

Unexpected behavior from HTML5 number input field

Is there a way to prevent users from entering values outside the specified max and min attributes? The constraints work when using the arrows in the input field, but not when typing directly into it. Note: I am considering utilizing angularjs for this fun ...

Uncovering data treasures on the web with BeautifulSoup: Extracting JSON files from DIV elements made simple

As I attempted to extract data from a website using standard class names for the required elements, I came across an interesting discovery. Near the top of the HTML code, there was what appeared to be a JSON file containing all the necessary information. ...

Choose the option to retrieve information from the database

Within my database, there is a table named "train_information" which contains the following fields: train_id country_id user_id train_name tare_weight number_of_bogies number_of_axles wheel_diameter_min wheel_diameter_max In addition, I have developed t ...

Executing Javascript code from a specified web address

Let's say I have an image that needs to be shifted vertically, and I achieve it using the following code snippet: document.getElementById('ImgID1').style.verticalAlign = However, the value by which I need to shift the image is provided thr ...

When attempting to showcase an image within an Angular form, the error message "Form control with unspecified name attribute lacks a value accessor" is displayed

I have a scenario where I am overlaying icons on an image within my ngForm. The goal is to allow users to drag the icons and save their new location when the form is submitted. Everything works as expected, but I encounter a pesky error whenever the page l ...

Customize the background color of Material UI input components on standard labels

Currently using react material ui and interested in changing the background color of a standard input. The issue arises when the label is overridden by the background color when the input is not selected. Input selected Input not selected This is my att ...

Applying class to target specific screen size in Bootstrap

I've been utilizing bootstrap for my project. My objective is to assign multiple classes based on screen size, such as sm-col and xs.col. For example, let's say I have a text id <div id="text" class="xs-format lg-format ..."></div> ...

Safari re-downloads background image when revisiting with jQuery CSS

Using jQuery to set a background-image on my website: $('.header-image').css('background-image', 'url(/img/image.jpg)'); However, upon returning to the page from Safari, the image is downloaded again, unlike Chrome and F ...

The icon on my page is not displaying, and the responsive menu is also not appearing

After tweaking the @media {} settings, I noticed that the icon displays properly when reduced, but disappears completely when fully covered with {}. Additionally, my responsive menu is not visible as expected. `@import url('https://fonts.googleap ...

How can I transfer a particular data value from a div to JavaScript within Laravel 5.0?

Displaying separate square divs based on integers retrieved from the database. This is the front-end view. I want to pass the room ID (code) to a JavaScript function when clicking on these div elements. https://i.stack.imgur.com/aIYTr.png Below is my cur ...

Top method for centering a flexible SVG vertically once the page width becomes too narrow

Currently, I have two SVG images displayed side by side on a webpage. One SVG needs to maintain a fixed size while the other should scale as needed, and I have achieved this functionality. However, I am facing an issue where I want the two SVGs to align v ...

Go to a distant web page, complete the form, and send it in

As the creator of Gearsbook.net, a social network for Gears of War players, I am constantly striving to improve the site's functionality. Currently, it is quite basic and in need of updates. One highly requested feature by users is the ability to lin ...

The database powered by Postgresql performs flawlessly when it comes to updating data with accurate code execution. However, there seems to be an

Imagine a zoo with a postgresql database. To enable web access to this database, I am using php and javascript. Most of the web pages are working fine, but I am currently working on a page where clients can add or remove animals from existing exhibits. T ...