html - 如何使用 flex box 使图像垂直居中?

标签 html css ionic2 flexbox

<分区>

我正在尝试使用 flex box 将图像垂直居中。

这是我的代码:

<ion-grid>
    <ion-row>
      <ion-col>
        <div class="logo-part">
          <img id="logo" src="assets/logo/logo.png" alt="img-logo">
          <ion-title>Service at your fingertips!</ion-title>
        </div>
      </ion-col>
    </ion-row>
</ion-grid>

CSS:

ion-content ion-grid {
  height: 100%;
}
ion-content ion-row{
  height: 50%;
}
div.logo-part {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  flex-direction: column;
}
img{
width:25%;
text-align:center;
}

图片没有垂直居中!

有什么帮助吗?

谢谢。

最佳答案

那是因为 flex-direction: column,当你使用它时,align-items 开始水平对齐而不是垂直对齐,要垂直对齐你需要设置 对齐内容:居中

Just to explain this behavior: this is because justify-content aligns according to the main direction and align-items according to the intersection. So, when you change flex-direction from row (horizontal) to column (vertical), justify-content aligns vertically and align-items horizontally. - Rodrigo Ferreira

关于html - 如何使用 flex box 使图像垂直居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49156661/

相关文章:

css - 如何在平板电脑(md) View 中将第二行中的文本与第一行中的文本对齐

html - 鼠标悬停时如何使嵌套项的文本变为红色?

html - 将 Bootstrap CSS 与自定义 CSS 结合使用——这种做法不好吗?

javascript - Express Handlebars 自定义路径

android - 如何在 ionic 2 移动应用程序中获取 IMEI 号码?

html - 验证:元素链接上的属性 rel 首先是错误值

css - 如何让 css 混合模式应用于不同的 "layers"

css - 永久修改不属于我的网站的 CSS,并将更改分发到我的网络

angular - 使用 JSDoc 评论 Angular 2/Ionic 2 项目

ionic2 - 使用提醒确认按钮打开 Google Play