【问题标题】:how to vertically align both image and text?如何垂直对齐图像和文本?
【发布时间】:2021-08-24 15:50:12
【问题描述】:

所以我需要将图像和文本都垂直居中。但是,我的代码出了点问题,我不知道为什么。我可以让文本居中,但是,图像不是我不知道为什么。有任何想法吗? 这是我目前的代码。

.container {
  height: 300px;
  width: 300px;
  position: relative;
  border: 3px solid black;
  background-color: transparent;
}

.center {
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

.settings {
  width: 100px;
  height: 100px;
}

.connecttext {
  color: #000;
  text-align: center;
}
<div class="container">
  <div class="center">
    <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/69/How_to_use_icon.svg/1200px-How_to_use_icon.svg.png" class="settings">
    <p class="connecttext"><strong>Connect</strong><br>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
    </p>
  </div>
</div>

【问题讨论】:

    标签: html css alignment vertical-alignment


    【解决方案1】:

    将此添加到.center

    .center {
        display: flex;
        place-items: center;
        ...
    }
    

    【讨论】:

    【解决方案2】:
    .center {
    display: flex;
    justify-content: center; //for horizontal 
    align-items: center; //for vertically align 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 2011-09-25
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多