Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ellipsis not working inside CSS Grid Layout [duplicate]

I am having some problems using elipsis property inside a div with css grid.

If I set the elipsis property directly in a grid child it works, but when used inside a div in a grid it doesn't work.

You can see here what's the problem:

body{
  text-align:center;
}
.container{
  width: 300px;
  background-color:#ccc;
  margin-left:auto;
  margin-right:auto;
}

#grid{
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

#grid > div{
  border:1px solid #000;
  padding:5px;
}

.elipsis{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
<h2>This works</h2>
<p>as selipsis class is directly in col-2 div</p>
<div class="container">
  <div id="grid">
    <div class="col-1">A</div>
    <div class="col-2 elipsis">
      B B B B B B B B B B B B B B B B B
    </div>
    <div class="col-3">C</div>
  </div>
</div>

<h2>This doesn't worl</h2>
<p>as selipsis class is in a div inside col-2 div</p>
<div class="container">
  <div id="grid">
    <div class="col-1">A</div>
    <div class="col-2">
      <div class="elipsis">B B B B B B B B B B B B B B B B B</div>
    </div>
    <div class="col-3">C</div>
  </div>
</div>

Thank you.

like image 848
David Rojo Avatar asked Dec 18 '22 03:12

David Rojo


1 Answers

This seems to resolve by applying overflow: hidden to the container of the div in your second case.

body {
  text-align: center;
}

.container {
  width: 300px;
  background-color: #ccc;
  margin-left: auto;
  margin-right: auto;
}

#grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

#grid>div {
  border: 1px solid #000;
  padding: 5px;
}

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

.col-2b {
  overflow: hidden;
}
<h2>This works</h2>
<p>as elipsis class is directly in col-2 div</p>
<div class="container">
  <div id="grid">
    <div class="col-1">A</div>
    <div class="col-2 elipsis">
      B B B B B B B B B B B B B B B B B
    </div>
    <div class="col-3">C</div>
  </div>
</div>

<h2>This doesn't work</h2>
<p>as elipsis class is in a div inside col-2 div</p>
<div class="container">
  <div id="grid">
    <div class="col-1">A</div>
    <div class="col-2 col-2b">
      <div class="elipsis">B B B B B B B B B B B B B B B B B</div>
    </div>
    <div class="col-3">C</div>
  </div>
</div>
like image 190
sol Avatar answered Feb 04 '23 13:02

sol