0

我正在构建一个高分页面模板,如果用户名和/或分数太长,我想修剪用户名(最好使用 CSS)。看这张照片:

高分页面模板

我可以用

white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
overflow-y:none;

修剪用户名,但我怎样才能相对于分数长度修剪它?

你知道如何做到这一点吗?这是我当前代码的 Codepen:http ://codepen.io/anon/pen/yymZGM

谢谢

4

3 回答 3

3

只需将分数跨度(向右浮动)移动到名称之前

更新了 CODEPEN

#highscores {
  width: 400px;
  padding: 1em;
  background-color: yellow;
  margin: auto;
  font-size: 1.5em;
}
#highscores .round {
  font-size: 1.2em;
  line-height: 1.2em;
  height: 1.2em;
  position: relative;
  background-color: red;
  padding: 0.4em 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow-y: none;
}
#highscores .round .round-index {
  padding-right: 2%;
}
#highscores .round .round-author {
  font-weight: 700;
  width: 60%;
}
#highscores .round .round-score {
  width: 28%;
  float: right;
  text-align: right;
}
<div id="highscores" class="celio-black">
  <p id="round-4" class="round">
    <span class="round-score">75</span>
    <span class="round-index">01</span>
    <span class="round-author">grosbouff</span>

  </p>
  <p id="round-1" class="round">
    <span class="round-score">75486987</span>
    <span class="round-index">02</span>
    <span class="round-author">I have a super long name isn't it ?</span>

  </p>
</div>

于 2015-04-16T08:22:22.870 回答
1

从语义上讲,您需要一个<table>,这样更简单:

HTML

<table id="highscores" class="celio-black">
  <tr id="round-4" class="round">
    <td class="round-index">01</td>
    <td class="round-author">grosbouff</td>
    <td class="round-score">75</td>
  </tr>                            
  <tr id="round-1" class="round">
    <td class="round-index">02</td>
    <td class="round-author">I have a super long name isn't it ?</td>
    <td class="round-score">75486987</td>
  </tr>                                              
</table>

SCSS

#highscores {
    width: 400px;
    padding: 1em;
    background-color: yellow;
    margin: auto;
    font-size: 1.5em;
    table-layout: fixed;
    border-collapse: collapse;
    .round {
        font-size: 1.2em;
        line-height: 1.2em;
        height: 1.2em;
        position: relative;
        background-color: red;
        padding: 0.4em 0;
        .round-index {
            padding-right: 2%;
        }
        .round-author {
            font-weight: 700;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            overflow-y: none;

        }
        .round-score {
        }
    }
}
于 2015-04-16T08:38:25.237 回答
0

试试这样:演示

.round {
    font-size: 1.2em;
    line-height: 1.2em;
    height:1.2em;
    position: relative;
    padding: 0.4em 0;
    overflow-y:none;
}
.round-index {
    padding-right: 2%;   
}
.round-author {
    font-weight: 700;
    width: 60%;
    max-width: 250px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    background-color:red;  
    display:inline-block;
}
.round-score {
    width: 28%;
    float: right;
    text-align: right;    
    max-width: 100px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    background-color:green;  
    display:inline-block;
}
于 2015-04-16T08:34:13.743 回答