button内flex垂直居中不居中怎么解决

2023-06-07,

本篇内容介绍了“buttonflex垂直居中不居中怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

问题描述

按钮样式为图标+文字,在使用flex布局写垂直居中时,iphone7手机上文字和图标却没有居中,居左显示。代码如下(已精简):

<button>
  <img src="./refresh.png" alt />
  {{ confirmButtonText }}
</button>

...
button {
  display: flex;
  align-items: center;
  justify-content: center;
  img {
      width: 36px;
      height: 36px;
      display: inline-block;
  }
}

预期样式:

实际样式:

解决方式

给icon和文字外再包一层标签,给外层标签设置flex垂直居中样式,代码如下:

<button>
  <span class="wrap">
     <img src="./refresh.png" alt />
      {{ confirmButtonText }}
  </span>
</button>

...
button {
   display: flex;
   align-items: center;
   justify-content: center;
  .wrap {
      img {
          width: 36px;
          height: 36px;
          display: inline-block;
      }
  }
}

“button内flex垂直居中不居中怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注本站网站,小编将为大家输出更多高质量的实用文章!

《button内flex垂直居中不居中怎么解决.doc》

下载本文的Word格式文档,以方便收藏与打印。