0

I'm building a highscores page template, And would like to trim the username (ideally with CSS) if the username and/or score is too long. See this picture :

highscores page template

I can use

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

To trim the username, but how can I trim it relatively to the score length ?

Have you got an idea of how doing this ? Here's a Codepen of my current code : http://codepen.io/anon/pen/yymZGM

Thanks

gordie
  • 1,637
  • 3
  • 21
  • 41

3 Answers3

3

Just move the score span (which is floated right) before the name

Updated 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>
Danield
  • 121,619
  • 37
  • 226
  • 255
1

Semantically, you need a <table>, so that way more simple:

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 {
        }
    }
}
romuleald
  • 1,406
  • 16
  • 31
0

Try like this: Demo

.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;
}
G.L.P
  • 7,119
  • 5
  • 25
  • 41