【发布时间】:2016-03-02 09:31:20
【问题描述】:
我正在尝试对齐 div 容器内 <span> 标记中的一些文本。文本应位于容器的垂直中心,距左边框几个像素。到左边的距离没什么大不了,但我怎样才能把我的文字放在他父母<div>容器的垂直中心?
.container{
border: 1px solid red;
height: 50px;
width: 100px;
}
.container-content{
height: 40px;
border: none;
border-left: 1px solid black;
border-right: 1px solid black;
margin-top: 5px;
margin-left: 3px;
margin-right: 3px;
}
.container-content span{
margin-left: 2px;
}
<div class="container">
<div class="container-content">
<span>Text<span>
</div>
</div>
编辑
所有建议的答案都适用于文本,但如果我使用图标字体,则图标不会放在中心。如何用 googles material-icon 字体解决这个问题?
.container {
border: 1px solid red;
height: 50px;
width: 100px;
}
.container-content {
height: 40px;
border: none;
border-left: 1px solid black;
border-right: 1px solid black;
margin-top: 5px;
margin-left: 3px;
margin-right: 3px;
display: flex;
align-items: center;
}
.container-content span {
margin-left: 5px;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="container-content">
<span><span><i class="material-icons">check_circle</i><span>
</div>
</div>
</body>
</html>
【问题讨论】: