我在使用css网格的div中使用elipsis属性时遇到了一些问题。
如果我直接在网格子项中设置elipsis属性,它可以工作,但是当在网格中的div中使用时,它不起作用。
您可以在此处查看问题:
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>
&#13;
谢谢。
答案 0 :(得分:5)
通过在第二种情况下将overflow: hidden
应用于div
的容器,似乎可以解决此问题。
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 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 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>