我正在使用Materialize类为用户创建一个“照片占位符”,其中包含其名字的第一个字母。
我在<td>
标记内使用长名称时遇到麻烦。我不想名称过长而损坏。因此,我尝试将class="truncate"
应用于包含该名称的垃圾邮件,但无法截断。
我该如何解决?
main {
padding: 16px;
}
table {
table-layout: fixed;
}
td {
width: 33.33%;
}
.photo-placeholder {
width: 40px;
height: 40px;
line-height: 40px;
border-radius: 50%;
text-align: center;
display: inline-block;
background-color: crimson;
color: white;
}
.truncate {
display: block;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<main>
<table id="table1" class="white z-depth-1 centered">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Col 1">
<span class="photo-placeholder">P</span>
<span class="truncate">Pedro Luis Arend Gonçalves</span>
</td>
<td data-label="Col 2">Info 2</td>
<td data-label="Col 3">Info 3</td>
</tr>
</tbody>
</table>
</main>
预期结果:
答案 0 :(得分:1)
似乎“ truncate”类将元素设置为display:block
,将其放在单独的行上。
一种解决方案是将其设置为display:inline-block
并将其宽度设置为表格单元格中剩余的空间。下面,我使用calc()
计算了适当的宽度。我还消除了两个<span>
元素之间的空白,因此不会增加多余的宽度。
main {
padding: 16px;
}
table {
table-layout: fixed;
}
td {
width: 33.33%;
}
.photo-placeholder {
width: 40px;
height: 40px;
line-height: 40px;
border-radius: 50%;
text-align: center;
display: inline-block;
background-color: crimson;
color: white;
}
#table1 .truncate {
display: inline-block;
width: calc(100% - 40px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<main>
<table id="table1" class="white z-depth-1 centered">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Col 1">
<span class="photo-placeholder">P
</span><span class="truncate">Pedro Luis Arend Gonçalves</span>
</td>
<td data-label="Col 2">Info 2</td>
<td data-label="Col 3">Info 3</td>
</tr>
</tbody>
</table>
</main>
为避免计算宽度,您只需float
占位符即可,如下所示:
main {
padding: 16px;
}
table {
table-layout: fixed;
}
td {
width: 33.33%;
}
.photo-placeholder {
float: left;
width: 40px;
height: 40px;
line-height: 40px;
border-radius: 50%;
text-align: center;
background-color: crimson;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<main>
<table id="table1" class="white z-depth-1 centered">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Col 1">
<span class="photo-placeholder">P</span>
<span class="truncate">Pedro Luis Arend Gonçalves</span>
</td>
<td data-label="Col 2">Info 2</td>
<td data-label="Col 3">Info 3</td>
</tr>
</tbody>
</table>
</main>
这是使用flexbox容器的另一种方法。我喜欢这个元素,因为使用align-items:center
将两个元素垂直居中比较容易。
main {
padding: 16px;
}
table {
table-layout: fixed;
}
td {
width: 33.33%;
}
.photoContent {
display: flex;
align-items: center;
}
.photo-placeholder {
flex: 0 0 40px;
height: 40px;
line-height: 40px;
border-radius: 50%;
text-align: center;
background-color: crimson;
color: white;
margin: 0 0.5em 0 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<main>
<table id="table1" class="white z-depth-1 centered">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Col 1">
<div class="photoContent">
<span class="photo-placeholder">P</span>
<span class="truncate">Pedro Luis Arend Gonçalves</span>
</div>
</td>
<td data-label="Col 2">Info 2</td>
<td data-label="Col 3">Info 3</td>
</tr>
</tbody>
</table>
</main>
答案 1 :(得分:0)
确保您还为元素设置了width
或max-width
样式规则。否则,只要有增长空间,它将占用该空间。在您的示例中,添加样式规则max-width: 50px;
对我有用。
答案 2 :(得分:0)
Fiddle尝试添加以下CSS:
.photo-placeholder {
margin: 10px;
min-width: 40px;
}
table.centered tbody tr td:first-child {
display: flex;
width: 100%;
align-items: center;
justify-content: center;
}