Ellipsis not working inside CSS Grid Layout [duplicate]
Asked Answered
E

1

6

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.

Ehrlich answered 20/10, 2017 at 16:5 Comment(1)
An initial setting on grid items is min-width: auto. This means that an item cannot be shorter than its content. You can override this default with min-width: 0 or overflow with any value other than visible. jsfiddle.net/epgghwnaCha
A
18

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>
Agree answered 20/10, 2017 at 16:10 Comment(3)
Perfect, it works fine :)Ehrlich
That did the trick for me, thanks!Ballon
Appears the doesn't worl [sic] example actually works now.Eolith

© 2022 - 2024 — McMap. All rights reserved.