flexbox-减少垂直间距

时间:2019-08-07 09:49:13

标签: css flexbox

如何减小文本之间的垂直间距并使文本中心对齐?

.item{
  border: 1px solid;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  height: 200px;
  margin: 4px;
  float: left;
  text-align: center;
  width: 200px;
}
h3, h4{
  margin: 0;
  width: 100%;
}
<div class="item">
  <h3>Title1</h3> 
  <h4>Title2</h4>
</div>
<div class="item">
  <h3>Title1</h3> 
  <h4>Title2</h4>
</div>
<div class="item">
  <h3>Title1</h3> 
  <h4>Title2</h4>
</div>
 

1 个答案:

答案 0 :(得分:1)

删除flex-wrap: wrap;并添加justify-content: center; flex-direction: column;

.item{
  border: 1px solid;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  height: 200px;
  margin: 4px;
  float: left;
  text-align: center;
  width: 200px;
}
h3, h4{
  margin: 0;
  width: 100%;
}
<div class="item">
  <h3>Title1</h3> 
  <h4>Title2</h4>
</div>
<div class="item">
  <h3>Title1</h3> 
  <h4>Title2</h4>
</div>
<div class="item">
  <h3>Title1</h3> 
  <h4>Title2</h4>
</div>