The width of the Div element is not determined by the parent CSS grid, leading to a lack of ellipsis

I am working on a grid layout where each column contains a div with text. My goal is to have the text show ellipsis when it overflows the column, even though the number of columns is random.

I attempted to use the following CSS for the text-containing div:

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

However, I found that unless I specify a width, the ellipsis will not be displayed.

Here is my CSS file:

.top-container {
  width: 400px;
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.column-container-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.column-container-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

And here is my HTML code:

<div>
   <div className="top-container">
       <div style={{background: "grey"}}>first column</div>
       <div style={{background: "yellow"}}>second column</div>
     </div>
     <div className="top-container">
       <div className="column-container-2">
          <div style={{gridColumnStart: 1, background: "blue"}}>
              <div className="truncate">This is my long text</div>
           </div>
           <div style={{gridColumnStart: 2, background: "red"}}>
              <div className="truncate">This is another long text</div>
           </div>
        </div>
        <div className="column-container-3">
           <div style={{gridColumnStart: 1, background: "green"}}>
               <div className="truncate">This is another long text</div>
           </div>
           <div style={{gridColumnStart: 2, background: "purple"}}>
              <div className="truncate">This is another long text</div>
           </div>
           <div style={{gridColumnStart: 3, background: "black"}}>
              <div className="truncate">This is another long text</div>
           </div>
        </div>
     </div>
</div>

You can view the code on Stackblitz here: https://stackblitz.com/edit/react-rtrhv3

My goal is for the columns in the following section to be the same size as the

<div style={{background: "grey"}}>first column</div>
and to evenly split their width among the 3 columns. The text within the columns should be truncated with ellipsis to fit the parent container.

Answer №1

Looks like you've nested it too deeply:

  • Try removing the top-container wrapper for the second row,
  • Since the default min-width or min-height for grid items is auto, the ellipsis is not visible - reset this by using min-width: 0 for all grid items, and
  • gridColumnStart inline definitions are unnecessary in this case.

Check out the demo below and the updated stackblitz:

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      name: 'React'
    };
  }

  render() {
    return (
      <div class="container">
          <div style={{background: "grey"}}>first column</div>
          <div style={{background: "yellow"}}>second column</div>
          <div className="column-container-2">
            <div style={{background: "blue"}}>
              <div className="truncate">This is my long text</div>
            </div>
            <div style={{background: "red"}}>
              <div className="truncate">This is another long text</div>
            </div>
          </div>
          <div className="column-container-3">
            <div style={{background: "green"}}>
              <div className="truncate">This is another long text</div>
            </div>
            <div style={{background: "purple"}}>
              <div className="truncate">This is another long text</div>
            </div>
            <div style={{background: "black"}}>
              <div className="truncate">This is another long text</div>
            </div>
          </div>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
.container {
  display: grid;
  width: 400px;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr; /* added */
}

.column-container-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.column-container-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.column-container-2 > div,
.column-container-3 > div {
  min-width: 0; /* added */
}

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></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

React Application for Multiple Tenants

I'm currently developing a React multi tenant application and am looking for the most effective method to generate and utilize tenant-specific variables. By running my app with: npm run start tenant1 I can access tenant1 within Webpack using the fo ...

When the context is irrelevant, the next optimal option would be chosen

Is there a way for two React components to exchange data without using React Context? I am trying to integrate two separate third party "widgets" into different sections of the same web page, but I don't have control over React Context. Currently, I h ...

I am experiencing issues with arrow pagination not functioning properly in TypeScript

My current project involves building a customer table with 10 customers displayed on each page. Additionally, there are arrows below the table to help users navigate and view more customers. Unfortunately, there seems to be an issue with the functionality ...

Properly managing mouseover events on a flipped div: tips and tricks

I created a div that flips when clicked using some HTML and CSS code. It works perfectly in Firefox 39 and Chrome 43. Here is the markup: <div class="flip-wrapper flippable-wrapper" id="fliptest-01"> <div class="flip-wrapper flippable ...

Formatting JSON Date Output in a Unique Style

I am sending an api request and I would like to present the date in a similar format to what can be seen at this link: Here is the json data I am receiving: dates: { start: { localDate: "2017-04-06", localTime: "19:31 ...

React route with custom webpack configuration is disrupted by subdirectories in the URL

In my user interface, I have set up a page to display a list of items along with a form for adding new ones. However, when I try to access the "New Company" link, I encounter a 404 error and see a blank screen indicating that it cannot find the main.js fil ...

Displaying a component multiple times based on an array retrieved from an API

I am currently working on a side project to create a stock market app. I have utilized the useState hook in order to retrieve data from the finnhub API, resulting in an array of objects representing various stock market companies. const [finance, setFinanc ...

Adjust the position of a child element to be just outside the boundaries of its parent element using CSS

I am facing an issue with 2 nested divs in my HTML code. The design requires the inner div to appear "on top of" the outer div, like this: (source: examplewebsite.com) Although I have applied CSS to both elements, including a negative top margin on t ...

the child div within a Bootstrap 3 column is not being properly styled

Experiencing a strange issue with a relative div within a column in Bootstrap 3. Here's my code: <div class="col-lg-6"> <div class="large" style="background: url(img/bg.jpg);"> <h1 class="grid-header">Content 1</h1> ...

Having trouble getting overflow:auto to work with CSS3 transformed child elements? Looking for a recommended workaround?

Issue: Browsers (FF5, Chrome12, IE9) are not recognizing css3 transforms applied to a child element inside a div when calculating the scrollHeight and scrollWidth of the containing div's scrollbars with "overflow: auto;". <style type="text/css"> ...

Centering "Label - Value" without tables in web design

It's common to have multiple labels (such as name, age, color) and a corresponding value for each one. One way to ensure that the values (for example Steve, 19, Red) all start in the same horizontal position is by organizing them in a 2 column, 3 row ...

The correct rendering of background image paths is not functioning properly

Currently, I am utilizing Django Compress to streamline the process of using fewer files in their original format, rather than converting them to CSS files. This method has been successful except for a minor issue with translating the paths for background ...

React slick does not display arrows when there are 4 or more photos

I am facing an issue where the next and previous arrows are not appearing when I have 4 or more photos on react-slick. However, they show up fine when there are 3 or fewer photos. You can view my code at this link: https://codesandbox.io/s/wyyrl6zz3l ...

What is the Proper Way to Add Inline Comments in JSX Code?

Currently, I am in the process of learning React and I have been experimenting with adding inline comments within JSX. However, when I try to use the regular JavaScript // comments, it leads to a syntax error. Let me share a snippet of my code below: const ...

What is the best way to target the final child element of a specific CSS class?

I am working with an HTML table and my goal is to remove the border-bottom from the last row, excluding the row with class .table-bottom: <table cellpadding="0" cellspacing="0" style="width:752px;" class="table"> <tr class="table-top">< ...

Tips on how to align several divs within another div

I've been attempting to center multiple div blocks within another div or HTML document, but I haven't had any success with the methods I've found on StOv. Here's an image of what I'm aiming for: https://i.stack.imgur.com/DB7uQ.jp ...

What is the best way to organize my npm module to utilize an external configuration file if one is available?

In the process of creating an npm module using React, I have successfully utilized webpack to bundle the code. However, my goal now is to allow for global customization of the module. To achieve this, I plan to utilize a config.json file located in the pro ...

What is the best way to exhibit information retrieved from my express server on the React frontend?

I've been working on connecting an Express backend with MySQL data and a React frontend for iterating through the information. While I haven't encountered any errors, nothing seems to display on React. What could be missing? The following code ...

MUI full screen dialog with material-table

Issue: When I click a button on my material-table, it opens a full-screen dialog. However, after closing the dialog, I am unable to interact with any other elements on the screen. The dialog's wrapper container seems to be blocking the entire screen. ...

What is the best way to send data from a Laravel Server to React using inertiajs?

I'm currently attempting to display a message after a post in React from a Laravel server using InertiaJS. After researching the documentation, online resources, and similar questions, I have come up with what I believe should work. I think the follo ...