Is there a way to prevent the content in the <p> tag from wrapping and hiding its overflow?

Is there a way to use tailwindcss to ensure that the first <p> child truncates or hides overflow as its container shrinks? I've tried using classes like whitespace-nowrap and overflow-hidden, but the text always wraps regardless. Any suggestions on how to achieve this?

The flex boxes seem to work fine, with the first text shrinking while the second remains the same size as the inner-fb-one and inner-fb-two flex boxes shrink. The only issue is getting the text to not wrap and hide the overflow.

To c


    <div className='flex flex-row ml-3 space-y-1 items-end border' id='container-fb'>
      <div className='flex flex-row items-center' id='inner-fb-one'>
        <p className='whitespace-nowrap'>truncate this first child as the container shrinks</p>
        <p className='shrink-0'>keep this the same size</p>
      </div>
      <div className='flex flex-row items-center' id='inner-fb-two'>
        <p className='whitespace-nowrap'>truncate this first child as the container shrinks</p>
        <p className='shrink-0'>keep this the same size</p>
      </div>
    </div>

Answer №1

<div className='flex flex-row ml-3 space-y-1 items-end border' id='container-fb'>
  <div className='flex flex-row items-center' id='inner-fb-one'>
    <p className='whitespace-nowrap text-overflow-ellipsis overflow-hidden w-full'>
      When the container shrinks, truncate the content of this first child
    </p>
    <p className='shrink-0'>Ensure this child maintains its original size</p>
  </div>
  <div className='flex flex-row items-center' id='inner-fb-two'>
    <p className='whitespace-nowrap text-overflow-ellipsis overflow-hidden w-full'>
      When the container shrinks, truncate the content of this first child
    </p>
    <p className='shrink-0'>Ensure this child maintains its original size</p>
  </div>
</div>

Answer №2

To prevent text within a <p> element from wrapping and being hidden due to overflow, you can utilize the CSS property white-space: nowrap. Here is an illustration:

<p style="white-space: nowrap;">This is an example of text that will not wrap and will hide its overflow.</p>

If you wish to apply this styling to multiple elements, you can create a CSS class and assign it to the <p> elements:

.no-wrap {
  white-space: nowrap;
}

<p class="no-wrap">This is an example of text that will not wrap and will hide its overflow.</p>

Alternatively, you can use the property overflow: hidden to conceal the overflow, as shown below:

<p style="overflow: hidden;">This is an example of text that will not wrap and will hide its overflow.</p>

By combining these two properties, you can achieve the desired result, illustrated here:

<p style="white-space: nowrap; overflow: hidden;">
  This is an example of text that will not wrap and will hide its overflow.
</p>

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

Creating a dynamic visual effect by changing the background color's opacity with each

