【问题标题】:How can I align these 2 elements so that the text is vertically centered to the icon?如何对齐这两个元素,使文本垂直居中于图标?
【发布时间】:2022-11-24 20:43:51
【问题描述】:

我试图让文本垂直位于菱形框旁边的中间,数字如下:

目前我有这段代码:

.diamond {
  width: 50px;
  aspect-ratio: 1;
  font: 20pt Arial, sans-serif;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #EB008B;
  margin: 20px;
  margin-bottom: 0px;
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}

.image-txt-container {
  display: flex;
  align-items: center;
  flex-direction: row;
}

.pf-title {
  margin-right :auto;
}
<div class="image-txt-container">
  <div class="diamond">1
  </div>
      <h4 class="pf-title">BRIDGING / SHORT-TERM FINANCE</h4>
</div>

【问题讨论】:

  • 消除底部边距:0px;来自钻石级

标签: html css flexbox vertical-alignment


【解决方案1】:

.diamond {
  width: 50px;
  aspect-ratio: 1;
  font: 20pt Arial, sans-serif;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #EB008B;
  margin: 20px;
  margin-bottom: 0px;
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}

.image-txt-container {
  display: flex;
  align-items: baseline;
  flex-direction: row;
}

.pf-title {
    margin-right: auto;
    font-size: 30px;
}
<div class="image-txt-container">
  <div class="diamond">1
  </div>
      <h4 class="pf-title">BRIDGING / SHORT-TERM FINANCE</h4>
</div>

我想你想要这样的东西

【讨论】:

  • 太好了谢谢
【解决方案2】:

通过写 margin:20px,你添加了 20px 的边距。

并使用 margin-bottom:0px 删除此边距,这意味着您的文本不居中。

如果你只需要右边距,你可以使用 margin-right:20px;

.diamond {
  width: 50px;
  aspect-ratio: 1;
  font: 20pt Arial, sans-serif;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #EB008B;
  margin-right: 20px;
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}

.image-txt-container {
  display: flex;
  align-items: center;
  flex-direction: row;
}

.pf-title {
  margin-right :auto;
}
<div class="image-txt-container">
  <div class="diamond">1
  </div>
      <h4 class="pf-title">BRIDGING / SHORT-TERM FINANCE</h4>
</div>

【讨论】:

    【解决方案3】:

    你的问题已经在那里得到回答。我试图给出不同的答案

    所以我从.diamond 中删除边距并给.pf-title 填充

    像这样

    .diamond {
      width: 50px;
      aspect-ratio: 1;
      font: 20pt Arial, sans-serif;
      color: white;
      display: flex;
      justify-content: center;
      align-items: center;
      background: #EB008B;
      /* margin: 20px; */     <--- remove
      margin-bottom: 0px;
      clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
    }
    
    .image-txt-container {
      display: flex;
      align-items: center;
      flex-direction: row;
    }
    
    .pf-title {
      padding: 10px;     <--- add
      margin-right :auto;
    }
    <div class="image-txt-container">
      <div class="diamond">1</div>
      <h4 class="pf-title">BRIDGING / SHORT-TERM FINANCE</h4>
    </div>
     

    我希望这对您有所帮助,并且可以成为您的另一个参考

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 2012-08-22
      相关资源
      最近更新 更多