【问题标题】:Centring an element above another element [duplicate]将一个元素居中于另一个元素上方[重复]
【发布时间】:2022-01-18 22:22:29
【问题描述】:

努力将我的icon 居中放置在文本上方,图标的高度很好,但应该像这样:

尝试使用 flex 但找不到正确的解决方案。

.container {
  display: block;
  width: 400px;
  height: 100px;
  border: 2px solid black;
}

.icon {
  width: 30px;
  height: 30px;
  background-color: red;
}

.text {
  text-align: center;
}
<div class="container">
  <div class="icon"></div>
    <p class="text">We couldn't find any matches to your search. <br></br>Please try again.</p>
</div>  

【问题讨论】:

  • 您没有对图标应用任何居中。将文本对齐放置在父元素上,而不仅仅是文本。

标签: html css flexbox


【解决方案1】:

你可以使用 CSS flexbox: 但记住默认方向是row。如果你切换到flex-direction: column,那么你将实现你想要的:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 400px;
  height: 100px;
  border: 2px solid black;
}

.icon {
  width: 30px;
  height: 30px;
  background-color: red;
}

.text {
  text-align: center;
}
<div class="container">
  <div class="icon"></div>
    <p class="text">We couldn't find any matches to your search. <br />Please try again.</p>
</div>

【讨论】:

  • 你肯定知道这是多方面的重复。
  • @isherwood 您使用flex-direction: column关闭问题的解决方案都没有@
  • 然后考虑将该解决方案添加到副本中。问题还是一样。
  • 还有更多使用 flex-direction:column 的问题,我们在这里并不真正需要。边距:自动就足够了
猜你喜欢
  • 2017-05-13
  • 2021-12-06
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多