Is there a CSS or Less solution to change the background color of all dynamically added or removed child elements with a gradual increase of opacity? ul li { list-style: none; font-size: 16px; text-align: center; font-weight: bold; margin: 3p ...

Looking for assistance on how to automatically close the sidebar or pull navigation menu after clicking on a menu item

My website is designed to be responsive and includes a navigation slider that is hidden off-screen by default. The slider features a vertical push/pull bar that remains visible for users to access the menu. The entire navigation slider is fixed on the page ...

Unable to insert hyperlinks into table rows within a React application

A React function was created by me to display a table. This is how the table appears. It accepts JSON as a parameter: { "id": 1, "firstName": "Richard", "lastName": "Morrison", "number ...

Placing an element in a fixed position behind a non-fixed element

I am facing a challenge with an element that has two children, one of them is absolutely positioned. My goal is to position this absolutely placed element behind its sibling. Unfortunately, the z-index property doesn't seem to be working as expected. ...

Putting an <input/> element inside a Material UI's <TextField/> component in ReactJS - a beginner's guide

I am attempting to style <TextField/> (http://www.material-ui.com/#/components/text-field) to resemble an <input/>. Here is what I have tried: <TextField hintText='Enter username' > <input className="form-control ...

Is there a way to keep the background image stationary as I scroll?

As someone who is new to html and CSS, I recently tried to enhance my previous project by incorporating a background image that spans the entire screen size and remains fixed while scrolling up or down. Here is the code snippet: ''' body { ...

Steps to display a variable in JavaScript on an HTML textarea

I'm working on a JavaScript variable called 'signature' var signature; //(Data is here) document.write(signature) Within my HTML document, I have the following: <div id="siggen"> <textarea id="content" cols="80" rows="10">& ...

The elements in an array.map are not being detected correctly by React / Javascript

import React, { Component } from "react"; class LogIssueScreen extends Component { constructor(props) { super(props); this.state = {}; } componentDidMount() { fetch(`${apiRoot}log_issue`, { method: "GET", }) ...

"Exploring the variations in design between wordpress.com and self-hosted wordpress

I'm currently puzzling over the source of the disparities in style between my Wordpress site hosted elsewhere and my identical theme on Wordpress.com. It's perplexing to see such contrasting appearances while I migrate my blog. After some irrele ...

Maintain the HTML font color when printing - Issue with IE settings, not the printer

I've been struggling with this issue all day. Initially, I thought it was a problem with the print settings, but then I realized that it's actually the "Print background colors and images" option in IE causing the trouble. Here is the last test ...

Can someone assist me in choosing a specific button from a group of buttons within material ui?

I have a situation where I have an array of phone numbers, each one with a button to copy the number and initiate a call. The challenge is that we are using material-ui v4 for our UI components. What I would like to achieve is that when someone clicks on ...

In IE8, a border is applied to the max-width property when the width is set to

I'm attempting to design a box that adjusts in size according to the parent container, utilizing a mix of max-width and width:100%. With this setup, the box's width is determined by the max-width property, ensuring it shrinks within its boundari ...

Best practices for maximizing efficiency with a shared component library

I am currently developing a React component library that is designed to be easily shareable. This library consists of numerous components, but it's possible that the end user may only require a select few for their project. When using Webpack (or Pa ...

Experiencing difficulty aligning the Material UI grid to float on the right side

I'm currently in the learning phase of material-ui and encountering an issue with floating the grid content to the right side. Despite trying alignItems="flex-start" justify="flex-end" direction="row" in the container grid, as well as using the css p ...

Generating an unexpected card image: "Requesting unfamiliar module"

I am attempting to create a random card image based on the string returned by this.generateRandomCard(), but I'm encountering the following error: "Requiring unknown module "./card_images/7S.png". If you are sure the module is there, try restarting t ...

Tips for using Howler library in React to automatically play audio upon loading the page

I'm troubleshooting why the audio on my webpage won't start playing when the page loads, and instead only plays after a mouse click triggers an event. The audio works fine but I want it to automatically play as soon as the page is loaded. import ...

Issue with nth-child selector not functioning as expected in unordered list

I have a straightforward <nav> setup with an unordered list inside: nav { position: absolute; right: 0px; white-space: nowrap; } nav ul { padding: 0; margin: 0; } nav ul li { clear: both; white-space: nowrap; color: white; displa ...

Tips on gathering data from the Mui date picker

On my website, I am using a date picker from MUI and I want to capture start and end dates just like we do with regular input fields. Here is the snippet of code I have: const Dates = [ { label: "From", name:"From" ,placeholder: " ...

Styling a Pie or Doughnut Chart with CSS

I am working on creating a doughnut chart with rounded segments using Recharts, and I want it to end up looking similar to this: Although I have come close to achieving the desired result, I am encountering some issues: Firstly, the first segment is over ...

Error: The 'useContext' property cannot be read as it is null in nextjs

In my Next.js, Strapi, Apollo GraphQL project, I am trying to access the user from context and use it in the header. The User object is accessible within the App function, but outside of it, I'm encountering an error. Is it possible to access context ...