【发布时间】:2017-06-12 16:03:36
【问题描述】:
我有一个瓷砖网格,其宽度是容器的百分比。每个图块都包含一个图标和一个标题,文本与图标左对齐。
我希望图标和文本的集合在该图块中居中。问题是一旦文本足够长可以换行,包含文本的 div 就会声称它是容器的整个剩余宽度。
tl:dr,我希望用红色圈出的空间分布在图标和文本的两侧,因为它会查找“教育”图块。
一些图块看起来像:
<ul class="grid">
<li class="tile">
<div class="container">
<i>i</i>
<div class="title">
Education
</div>
</div>
</li>
<li class="tile">
<div class="container">
<i>i</i>
<div class="title">
title containnnng longish words
</div>
</div>
</ul>
使用 css 之类的:
ul.grid {
list-style: none;
margin: 0;
padding: 0;
}
.tile {
display: block;
width: 150px; /* this is a percentage of the container width in real life */
height: 80px;
background-color: rgb(192, 230, 245);
margin: 5px;
}
.container {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
/* this is an icon, I just don't feel like importing font awesome here */
i {
margin-right: 5px;
}
JS fiddle 让它更清晰: https://jsfiddle.net/avaleske_socrata/nt0vhtmg/1/
【问题讨论】:
-
基本上你希望它居中?
-
左对齐太令人困惑了。
-
@RokoC.Buljan 是的,这正是问题所在。没看到这个问题。没有人解决它,这并不令人振奋。
标签: html css flexbox centering