Skip to content

Instantly share code, notes, and snippets.

@maxcelos
Created March 14, 2019 09:17
Show Gist options
  • Save maxcelos/7cc5b1aa56e3a787c379a225db72c4a9 to your computer and use it in GitHub Desktop.
Save maxcelos/7cc5b1aa56e3a787c379a225db72c4a9 to your computer and use it in GitHub Desktop.
Automatically creates a initials placeholder from user's name like Trello
<html>
<head>
<title>Trello's-like Name Initials</title>
<style>
[data-letters]:before {
content:attr(data-letters);
display:inline-block;
font-size:1em;
width:2.5em;
height:2.5em;
line-height:2.5em;
text-align:center;
border-radius:50%;
background: #eeeeee;
vertical-align:middle;
margin-right:0.2em;
color: #515151;
}
</style>
</head>
<body>
<span data-letters="" id="navUserInitials">
<span id="navUserName">Marcelo Barros</span>
</span>
<script>
let name = document.getElementById('navUserName').innerHTML;
let initialSpot = document.getElementById('navUserInitials');
let initials = name.match(/\b\w/g) || [];
initials = ((initials.shift() || '') + (initials.pop() || '')).toUpperCase();
initialSpot.setAttribute('data-letters', initials)
</script>
</body>
</html>
@ricardopedias
Copy link

Muito bom!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment