CSS实现⽂字头像(圆⾓头像⽂字内容)先上效果图。
关键css:
.avatar-container{
height: 1.5rem;
width: 1.5rem;
display: inline-block;
background: #aaaaaa;
text-align: center;
border-radius: 0.75rem;
}
.avatar-content{
margin-top: 0.25rem;
display: block;
}
其实本来想直接⽣成⼀张图⽚来着,后来发现js直接⽣成图⽚的效果不是很好,基本都是要⽤ canvas,效果、性能都是问题。后来发现⾃⼰真实傻,直接⽤css做⼀个出来不就⾏啦。
原理就是,⼀个div作为外壳,设置圆⾓,设置背景。把⽂字内容居中,OK了。
以下是例⼦的完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Avatar Test</title>
<meta name="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
<style>
body{
background: #F6F6F6;
color: #555555;
overflow-x: hidden;
position: relative;
padding-bottom: 2.1rem;
font-family:"微软雅⿊", arial;
line-height: 1.6;
font-size: 16px;
}
.address-box{
background-color: #FFFFFF;
margin: 0.4rem 0.2rem 0.4rem 0.2rem;
border-radius: 5px;
padding-left: 0.4rem;
padding-bottom: 0.2rem;
}
.address-box-up{
padding-top: 0.6rem;
font-weight: 700;
}
}
.avatar-container{
height: 1.5rem;
width: 1.5rem;
display: inline-block;
background: #aaaaaa;
text-align: center;
border-radius: 0.75rem;
}
.avatar-content{
margin-top: 0.25rem;
display: block;
}
.user-name{
margin-left: 0.4rem;
}
.
address-box-middle{
font-weight: 700;
margin-top: 0.2rem;
margin-bottom: 0.2rem;
}
.address-box-down{
font-size: 0.55rem;
color: #aaaaaa;
}
.address-box-down img{
position: relative;html span 居中
top: -1rem;
height: 1rem;
float: right;
right: 0.5rem;
}
.address-box-down span{
display: block;
margin-right: 2rem;
}
</style>
<script type="text/javascript">
function setRem(){
var html = document.documentElement;
var hWidth = BoundingClientRect().width;
html.style.fontSize = hWidth /16+'px';
}
setRem();
</script>
</head>
<body>
<div class="address-box">
<div class="address-box-up">
<div class="avatar-container"><span class="avatar-content">亮</span></div> <span class="user-name">诸葛亮</span>
</div>
<div class="address-box-middle">
<span>188********</span>
</div>
<div class="address-box-down">
<span>四川省成都市武侯区武侯⼤道南段150号</span>
</div>
</div>
<div class="address-box">
<div class="address-box-up">
<div class="avatar-container"><span class="avatar-content">亮</span></div> <span class="user-name">诸葛亮</span>
<span class="user-name">诸葛亮</span>
</div>
<div class="address-box-middle">
<span>188********</span>
</div>
<div class="address-box-down">
<span>四川省成都市武侯区武侯⼤道南段150号</span>
</div>
</div>
<div class="address-box">
<div class="address-box-up">
<div class="avatar-container"><span class="avatar-content">亮</span></div> <span class="user-name">诸葛亮</span>
</div>
<div class="address-box-middle">
<span>188********</span>
</div>
<div class="address-box-down">
<span>四川省成都市武侯区武侯⼤道南段150号</span>
</div>
</div>
<div class="address-box">
<div class="address-box-up">
<div class="avatar-container"><span class="avatar-content">亮</span></div> <span class="user-name">诸葛亮</span>
</div>
<div class="address-box-middle">
<span>188********</span>
</div>
<div class="address-box-down">
<span>四川省成都市武侯区武侯⼤道南段150号</span>
</div>
</div>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。